217 20 11MB
English Pages XII; 398 [416] Year 1991
Published by the Press Syndicate of the University of Cambridge The Pitt Building. Trumpington Street. Cambridge CB2 1RP 40 West 20th Street. New York. NY 10011-4211. USA 10 Stamford Road. Oakleigh, Melbourne 3166, Australia Copyright © Cambridge University Press except for computer programs and procedures which are Copyright © Numerical Recipes Software 1985. 1991 First published 1991 Reprinted 1992. 1994. 1996. 1998
Typeset in T}3X
The computer routines in this book are available on machinereadable diskette for IBM PCs and compatible machines. There are also versions of this book and its software available in the FORTRAN. C, and Pascal programming languages. To purchase diskettes. use the order form at the back of the book, or write to Cambridge University Press, 110 Midland Avenue, Port Chester, New York 10573. Unlicensed transfer of Numerical Recipes programs from the above-mentioned IBM PC diskette to any other format, or to any computer except a single IBM PC or compatible for each diskette purchased, is strictly prohibited. Licenses for authorized transfers to other computers are available from Numerical Recipes Software, P.O. Box 243, Cambridge, MA 02238. Technical questions, corrections, and requests for information on other available formats should also be directed to this address.
Library of Congress Cataloging-in-Publication Data
Sprott, Julien C. Numerical recipes : routines and examples in BASIC / Julien C. Sprott. p. em. "Comparison manual to Numerical recipes: the art of scientific computing." "In association with Numerical Recipes Software." Includes index. ISBN 0-521-40689-7 paperback. - ISBN 0-521-40688-9 diskette. 1. Numerical analysis-Computer programs. 2. Science-Mathematics-Computer programs. 3. BASIC (Computer program language) I. Numerical Recipes Software (Finn) n. Numerical recipes. m. Title. QA297.S68 1991 90-26323 519.4'02085'5133-dc20 CIP ISBN 0-521-40689-7 paperback ISBN 0-521-40688-9 diskette
Transferred to digital printing 2002
CONTENTS Foreword by Numerical Recipes Software
vii
Disclaimer of Warranty
viii
Author's Preface
ix
Important Note on Dialects of BASIC
x
1. Preliminaries
1
2.
Linear Algebraic Equations
9
3.
Interpolation and Extrapolation
39
4.
Integration of Functions
60
5.
Evaluation of Functions
78
6.
Special Functions
92
7.
Random Numbers
137
8.
Sorting
169
9.
Root Finding and Sets of Equations
188
10.
Minimization and Maximization of Functions
214
11.
Eigensystems
.
250
12.
Fourier Methods
271
13.
Statistical Description of Data
297
14.
Modeling of Data
333
15. Ordinary Differential Equations
360
16.
Two-Point Boundary Value Problems
376
17.
Partial Differential Equations
390
Recipes Index
397
v
Numerical Recipes Routines and Examples in BASIC
198
IF lBS(E) >= TOLl AND ABS(FA) > lBS(FB) THEN S .. FB I FA Attempt inverse quadratic interpolation. IF 1 .. C THEN P .. 2•• XM • S Q = 1! - S ELSE Q .. Fl I FC R • FB I FC P • S • (2! • XM • Q • (Q - R) - (B - A) • (R - 1!» Q .. (Q - 1!) • (R - 1!) • (S - 1!) END IF Check whether in bounds. IF P > O! THEN Q .. -Q P .. 1BS(P) DUM .. 3! • XM • Q - ABS(TOLl • Q) IF DUM > ABS(E • Q) THEN DUM E. Q IF 2! • P < DUM THEN E .. D
Accept interpolation.
D .. P I Q ELSE D .. 1M E .. D
Interpolation failed, use bisection.
EID IF
ELSE
Bounds decreasing too slowly, use bisection.
D .. 1M E .. D
END IF 1 • B
Move last best guels to A.
Fl .. FB IF 1BS (D) > TOLl THEN Evaluate new trial B .. B + D ELSE B .. B + ABS(TOL1) • SGN(XM) END IF FB .. FUNC(B) NEXT ITER. PRINT "ZBRENT exceeding maximum iterations." ZBRENT .. B END Fl1NCTION
root.
A sample program using ZBRENT is the following: DECLARE FUNCTION FUNC!
(X!)
DECLARE SUB ZBRAK (DUM!, xi t , X2!, N!, XB1! (), XB2! (), NB!) DECLARE FUNCTION BESSJO! (X!) DECLARE FUNCTION ZBRENT! (DUM!, xi t , X2!, TOL!)
'PROGRAM D9R7 'Driver for routine ZBRENT CLS N -
100
NBMAX -
Xl
1!
X2
50!
20
DIM XBl (NBMAX), XB2 (NBMAX) NB ... NaMAX
202
Numerical Recipes Routines and Examples in BASIC
CALL ZBRAK(DUM, xi, X2, N, XB1 (), XB2 (), NB) PRINT "Roots of BESSJO:" PRINT PRINT" x F(x)" PRINT FOR I ... 1 TO NB XACC = .000001 * (XB1(I) + XB2(I» I 2! ROOT ... RTSAFE (DUM, XB1 (I), XB2 (I), XACC) PRINT "Root"; PRINT USING "111"; I; PRINT USING "1111111. 111111"; ROOT; PRINT" "; PRINT USING "1.1111'"''"''"''"'''; BESSJO (ROOT) NEXT I END FUNCTION FUNC (X) FUNC = BESSJO (X) END FUNCTION SUB FUNCD (X, FQ, FQ = BESSJO (X) DF = -BESSJ1 (X)
DF)
END SUB
Routine LAGUER finds the roots of a polynomial with complex coefficients. The polynomial of degree M is specified by M + 1 coefficients which, in sample program D9R10, are specified in a DATA statement and kept in array A. The polynomial in this case is
F(z) = z4 - (1 + 2i)z2
+ 2i
The four roots of this polynomial are z = 1.0, z = -1.0, z = 1 + i, and z = -(1 + i). LAGUER proceeds on the basis of a trial root, and attempts to converge to true roots. The root to which it converges depends on the trial value. The program tries a series of complex trial values along the line in the imaginary plane from -1.0 - i to 1.0 + i. The actual roots to which it converges are compared to all previously found values, and if different, are printed. Here is the recipe LAGUER:
DECLARE DECLARE DECLARE DECLARE DECLARE
FUNCTION FUNCTION FUNCTION FUNCTION FUNCTION
CABS! (Ai!, A2!) CDIV1! (Ai!, A2!, B1!, B2!) CDIV2! (Ai!, A2!, B1!, B2!) CSQR1! (X!, Y!) CSQR2! (X!, Y!)
FUNCTION CABS (Ai, A2) X = ABS(A1) Y = ABS(A2) IF I = O! THEN CABS = Y ELSEIF Y = O! THEN CABS I ELSEIF X > Y THEN CABS = X • SQR(1! + SQR(Y I X»
Magnitude of complex number.
204
Numerical Recipes Routines and Examples in BASIC
W = SQR(ABS(X» * SQR(.6 * (1! + SQR(l! + SQR(ABS(Y / X»») ELSE R = ABS(X / Y) W = SQR(ABS(Y» * SQR(.6 * (R + SQR(l! + SQR(R»» END IF IF X >= O! THEN U=W V = Y / (2! * U) ELSE IF Y >= O! THEN V WELSE V -W U = Y / (2! * V) END IF END IF CSQR2 = V END FUNCTION SUB LAGUER (A(), M, XC), EPS, POLISHy') Given the degree Mand the M+l complex coefficients A of the polynomial given EPS the desired fractional accuracy, and given a complex value X,
Er::l A(i)zi-l,
and this routine improves X by Laguerre's method until it converges to a root of the given polynomial. For normal use POLISHy' should be input as O. When POLISHy' is input as -1, the routine ignores EPS and instead attempts to improve X (assumed to be a good initial guess) to the achievable roundoff limit.
DIM ZERO (2) , B(2), D(2), F(2), G(2), H(2) DIM G2(2), SQ(2), GP(2), GM(2) , DX(2), Xl(2) ZERO(l) = O! ZERO(2) = O! EPSS = 6E-08 MAXIT = 100 DXOLD = CABS(X(l), X(2» FOR ITER = 1 TO MAXIT Loop over iterations up to allowed maximum. B(l) = A(l, M + 1) B(2) = A(2, M + 1) ERQ = CABS(B(1), B(2» D(1) ZERO(1) D(2) ZERO(2) F(l) ZERO(l) F(2) ZERO(2) ABX = CABS(X(l), X(2» FOR J = M TO 1 STEP -1 Efficient computation ofthe polynomial and DUM = 1(1) * F(1) - X(2) * F(2) + D(l) its first two derivatives. F(2) = 1(2) * F(l) + X(l) * F(2) + D(2) F(l) = DUM DUM = X(l) * D(l) - X(2) • D(2) + B(1) D(2) = X(2) * D(l) + X(l) • D(2) + B(2) D(1) = DUM DUM = 1(1) • B(l) - 1(2) • B(2) + A(1, J) B(2) = 1(2) • B(1) + 1(1) • B(2) + A(2, J) B(1) = DUM ERQ = CABS(B(1), B(2» + ABX • ERQ NEIT J ERQ = EPSS * ERQ Estimate of roundoff error in evaluating polynomial. IF CABSeBel), B(2»