Tuesday 20 September 2011

Regula-Falsi Method


/*This program finds roots by Regula-Falsi method OR Method of False position */

#include<stdio.h>
#include<math.h>
#include<conio.h>

float root(float x)
{
float f;
f=x*x*x-x-1;
return(f);
}

void main()
{
int i,n;
float a,b,x,e,t,p;
clrscr();
printf("Type a and b\n");
scanf("%f %f",&a,&b);
printf("Type maximum number of interval n\n");
scanf("%d",&n);
printf("Type the value of e\n");
scanf("%f",&e);
p=root(a)*root(b);
if(p>0)
printf("Root does not lies between a and b");
else
{
i=1;
t=1;
while(t>e && i<=n)
{
x=(a*root(b)-b*root(a))/(root(b)-root(a));
if(root(x)*root(a)>0)
a=x;
else
b=x;
t=fabs(a-b);
if(t>fabs(root(x)))
t=fabs(root(x));
i++;
}
if(i>n)
printf("Iteration does not converge");
else
{
 printf("Approximate root=%8.4f\n",x);
 printf("Number of Iteration=%d\n",i);
}
}
getch();
}

1 comment:

  1. this program is so easy for finding the roots of the equation and very helpful for us.

    ReplyDelete