-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBisection_Method.c
More file actions
73 lines (46 loc) · 1.98 KB
/
Bisection_Method.c
File metadata and controls
73 lines (46 loc) · 1.98 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h> //standart input output library for printf function
#include <math.h> //math library for pow function
double f( double x ); //function for function's value
double precision( double point, double older_point ); //function for relative error
int main(){
double a_1 = 1; //initializing variables
double b_1 = 2;
double prep = 1;
double point=1;
double older_point=1;
int n = 0;
printf( " n a_n b_n point point val relative error\n" );
printf( "------------------------------------------------------------------------------------------------\n" );
printf( " %d a_n = %lf || b_n = %lf || point = %lf || point val = %lf\n", n, a_1, b_1 ,point , f(point) );
while ( prep > pow( 10, -3 )){ //checking relative error condition
printf( "------------------------------------------------------------------------------------------------\n" );
n++; //recursion number
older_point = point; //assignment of point to older point for relative error
point = (b_1 - a_1 ) / 2 + a_1; //middle point
if( f( point ) == 0 ) //checking if root is found
{
printf( "Root is: %lf\n", point );
}
if ( f( point ) * f( a_1 ) > 0 ){ //checking bisection conditions
a_1 = point;
}
if ( ! (f( point )*f( a_1 ) > 0)) {
b_1 = point;
}
prep = precision( point, older_point );
printf( " %d a_n = %lf || b_n = %lf || point = %lf || point val = %lf || %lf\n",n, a_1, b_1 ,point , f(point), prep );
}
printf( "------------------------------------------------------------------------------------------------\n" );
return 0;
}
double f( double x ){
double y;
y = pow( x, 3 ) + 4 * pow( x, 2 ) - 10;
return y;
}
double precision( double point, double older_point ){
if( ( ( ( point - older_point ) / older_point ) < 0 ) ){
return - ( ( point - older_point ) / older_point ); }
else{
return ( point - older_point ) / older_point; }
}