A well-known root-finding algorithm is the secant method. Let f : [a,b] → R. Given two initial guesses x0 and x1, the algorithm defines the sequence of approximations (xn)n∈N0 as
xn :=xn−1−f(xn−1) xn−2−xn−1 for n≥2, f(xn−2) − f(xn−1)
i.e.,the approximationxn is the root of the line that connects the points(xn−2,f(xn−2))and(xn−1,f(xn−1)). Write a function double secant(double (*f)(double), double x0, double x1, double tau),which performs the above iteration until either |f(xn) − f(xn−1)| ≤ τ or
|f(xn)| ≤ τ and |xn − xn−1| ≤ (τ for |xn| ≤ τ
In the first case, print a warning to inform the user that the result is presumably wrong. The function returns xn as an approximation of a zero of f. Use assert to check that τ > 0. The function requires as input a suitable implementation double f(double x) of the object function f. Moreover, write a main program that reads x0, x1, and τ from the keyboard, calls the function, and prints to the screen the approximate zero xn and the function value f(xn).
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)