Click below to view this site using some new reading style

** Classic | Flipcard | Magazine | Mosaic | Sidebar | Snapshot | Timeslide **

Secant Method Program in C

9:46 PM Posted In Edit This 0 Comments »

#include<stdio.h>
#include<conio.h>
#include<math.h>
float F(float x){return(x*x*x+2*x-2);}
int main()
{
  float x1,x2,x3,f1,f2,t,ESP;
  printf("Enter the value of x1 : ");
  scanf("%f",&x1);
  printf("\nEnter the value of x2 : ");
  scanf("%f",&x2);
  printf("\nEnter the allowed error : ");
  scanf("%f",&ESP);
  printf("\n_____________________________________________________________\n");
  printf("\n     x1\t     x2\t       x3\t  f(x1)\t      f(x2)");
  printf("\n_____________________________________________________________\n");
  do
  {
  f1=F(x1);
  f2=F(x2);
  x3=x2-((f2*(x2-x1))/(f2-f1));
  printf("\n%f   %f   %f   %f   %f",x1,x2,x3,f1,f2);
  x1=x2;
  x2=x3;
  if(f2<0)
    t=fabs(f2);
  else
    t=f2;
  }while(t>ESP);
printf("\n_____________________________________________________________\n");
printf("\n\nThe final root is = %f",x3);
getch();
}

0 comments: