Secant method
1. The method:
This method requires guessing the two first values close to the root x0
and x1 for the solution of the equation f(x) = 0. The secant from the point
(x0, f(x0)) to the point (x1, f(x1)) cross
the x axis at the point (x2, 0)). From this x-intercept, we get the point
(x2, f(x2)).
The second iteration consists of drawing the secant from the point (x1, f(x1))
to the point (x2, f(x2)). The related x-intercept of this secant is x3.
From this, we get the point (x3, f(x3)). The third iteration consists of
drawing the secant from the point (x2, f(x2)) to the point
(x3, f(x3)). The related x-intercept of this secant is x4,
... and so on until we get an x-intercept closer to the root of the function f(x); that is
a sufficiently high level of precision corresponding to a small difference between xn and xn-1.
The secant related to the segment 0 and x1, can be found as follow:
y ax + b
f(x0) = a x0 + b (1)
f(x1) = a x1 + b (2)
Subtracting (1) from (2), yields:
f(x1) - f(x0) = a (x1 - x0),
then: a = (f(x1) - f(x0))/(x1 - x0)
Subtracting (1) from 2 x (2), yields:
2 f(x1) - f(x0) = a (2 x1 - x0) + b
then:
b = 2 f(x1) - f(x0) - a (2 x1 - x0)
Substituting the expression of "a" yields:
b = 2 f(x1) - f(x0) - (f(x1) - f(x0))
- x1 (f(x1) - f(x0))/(x1 - x0)
= f(x1) - x1 (f(x1) - f(x0))/(x1 - x0)
Then:
y ax + b = x (f(x1) - f(x0))/(x1 - x0) +
f(x1) - x1 (f(x1) - f(x0))/(x1 - x0) ) =
= (x - x1) (f(x1) - f(x0))/(x1 - x0) +
f(x1)
For y = 0, we have x2:
x2= - f(x1) (x1 - x0)/ (f(x1) - f(x0)) + x1
The next will have the following expression:
x3= - f(x2) (x2 - x1)/ (f(x2) - f(x1)) + x2
...
The nth x-intercept is:
xn= xn-1 - f(xn-1) (xn-1 - xn-2)/ (f(xn-1) - f(xn-2))
2. Example in clanguage
#include
#include
#include
double g = 9.81;
float y = 500, to, t1, z = 0.05;
// First function --------
float f(float t)
{
return y - (g/2)*t*t;
}
typedef float (*PFUN) (float);
float secant (PFUN f, float to, float t1){
float ss = 0;
float c[21];
c[0] = to ;
c[1] = t1 ;
int i=1,n = 20;
for (i =1; i<=n; i++){
c[i+1] = c[i] - (c[i] - c[i-1])*f(c[i])/(f(c[i]) - f(c[i-1]));
printf("%f \n", c[i+1]);
if (abs((c[i] - c[i-1])/c[i-1]) <= z/100.0 )
{
printf("\nThe solution is t = %f seconds.\n", c[i+1]);
ss= c[i];
i = n+1;
}
}
return ss;
}
// ---------------------The main -------------
int main()
{
printf("\n Input values for t0 and t1 (in seconds) : --> ");
scanf("%f%f", &to, &t1);
secant (&f,to,t1);
return 0;
}
The execution gives:
-------------------
secant_free_fall
10 11
Input values for t0 and t1 (in seconds) : --> 10.092229
The solution is t = 10.092229 seconds.
>Exit code: 0