False Position Method or Regula Falsi Method Using c programming

False Position Method or Regula Falsi Method Using c programming
False Position Method or Regula Falsi Method Using c programming



#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return (x*x*x-5*x+1);
}
int main()
{
clrscr();
int itr=0, maxitr;
float x1,x2,x3,x4,aerr;
printf("nProgram to find Root of an Equation by Regula falsi Methodnn");
printf("nEnter value of x0, x1, allowed error and maximum iterationn");
scanf("%f %f %f %d", &x1, &x2, &aerr, &maxitr);
x3=((x1*f(x2))-(x2*f(x1)))/(f(x2)-f(x1));
printf("nnIn iteration %d, Value of x3 =t%f",itr+1,x3);
do
{
if (f(x1)*f(x3)<0)
x2=x3;
else
x1=x3;
x4=x3;
x3=((x1*f(x2))-(x2*f(x1)))/(f(x2)-f(x1));
itr++;
printf("nIn iteration %d Value of x=t%f", itr+1,x3);
if (fabs(x4-x3)<aerr)
{
printf("nnAfter %d iteration, Root= %f", itr,x4);
getch();
return 0;
}
}
while (itr<maxitr);
printf("nnSolution does not converge Iteration not sufficient");
getch();
return 1;
}

Output: 

Program to find Root of an Equation by Regula falsi Method                                                                       
                                                                                                                                 
                                                                                                                                 
Enter value of x0, x1, allowed error and maximum iteration                                                                       
1 2 3 4                                                                                                                          
                                                                                                                                 
                                                                                                                                 
In iteration 1, Value of x3 =   2.500000                                                                                         
In iteration 2 Value of x=      1.631579                                                                                         
                                                                                                                                 
After 1 iteration, Root= 2.500000 
Post a Comment