Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
bisection_method.c File Reference

In mathematics, the Bisection Method is a root-finding method that applies to any continuous function for which one knows two values with opposite signs. More...

#include <assert.h>
#include <math.h>
#include <stdio.h>
Include dependency graph for bisection_method.c:

Macros

#define EPSILON   0.0001
 for assert
 
#define NMAX   50
 

Functions

double sign (double a, double b)
 Function to check if two input values have the same sign (the property of being positive or negative)
 
double func (double x)
 Continuous function for which we want to find the root.
 
double bisection (double x_left, double x_right, double tolerance)
 Root-finding method for a continuous function given two values with opposite signs.
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

In mathematics, the Bisection Method is a root-finding method that applies to any continuous function for which one knows two values with opposite signs.

The method consists of repeatedly bisecting the interval defined by the two values and then selecting the subinterval in which the function changes sign, and therefore must contain a root. It is a very simple and robust method, but it is also relatively slow. Because of this, it is often used to obtain a rough approximation to a solution which is then used as a starting point for more rapidly converging methods.

Author
Aybars Nazlica

Macro Definition Documentation

◆ EPSILON

#define EPSILON   0.0001

for assert

for fabs for IO operations

Function Documentation

◆ bisection()

double bisection ( double  x_left,
double  x_right,
double  tolerance 
)

Root-finding method for a continuous function given two values with opposite signs.

Parameters
x_leftLower endpoint value of the interval
x_rightUpper endpoint value of the interval
toleranceError threshold
Returns
root of the function if bisection method succeed within the maximum number of iterations
-1 if bisection method fails
59{
60 int n = 1; // step counter
61 double middle; // midpoint
62
63 while (n <= NMAX)
64 {
65 middle = (x_left + x_right) / 2; // bisect the interval
66 double error = middle - x_left;
67
68 if (fabs(func(middle)) < EPSILON || error < tolerance)
69 {
70 return middle;
71 }
72
73 if (sign(func(middle), func(x_left)) > 0.0)
74 {
75 x_left = middle; // new lower endpoint
76 }
77 else
78 {
79 x_right = middle; // new upper endpoint
80 }
81
82 n++; // increase step counter
83 }
84 return -1; // method failed (maximum number of steps exceeded)
85}
#define EPSILON
for assert
Definition bisection_method.c:21
double sign(double a, double b)
Function to check if two input values have the same sign (the property of being positive or negative)
Definition bisection_method.c:32
double func(double x)
Continuous function for which we want to find the root.
Definition bisection_method.c:43
void error()
Utility function used to print an error message to stderr.
Definition remote_command_exec_udp_client.c:46
Here is the call graph for this function:

◆ func()

double func ( double  x)

Continuous function for which we want to find the root.

Parameters
xReal input variable
Returns
The evaluation result of the function using the input value
44{
45 return x * x * x + 2.0 * x - 10.0; // f(x) = x**3 + 2x - 10
46}

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
108{
109 test(); // run self-test implementations
110 return 0;
111}
static void test()
Self-test implementations.
Definition bisection_method.c:91
Here is the call graph for this function:

◆ sign()

double sign ( double  a,
double  b 
)

Function to check if two input values have the same sign (the property of being positive or negative)

Parameters
aInput value
bInput value
Returns
1.0 if the input values have the same sign,
-1.0 if the input values have different signs
33{
34 return (a > 0 && b > 0) + (a < 0 && b < 0) - (a > 0 && b < 0) -
35 (a < 0 && b > 0);
36}

◆ test()

static void test ( void  )
static

Self-test implementations.

Returns
void
92{
93 /* Compares root value that is found by the bisection method within a given
94 * floating point error*/
95 assert(fabs(bisection(1.0, 2.0, 0.0001) - 1.847473) <
96 EPSILON); // the algorithm works as expected
97 assert(fabs(bisection(100.0, 250.0, 0.0001) - 249.999928) <
98 EPSILON); // the algorithm works as expected
99
100 printf("All tests have successfully passed!\n");
101}
double bisection(double x_left, double x_right, double tolerance)
Root-finding method for a continuous function given two values with opposite signs.
Definition bisection_method.c:58
Here is the call graph for this function: