Secant-Method
Secant-Method
1. Start
6. Print root as x2
7. Stop
C Source Code: Secant Method
#include<stdio.h>
#include<math.h>
void main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 1, N;
clrscr();
/* Inputs */
printf("\nEnter initial guesses:\n");
scanf("%f%f", &x0, &x1);
printf("Enter tolerable error:\n");
scanf("%f", &e);
printf("Enter maximum iteration:\n");
scanf("%d", &N);
if(f0 == f1)
{
printf("Mathematical Error.");
exit(0);
}
x2 = (x0*f1- x1*f0)/(f1-f0);
f2 = f(x2);
printf("%d\t\t%f\t%f\t%f\t%f\n",step,x0,x1,x2, f2);
x0 = x1;
f0 = f1;
x1 = x2;
f1 = f2;
step = step + 1;
if(step > N)
{
printf("Not Convergent.");
exit(0);
}
}while(fabs(f2)>e);
Step x0 x1 x2 f(x2)
1 1.000000 2.000000 2.200000 1.248001
2 2.000000 2.200000 2.088968 -0.062124
3 2.200000 2.088968 2.094233 -0.003554
4 2.088968 2.094233 2.094553 0.000012
5 2.094233 2.094553 2.094552 0.000001