-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbisection method.c
More file actions
56 lines (47 loc) · 1.62 KB
/
bisection method.c
File metadata and controls
56 lines (47 loc) · 1.62 KB
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
54
55
56
#include <stdio.h>
#include <math.h>
void bisect (float *mid_pt, float int_st, float int_end, int *iter_cnt);
double get_fun (double res);
int main ()
{
int iter_cnt, mx_iter_cnt;
float mid_pt, int_st, int_end, err_all, root;
printf (" \n Enter the first starting point: ");
scanf (" %f", &int_st);
printf (" \n Enter the second ending point: ");
scanf (" %f", &int_end);
printf (" \n Enter the maximum iteration to be allowed: ");
scanf (" %d", &mx_iter_cnt);
printf (" Input the no. of allowed error point: ");
scanf (" %f", &err_all);
bisect (&mid_pt, int_st, int_end, &iter_cnt);
for (iter_cnt = 0; iter_cnt < mx_iter_cnt; mid_pt = root)
{
if ( get_fun (int_st) * get_fun (mid_pt) < 0)
{
int_end = mid_pt;
}
else
{
int_st = mid_pt;
}
bisect ( &root, int_st, int_end, &iter_cnt);
if ( fabs (root - mid_pt) < err_all)
{
printf (" \n The approximation root is: %f \n", root);
return 0;
}
}
printf (" The iterations are insufficient: ");
return 0;
}
void bisect (float *mid_pt, float int_st, float int_end, int *iter_cnt)
{
*mid_pt = (int_st + int_end) / 2; // get the middle value
++(*iter_cnt); // increment the iteration value
printf ( " Iteration \t %d: \t %f \n", *iter_cnt, *mid_pt);
}
double get_fun (double res)
{
return (res * res * res - 4 * res - 9);
}