restart:# ROMBERG ALGORITHM 4.2# # To approximate I = integral ( f(x) dx ) from a to b:## INPUT: endpoints a, b: integer n.## OUTPUT: an array R. ( R(2,n) is the approximation to I. )## R is computed by rows: only 2 rows saved in storage print(`This is Romberg Integration. \134n`):print(`Input the function F(x) in terms of x `):print(`For example: cos(x) `):F := scanf(`%a`)[1]:print(`F(x) = `):print(F):F := unapply(F,x):OK := FALSE:while OK = FALSE doprint(`Input lower limit of integration and `):print(`upper limit of integration `):print(`separated by a blank `):A := scanf(`%f`)[1]:B := scanf(`%f`)[1]:print(`a = `):print(A):print(`b = `):print(B):if A > B thenprint(`Lower limit must be less than upper limit `):elseOK := TRUE:fi:od: OK := FALSE:while OK = FALSE doprint(`Input number of rows - no decimal point. `):N := scanf(`%d`)[1]:print(`N = `):print(N):if N > 0 thenOK := TRUE:elseprint(`Number must be a positive integer `):fi:od:if OK = TRUE then# STEP 1H := B-A:R[0,0] := (F(A)+F(B))/2*H:# STEP 2OUP := default:fprintf(OUP,`Romberg Integration \134n`):fprintf(OUP,`Initial Data:\134n`);fprintf(OUP,`Limits of integration = [%12.8f, %12.8f]\134n`, A, B);fprintf(OUP,`Number of rows = %3d\134n`, N);fprintf(OUP,`\134nRomberg Integration Table:\134n`);fprintf(OUP,`\134n%12.8f\134n\134n`, R[0,0]);# STEP 3for I1 from 2 to N do# STEP 4# approximation from Trapezoidal methodSUM := 0:M := 2^(I1-2):for K from 1 to M doSUM := SUM+F(A+(K-0.5)*H):od:R[1,0] := (R[0,0]+H*SUM)/2:# STEP 5# extrapolationfor J from 2 to I1 doL := 2^(2*(J-1)):R[1,J-1] := R[1,J-2]+(R[1,J-2]-R[0,J-2])/(L-1):od:# STEP 6for K from 1 to I1 dofprintf(OUP,` %11.8f`,R[1,K-1]):od:fprintf(OUP,`\134n\134n`);# STEP 7H := H/2:# since only two rows are kept in storage, this step is # to prepare for the next row# update row 1 of R# STEP 8for J from 1 to I1 doR[0,J-1] := R[1,J-1]:od:od:fi:JSFH