C program for SECANT METHOD.

Share
#include <stdio.h>
#include<conio.h>
#include<math.h>
#define e 0.001
#define f(x) x*x*x -4*x +1

int main()
{
	 float x0, x1, x2, f0, f1, f2;
	 int i= 0;
	 printf("\nEnter two initial guesses:\n");
	 scanf("%f,%f", &x0, &x1);

	 printf("\ni\t\tx0\t\tx1\t\tx2\t\tf(x2)\n");
	 do
	 {
		  f0 = f(x0);
	 	  f1 = f(x1);
		  x2 = ((x0*f1)-(x1*f0))/(f1-f0);
		  f2 = f(x2);
		
		  printf("%d\t\t%f\t%f\t%f\t%f\n",i, x0, x1, x2, f2);
		  f0=f1;
		  f1=f2;
		  x0=x1;
		  x1=x2;
		i++;
	 }while(fabs(x2)>e);
	 printf("\nRoot is: %f", x2);
	 getch();
}

OUTPUT

Share
Sudeep Mishra

Sudeep Mishra

Healing

%d bloggers like this: