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

Secant Method implementation. More...

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

Macros

#define TOLERANCE   0.0001
 for assert
 
#define NMAX   100
 

Functions

double func (double x)
 Continuous function for which we want to find the root.
 
double secant_method (double x0, double x1, double tolerance)
 Root-finding method for a continuous function given two points.
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Secant Method implementation.

Find a continuous function's root by using a succession of roots of secant lines to approximate it, starting from the given points' secant line.

Author
Samuel Pires

Macro Definition Documentation

◆ TOLERANCE

#define TOLERANCE   0.0001

for assert

for fabs for io operations

Function Documentation

◆ 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
22{
23 return x * x - 3.; // x^2 = 3 - solution is sqrt(3)
24}

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
77{
78 test(); // run self-test implementations
79 return 0;
80}
static void test()
Self-test implementations.
Definition secant_method.c:61
Here is the call graph for this function:

◆ secant_method()

double secant_method ( double  x0,
double  x1,
double  tolerance 
)

Root-finding method for a continuous function given two points.

Parameters
x0One of the starting secant points
x1One of the starting secant points
toleranceDetermines how accurate the returned value is. The returned value will be within tolerance of the actual root
Returns
root of the function if secant method succeed within the maximum number of iterations
-1 if secant method fails
37{
38 int n = 1; // step counter
39
40 while (n++ < NMAX)
41 {
42 // calculate secant line root
43 double x2 = x1 - func(x1) * (x1 - x0) / (func(x1) - func(x0));
44
45 // update values
46 x0 = x1;
47 x1 = x2;
48
49 // return value if it meets tolerance
50 if (fabs(x1 - x0) < tolerance)
51 return x2;
52 }
53
54 return -1; // method failed (maximum number of steps exceeded)
55}
double func(double x)
Continuous function for which we want to find the root.
Definition secant_method.c:21
Here is the call graph for this function:

◆ test()

static void test ( void  )
static

Self-test implementations.

Returns
void
62{
63 // compares root values found by the secant method within the tolerance
64 assert(secant_method(0.2, 0.5, TOLERANCE) - sqrt(3) < TOLERANCE);
65 assert(fabs(secant_method(-2, -5, TOLERANCE)) - sqrt(3) < TOLERANCE);
66 assert(secant_method(-3, 2, TOLERANCE) - sqrt(3) < TOLERANCE);
67 assert(fabs(secant_method(1, -1.5, TOLERANCE)) - sqrt(3) < TOLERANCE);
68
69 printf("All tests have successfully passed!\n");
70}
double secant_method(double x0, double x1, double tolerance)
Root-finding method for a continuous function given two points.
Definition secant_method.c:36
#define TOLERANCE
for assert
Definition secant_method.c:13
Here is the call graph for this function: