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>
|
#define | EPSILON 0.0001 |
| for assert
|
|
#define | NMAX 50 |
|
|
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.
|
|
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
◆ EPSILON
for assert
for fabs for IO operations
◆ 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_left | Lower endpoint value of the interval |
x_right | Upper endpoint value of the interval |
tolerance | Error 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;
61 double middle;
62
63 while (n <= NMAX)
64 {
65 middle = (x_left + x_right) / 2;
66 double error = middle - x_left;
67
69 {
70 return middle;
71 }
72
74 {
75 x_left = middle;
76 }
77 else
78 {
79 x_right = middle;
80 }
81
82 n++;
83 }
84 return -1;
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
◆ func()
Continuous function for which we want to find the root.
- Parameters
-
- Returns
- The evaluation result of the function using the input value
44{
45 return x * x * x + 2.0 * x - 10.0;
46}
◆ main()
Main function.
- Returns
- 0 on exit
108{
110 return 0;
111}
static void test()
Self-test implementations.
Definition bisection_method.c:91
◆ 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
-
a | Input value |
b | Input 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
94
95 assert(fabs(
bisection(1.0, 2.0, 0.0001) - 1.847473) <
97 assert(fabs(
bisection(100.0, 250.0, 0.0001) - 249.999928) <
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