Educational Blog: SECANT Method in C

Tuesday, 18 November 2014

SECANT Method in C

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define ESP 0.0001
#define F(x) (x)*(x) - 4*(x) - 10
void main()
{
  float x1,x2,x3,f1,f2,t;
  clrscr();
  printf("\nEnter the value of x1: ");
  scanf("%f",&x1);
  printf("\nEnter the value of x2: ");
  scanf("%f",&x2);
  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\nApp.root = %f",x3);
getch();
}


---------------------- OUT PUT -------------------------


Enter the value of x1: 4

Enter the value of x2: 2

___________________________________________________________

    x1        x2         x3         f(x1)       f(x2)
___________________________________________________________

4.000000   2.000000   9.000000   -10.000000   -14.000000
2.000000   9.000000   4.000000   -14.000000    35.000000
9.000000   4.000000   5.111111    35.000000   -10.000000
4.000000   5.111111   5.956522   -10.000000   -4.320987
5.111111   5.956522   5.722488   -4.320987     1.654063
5.956522   5.722488   5.741121    1.654063    -0.143084
5.722488   5.741121   5.741659   -0.143084    -0.004015
5.741121   5.741659   5.741657   -0.004015     0.000010
___________________________________________________________


App.root = 5.741657

No comments:

Post a Comment