-
Notifications
You must be signed in to change notification settings - Fork 0
/
Secant_Method
53 lines (41 loc) · 884 Bytes
/
Secant_Method
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#define f(x) x*x*x - 2*x - 5
void main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 1, N;
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);
printf("\nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n");
do
{
f0 = f(x0);
f1 = f(x1);
if(f0 == f1)
{
printf("Mathematical Error.");
exit(0);
}
x2 = x1 - (x1 - x0) * f1/(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);
printf("\nRoot is: %f", x2);
}