Find roots of any linear algebraic nth order equation by Regula Falsi method
The Code
Code: Cpp
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define ACC 0.00001
#define N 10
int degree;
float coeff[N];
void input(void);
float power(float,int);
float F(float);
/******************************************************/
/***** Finds the root of any linear algebraic *****/
/***** any order equation by Regula Falsi method *****/
/******************************************************/
void main()
{
char ch;
clrscr();
do
{
input();
printf("Do you wish to continue[y/n]\n");
fflush(stdin);
scanf("%c",&ch);
}while(ch=='Y' || ch=='y');
}
void input(void)
{
int i,print;
float a,b;
float f;
printf("\nEnter the degree of diferential equation\n");
scanf("%d",°ree);
print=degree;
for(i=0;i<=degree;i++)
{
printf("Enter the co-efficient of x to the power of %d\t",print--);
scanf("%f",&coeff[i]);
}
do
{
printf("\nEnter the values of a and b\n");
scanf("%f%f",&a,&b);
if(F(a)*F(b)>0)
printf("\nThere is no root in the given interval please re-enter\n");
}while(F(a)*F(b)>0);
if(F(a)*F(b)==0)
{
if(F(a)==0)
printf("\n%f is the root of the equation\n",a);
else if(F(b)==0)
printf("\n%f is the root of the equation\n",b);
else
printf("\n%f and %f are the roots of the equation\n",a,b);
}
do
{
f=((a*F(b))-(b*F(a)))/(F(b)-F(a));
if(F(a)*F(f)<0)
b=f;
else if(F(b)*F(f)<0)
a=f;
}while(fabs(F(f))>=ACC);
printf("\n%f is the root\n",f);
}
float F(float x)
{
int i,deg;
float func=0.0;
deg=degree;
for(i=0;i<=degree;i++)
{
func=func+coeff[i]*power(x,deg);
deg--;
}
return(func);
}
float power(float x,int deg)
{
int i;
float res=1;;
for(i=1;i<=deg;i++)
res=res*x;
return(res);
}
shabbir
likes this
