-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbisectionMethod.c
More file actions
50 lines (45 loc) · 754 Bytes
/
Copy pathbisectionMethod.c
File metadata and controls
50 lines (45 loc) · 754 Bytes
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
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MAXN 200
#define F(X) ((X*X*X) + (4*X*X)-10)
int main()
{
double a,b,p,tol,fa,fb,fp;
a=1.25,b=1.50,tol=10e-6;
int i=0;
fa=F(a),fb=F(b);
if(fa*fb>=0)
{
puts("no root");
return EXIT_FAILURE;
}
puts("a b c f(a) f(b) f(c)");
while(i<MAXN)
{
i++;
p=a+(b-a)/2;
fp=F(p);
printf("%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",a,b,p,fa,fb,fp);
if(fabs(fp)<=tol)
{
printf("the approaximate solution is = %lf\n",p);
printf("iteration number =%d\n",i);
return EXIT_SUCCESS;
}
else
{
if(fp*fa<0)
{
b=p;
fb=fp;
}
else
{
a=p;
fa=fp;
}
}
}
printf("iteration overflow . . . ");
}