'***************************************************** '* Program to demonstrate Newton's 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 (x+1)^5) * '* * '* Input the initial guess: * '* * '* X0 = 0 * '* * '* Convergence factor: 1e-6 * '* * '* Maximum number of iterations: 30 * '* * '* * '* The calculated zero is X = -.9984525574007144 * '* * '* The associated Y value is Y = 8.872622688643E-015 * '* * '* The number of steps was: 30 * '***************************************************** defint i-n defdbl a-h,o-z cls print print " Input the initial guess:" print input " X0 = ",x0 print input " Convergence factor : ",e print input " Maximum number of iterations: ",m gosub 2000 'Call Newton routine 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 'derivative y1 = 5+20*x+30*x*x+20*x^3+5*x^4 return '************************************* '*********************************************** '* Newton's method subroutine * '* ------------------------------------------- * '* This routine calculates the zeros of a * '* function Y(x) by Newton's method. * '* The routine requires an initial guess, x0, * '* and a convergence factor, e. Also required * '* is alimit on the number of iterations, m. * '*********************************************** 2000 n=0 'Get y and y1 2100 x=x0 : gosub 1000 'Update estimate x0=x0-y/y1 n=n+1 if n>=m then return if ABS(y/y1)>e then goto 2100 x=x0 return 'End of file Newton1.bas