This method is also called "Regula Falsi Method".
ALGORITHM:
ALGORITHM:
- START.
- Input initial values x1 and x2.
- Compute f1=f(x1) and f2=f(x2).
- If f1*f2>0, x1 and x2 do not bracket the the root and go to step 2.
- Compute x=x1-(f(x1)*(x2-x1))/((f(x2)-f(x1)) and fx=f(x).
- If f1*fx<0, set x2=x and set f2=fx, Else x1=x and f1=fx.
- If |(x2-x1)/x|<accuracy, then root = x, Else go to step 5.
- STOP
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return(x*x*x-2*x-5);
}
void main()
{
float x1,x2,x=0,tmp,eps=0.001;
int iteration=0;
do
{
printf("Enter Initial guess:");
scanf("%f %f", &x1,&x2);
if(f(x1)*f(x2)>0)
printf("\nInvalid Guess.... Type the guess again...\n");
}while(f(x1)*f(x2)>0);
do
{
iteration++;
tmp=x;
x=x1-(f(x1)*(x2-x1))/(f(x2)-f(x1));
if(f(x1)*f(x)<0)
x2=x;
else
x1=x;
}while(fabs(tmp-x)>eps);
printf("\n Result = %.4f Iteration=%d",x,iteration);
getch();
No comments:
Post a Comment