Write a program to find the roots of f(x) = 0 using Secant method.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
 float a,b,x0,x1,x2;
 int i;
 float f(float);
 clrscr();
 printf("enter the value of a & b ");
 scanf("%f%f",&a,&b);
 x0=a;
 x1=b;
 printf("\na=%f \n b=%f",x0,x1);
 x2=x1-(((x1-x0)/(f(x1)-f(x0)))*f(x1));
 printf("\n\nx2=%f",x2);
 for(i=0;x1!=x2;i++)
 {
  printf("\n(%d)%f",i,x2);
  x0=x1;
  x1=x2;
  x2=x1-(((x1-x0)/(f(x1)-f(x0)))*f(x1));
  }
  printf("\n the roots is %f converges is %d approximately",x2,i);
  getch();
  }
  float f(float x)
  {
   return((pow(x,3))+(-5*x+1));
  }

No comments:

Post a Comment