You are on page 1of 2

#include<stdio.

h>
#include<math.h>

float func(float x)
{
return (x*exp(x)-cos(x));
}

int main()
{

int i=0,pos=0;
float x0,x1,x2,fx0,fx1,fx2;

printf("\n\n\t");
printf("** Program to find Root of the equation via regula-falsi Method
**");
printf("\n\n\n");
while(1)
{

if(func(i)==0)
{
printf("\nExact Root: %d",i);
break;
}

if(func(i)*func(i+1)<0)
{
pos++; //CONDITION SATISFIED .. OUT OF
LOOP
break;
}
i++;
}
if(pos)
{
x0=i;
x1=i+1;
}
else
printf("\nSorry Equation do not have Positive Root\
n");

if(pos)
{

printf("\nCalculation Table\n");

printf("Root lies between: %.0f and %.0f",x0,x1);

fx1=func(x1);
fx0=func(x0);
x2=(float)(x1-(fx1*(x1-x0)/(fx1-fx0)));
fx2=func(x2);
printf("\n i\t\tx0\t\tx1\t\tx2\t\tfx2 ");

for(i=1;;i++)
{
printf("\n %d\t\t%.4f\t\t%.4f\t\t%.4f\t\t
%.4f",i,x0,x1,x2,fx2);

if( (float)fabs(x0-x2)<0.0001f ||(float)fabs(x2-


x1)<0.0001f )
break;

if(fx2<0)
{x0=x2;
fx0=fx2;}

else{
x1=x2;
fx1=fx2; }
x2=(float)(x1-(fx1*(x1-x0)/(fx1-fx0)));
fx2=func(x2);
}

}
printf("\n Root of the equation is %.4f",x2);

You might also like