restart: # WAVE EQUATION FINITE-DIFFERENCE ALGORITHM 12.4 # # To approximate the solution to the wave equation: # subject to the boundary conditions # u(0,t) = u(l,t) = 0, 0 < t < T = max t # and the initial conditions # u(x,0) = F(x) and Du(x,0)/Dt = G(x), 0 <= x <= l: # # INPUT: endpoint l: maximum time T: constant ALPHA: integers m, N. # # OUTPUT: approximations W(I,J) to u(x(I),t(J)) for each I = 0, ..., m # and J=0,...,N. print(`This is the Finite-Difference Method for the Wave Equation.`): print(`Input the functions F(X) and G(X) in terms of x, separated by a space.`): print(`For example: sin(3.141592654*x) 0`): F := scanf(`%a`)[1]: G := scanf(`%a`)[1]: print(`F(x) = `):print(F):print(`G(x) = `):print(G): F := unapply(F,x): G := unapply(G,x): print(`The lefthand endpoint on the X-axis is 0.`): OK := FALSE: while OK = FALSE do print(`Input the righthand endpoint on the X-axis.`): FX := scanf(`%f`)[1]: print(`Righthand endpoint = `):print(FX): if FX <= 0 then print(`Must be a positive number.`): else OK := TRUE: fi: od: OK := FALSE: while OK = FALSE do print(`Input the maximum value of the time variable T.`): FT := scanf(`%f`)[1]: print(`Maximum time value = `):print(FT): if FT <= 0 then print(`Must be a positive number.\134n`): else OK := TRUE: fi: od: print(`Input the constant alpha.`): ALPHA := scanf(`%f`)[1]:print(`alpha = `):print(ALPHA): OK := FALSE: while OK = FALSE do print(`Input integer m := number of intervals on X-axis`): print(`and N := number of time intervals - separated by a blank.`): print(`Note that m must be 3 or larger.`): M := scanf(`%d`)[1]: N := scanf(`%d`)[1]: print(`Number of intervals on x-axis m = `):print(M): print(`Number of time intervals n = `):print(N): if M <= 2 or N <= 0 then print(`Numbers are not within correct range.`): else OK := TRUE: fi: od: if OK = TRUE then M1 := M+1: M2 := M-1: N1 := N+1: N2 := N-1: # Step 1 # V is used in place of lambda H := FX/M: K := FT/N: V := ALPHA*K/H: # Step 2 for J from 2 to N1 do W[0,J-1] := 0: W[M1-1,J-1] := 0: od: # Step 3 W[0,0] := evalf(F(0)): W[M1-1,0] := evalf(F(FX)): # Step 4 for I2 from 2 to M do W[I2-1,0] := F(H*(I2-1)): W[I2-1,1] := (1-V^2)*F(H*(I2-1))+V^2*(F(I2*H)+F(H*(I2-2)))/2+K*G(H*(I2-1)): od: # Step 5 for J from 2 to N do for I2 from 2 to M do W[I2-1,J] := evalf(2*(1-V^2)*W[I2-1,J-1]+V^2*(W[I2,J-1]+W[I2-2,J-1])-W[I2-1,J-2]): od: od: # Step 6 print(`Choice of output method:`): print(`1. Output to screen`): print(`2. Output to text file`): print(`Please enter 1 or 2.`): FLAG := scanf(`%d`)[1]: print(`Input is `):print(FLAG): if FLAG = 2 then print(`Input the file name in the form - drive:\134\134name.ext`): print(`for example: A:\134\134OUTPUT.DTA`): NAME := scanf(`%s`)[1]: print(`Output file is `):print(NAME): OUP := fopen(NAME,WRITE,TEXT): else OUP := default: fi: fprintf(OUP, `FINITE DIFFERENCE METHOD FOR THE WAVE EQUATION\134n\134n`): fprintf(OUP, ` I X(I) W(X(I),%12.6e)\134n`, FT): for I2 from 1 to M1 do X := (I2-1)*H: fprintf(OUP, `%3d %11.8f %13.8f\134n`, I2, X, W[I2-1,N1-1]): od: if OUP <> default then fclose(OUP): print(`Output file `,NAME,` created successfully`): fi: fi: SWZuVGhpc35pc350aGV+RmluaXRlLURpZmZlcmVuY2V+TWV0aG9kfmZvcn50aGV+V2F2ZX5FcXVhdGlvbi5HNiI= SWJvSW5wdXR+dGhlfmZ1bmN0aW9uc35GKFgpfmFuZH5HKFgpfmlufnRlcm1zfm9mfngsfnNlcGFyYXRlZH5ieX5hfnwrc3BhY2UuRzYi SURGb3J+ZXhhbXBsZTp+fnNpbigzLjE0MTU5MjY1NCp4KX5+MEc2Ig== SShGKHgpfj1+RzYi LUkkc2luRzYkJSpwcm90ZWN0ZWRHSShfc3lzbGliRzYiNiMsJEkieEdGJyQiK2FFZlRKISIq SShHKHgpfj1+RzYi JCIiIUYj SUpUaGV+bGVmdGhhbmR+ZW5kcG9pbnR+b25+dGhlflgtYXhpc35pc34wLkc2Ig== SUxJbnB1dH50aGV+cmlnaHRoYW5kfmVuZHBvaW50fm9ufnRoZX5YLWF4aXMuRzYi STZSaWdodGhhbmR+ZW5kcG9pbnR+PX5HNiI= JCIiIiIiIQ== SVBJbnB1dH50aGV+bWF4aW11bX52YWx1ZX5vZn50aGV+dGltZX52YXJpYWJsZX5ULkc2Ig== STZNYXhpbXVtfnRpbWV+dmFsdWV+PX5HNiI= JCIiIiIiIQ== STpJbnB1dH50aGV+Y29uc3RhbnR+YWxwaGEuRzYi SSlhbHBoYX49fkc2Ig== JCIiIyIiIQ== SVFJbnB1dH5pbnRlZ2Vyfm1+Oj1+bnVtYmVyfm9mfmludGVydmFsc35vbn5YLWF4aXNHNiI= SVphbmR+Tn46PX5udW1iZXJ+b2Z+dGltZX5pbnRlcnZhbHN+LX5zZXBhcmF0ZWR+Ynl+YX5ibGFuay5HNiI= SUFOb3RlfnRoYXR+bX5tdXN0fmJlfjN+b3J+bGFyZ2VyLkc2Ig== SUNOdW1iZXJ+b2Z+aW50ZXJ2YWxzfm9ufngtYXhpc35tfj1+RzYi IiM1 ST5OdW1iZXJ+b2Z+dGltZX5pbnRlcnZhbHN+bn49fkc2Ig== IiM/ STlDaG9pY2V+b2Z+b3V0cHV0fm1ldGhvZDpHNiI= STQxLn5PdXRwdXR+dG9+c2NyZWVuRzYi STcyLn5PdXRwdXR+dG9+dGV4dH5maWxlRzYi STVQbGVhc2V+ZW50ZXJ+MX5vcn4yLkc2Ig== SSpJbnB1dH5pc35HNiI= IiIi FINITE DIFFERENCE METHOD FOR THE WAVE EQUATION I X(I) W(X(I),1.000000e+00) 1 0.00000000 0.00000000 2 0.10000000 0.30901699 3 0.20000000 0.58778525 4 0.30000000 0.80901699 5 0.40000000 0.95105652 6 0.50000000 1.00000000 7 0.60000000 0.95105652 8 0.70000000 0.80901699 9 0.80000000 0.58778525 10 0.90000000 0.30901699 11 1.00000000 0.00000000 JSFH