'***************************************************** '* Program to demonstrate Secant Method Subroutine * '* ------------------------------------------------- * '* Reference: BASIC Scientific Subroutines, Vol. II * '* By F.R. Ruckdeschel, BYTE/McGRAWW-HILL, 1981 [1]. * '* ------------------------------------------------- * '* 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 * '* * '* The associated Y value is Y = 0 * '* * '* The number of steps was: 3 * '* * '***************************************************** defint i-n defdbl a-h,o-z cls print print " Input the initial guesses:" print input " X0 = ",x0 input " X1 = ",x1 print input " Convergence factor : ",e input " Maximum number of iterations: ",m gosub 2000 'Call secant routine print print print " The calculated zero is X = "; x print print " The associated Y value is Y = "; y print print " The number of steps was: "; n print end '************************************* 1000 ' Function subroutine y = 1+5*x+10*x*x+10*x^3+5*x^4+x^5 return '************************************* '*********************************************** '* Secant method subroutine * '* ------------------------------------------- * '* This subroutine calculates the zeroes of a * '* function Y(x) using the secant method. * '* Two initial guess 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. * '*********************************************** 2000 n=0 'Start iteration 2100 x=x0 : gosub 1000 y0=y : x=x1 'Get next point gosub 1000 y1=y 'Calculate new estimate 'Guard against y1-y0 too small if ABS(y1-y0)<0.001 then y1=y1+0.001 x=(x0*y1-x1*y0)/(y1-y0) n=n+1 'Test for convergence if n>=m then return if ABS(x1-x0)