'**************************************************** '* Root of a non-linear Equation By Lagrange * '* ------------------------------------------------ * '* REF.: "Méthode de calcul numérique - Programmes * '* en Basic By Claude Nowakowski, PS1 1981". * '* * '* ------------------------------------------------ * '* SAMPLE RUN: * '* * '* Find a root of equation: X - E SIN X * '* * '* Input Starting, Ending X: 1, 10 * '* Input max. error: 1E-5 * '* * '* Root = 2.199122862796527 * '* Iterations: 7 * '* Y = -5.409157737747833D-07 * '* * '**************************************************** DEFINT I-N DEFDBL A-H, O-Z 'Program Lagrange PRINT INPUT " Input Starting, Ending X: ", X1, X2 INPUT " Input max.error: ", E PRINT MAXITER = 50 Y1 = FUNC(X1) Y2 = FUNC(X2) IF Y1 * Y2 > 0 THEN PRINT " Bad choice for X1, X2!" END END IF IF ABS(Y1 * Y2) < 1E-10 THEN PRINT " X1 or X2 is a root!" END END IF FOR K = 1 TO MAXITER XB = X2 - (X2 - X1) * Y2 / (Y2 - Y1) IF ABS(X1 - XB) < E THEN GOTO 100 'normal exit YB = FUNC(XB) IF Y1 * YB < 0 THEN X2 = XB: Y2 = YB ELSE X1 = XB: Y1 = YB END IF NEXT K PRINT " No root found!" END 100 PRINT " Root = "; XB PRINT " Iterations: "; K PRINT " Y = "; FUNC(XB) PRINT END 'User-defined non-linear function FUNCTION FUNC (X) FUNC = X - 2.718281828 * SIN(X) END FUNCTION 'End of file Lagrange1.bas