/*********************************************************** * Program to demonstrate the modified false position * * 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 (x+1)^5) * * * * Input the initial guesses (root must be between x0, x1): * * * * X0 = -2 * * X1 = 2 * * * * Convergence factor: .0001 * * * * Maximum number of iterations: 50 * * * * * * The calculated zero is X = -1.000803 * * * * The associated Y value is Y = 0.000000 * * * * The number of steps was: 47 * * * ***********************************************************/ #include #include double e,x,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; } //************************************************* /********************************************** * Modified false position subroutine * * ------------------------------------------- * * This subroutine calculates the zeroes of a * * function Y(x) uses Hamming's modification * * to speed convergence. * * Two initial guesses are required, x0 and x1,* * bracketting the root, and a convergence * * criterion, e. Also required is the maximum * * number of iterations, m. The root is retur- * * ned in x, the actual number of iterations * * used in n. * **********************************************/ void False_position() { double yy,y0,y1; n=0; // Make x0=m) return; if (fabs(x1-x)0) goto e300; x1=x; y1=yy; y0=y0/2.0; e300: x0=x; y0=yy; y1=y1/2.0; goto e200; } void main() { printf("\n Input the initial guesses:\n\n"); printf(" X0 = "); scanf("%lf",&x0); printf(" X1 = "); scanf("%lf",&x1); printf("\n Convergence factor: "); scanf("%lf",&e); printf("\n Maximum number of iterations: "); scanf("%d",&m); False_position(); // Call False position routine printf("\n\n The calculated zero is X = %f\n\n",x); printf(" The associated Y value is Y = %f\n\n",Y(x)); printf(" The number of steps was: %d\n\n",n); } // End of file Regula.cpp