/**************************************************** * Program to demonstrate Secant method subroutine * * ------------------------------------------------- * * Reference: BASIC Scientific Subroutines, Vol. II * * By F.R. Ruckdeschel, BYTE/McGRAWW-HILL, 1981 [1]. * * * * C++ version by J-P Moreau, Paris. * * (www.jpmoreau.fr) * * ------------------------------------------------- * * SAMPLE RUN: * * * * ( Example: Find a real root of f(x)=(x+1)^5 ) * * * * Input the initial guesses: * * * * X0 = -2 * * X1 = 0 * * * * Convergence factor: 1e-6 * * Maximum number of iterations: 50 * * * * * * The calculated zero is X = -1.000000 * * * * The associated Y value is Y = 0.000000 * * * * The number of steps was: 3 * * * ****************************************************/ #include #include double e,x0,x1; int m,n; /************************************************** Function subroutine */ double Y(double x) { return (1+5*x+10*x*x+10*x*x*x+5*x*x*x*x+x*x*x*x*x); } /**************************************************/ /********************************************** * Secant method subroutine * * ------------------------------------------- * * This subroutine calculates the zeroes of a * * function Y(x) using the secant method. * * Two initial guesses are required, x0 and x1,* * and a convergence criterion, e. Also requi- * * red is the maximum number of iterations, m. * * The root is returned in x, the number of * * iterations performed in n. * **********************************************/ void Secant() { double x,y0,y1; n=0; // Start iteration e100: y0=Y(x0); y1=Y(x1); // Calculate new estimate // Guard against y1-y0 too small if (fabs(y1-y0)<0.001) y1 += 0.001; x=(x0*y1-x1*y0)/(y1-y0); n++; // Test for convergence if (n>=m) return; if (fabs(x1-x0)