'***************************************************** '* Program to demonstrate Bisection subroutine * '* ------------------------------------------------- * '* Reference: BASIC Scientific Subroutines, Vol. II * '* By F.R. Ruckdeschel, BYTE/McGRAWW-HILL, 1981 [1]. * '* ------------------------------------------------- * '* Example: Find a real root of f(x)=(x-2)(x+1)(x+10)* '* * '* Sample run: * '* * '* What is the initial range (X0,X1): * '* X0 = 0 * '* X1 = 10 * '* Convergence criterion: 1e-6 * '* * '* The calculated zero is X = 1.99999988079071 * '* The associated Y value is Y = -4.2915342106E-006 * '* The number of steps was: 24 * '* * '***************************************************** defint i-n defdbl a-h,o-z cls print print " What is the initial range (X0,X1):" print input " X0 = ",x0 input " X1 = ",x1 print input " Convergence criterion: ",e gosub 2000 'Call bisection 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: ";m print end '************************************* 1000 ' Function subroutine y = (x-2)*(x+1)*(x+10) return '************************************* '*********************************************** '* Bisection method subroutine * '* ------------------------------------------- * '* This routine iteratively seeks the zero of * '* function Y(x) using the method of interval * '* halving until the interval is less than e * '* in width. It is assumed that the function * '* Y(x) is available from a function routine. * '* ------------------------------------------- * '* Input values: range (x0,x1), and e. * '* Output values: root x, Y(x) and number of * '* steps m. * '*********************************************** 2000 m=0 2100 x=x0 gosub 1000 'Call function routine y0=y : x=x1 gosub 1000 'Call function routine x=(x0+x1)/2 gosub 1000 'Call function routine m=m+1 if y*y0=0 then return if y*y0<0 then x1=x if y*y0>0 then x0=x if ABS(x1-x0)>e then goto 2100 return 'End of file Bisect.bas