This method is also called "Chord Method".
ALGORITHM:
- START.
- Input initial values x1 and x2.
- Compute xi+1 = xi - (f(xi)(xi-xi-1))/(f(xi)(xi-xi-1)) for i=2,3,4,.....
- Next, xi-1 is replaced by xi and xi by xi+1 in the iteration process.
- If |(xi - xi-1)/ xi+1| < accuracy, then root = xi+1 , Else go to step 3.
- STOP.
#include<stdio.h>
#include<math.h>
#include<conio.h>
float f(float x)
{
return(x*exp(x)-cos(x)); /* xex = cos x */
}
void main()
{
float x1,x2,x=0,eps=0.0001,tmp;
int iteration=0;
printf("\nEnter two guesses:");
scanf("%f %f",&x1,&x2);
do
{
tmp=x;
x=(f(x2)*x1-f(x1)*x2)/(f(x2)-f(x1));
iteration++;
x1=x2;
x2=x;
}while(fabs(tmp-x)>eps);
printf("\nRoot=%.5f \t Iteration=%d",x,iteration);
getch();
No comments:
Post a Comment