> 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. > alg124 := proc() local F, G, OK, FX, FT, ALPHA, M, N, M1, M2, N1, N2, H, K, V, J, W, I, flag, NAME, OUP, X; > printf(`This is the Finite-Difference Method for the Wave Equation.\n`); > printf(`Input the functions F(X) and G(X) in terms of x, separated by a > space.\n`); > printf(`For example: sin(3.141592654*x) 0\n`); > F := scanf(`%a`)[1]; > G := scanf(`%a`)[1]; > F := unapply(F,x); > G := unapply(G,x); > printf(`The lefthand endpoint on the X-axis is 0.\n`); > OK := FALSE; > while OK = FALSE do > printf(`Input the righthand endpoint on the X-axis.\n`); > FX := scanf(`%f`)[1]; > if FX <= 0 then > printf(`Must be a positive number.\n`); > else > OK := TRUE; > fi; > od; > OK := FALSE; > while OK = FALSE do > printf(`Input the maximum value of the time variable T.\n`); > FT := scanf(`%f`)[1]; > if FT <= 0 then > printf(`Must be a positive number.\n`); > else > OK := TRUE; > fi; > od; > printf(`Input the constant alpha.\n`); > ALPHA := scanf(`%f`)[1]; > OK := FALSE; > while OK = FALSE do > printf(`Input integer m := number of intervals on X-axis\n`); > printf(`and N := number of time intervals - separated by a blank.\n`); > printf(`Note that m must be 3 or larger.\n`); > M := scanf(`%d`)[1]; > N := scanf(`%d`)[1]; > if M <= 2 or N <= 0 then > printf(`Numbers are not within correct range.\n`); > 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 I from 2 to M do > W[I-1,0] := F(H*(I-1)); > W[I-1,1] := (1-V^2)*F(H*(I-1))+V^2*(F(I*H)+F(H*(I-2)))/2+K*G(H*(I-1)); > od; > # Step 5 > for J from 2 to N do > for I from 2 to M do > W[I-1,J] := > evalf(2*(1-V^2)*W[I-1,J-1]+V^2*(W[I,J-1]+W[I-2,J-1])-W[I-1,J-2]); > od; > od; > # Step 6 > printf(`Choice of output method:\n`); > printf(`1. Output to screen\n`); > printf(`2. Output to text file\n`); > printf(`Please enter 1 or 2.\n`); > FLAG := scanf(`%d`)[1]; > if FLAG = 2 then > printf(`Input the file name in the form - drive:\\name.ext\n`); > printf(`for example: A:\\OUTPUT.DTA\n`); > NAME := scanf(`%s`)[1]; > OUP := fopen(NAME,WRITE,TEXT); > else > OUP := default; > fi; > fprintf(OUP, `FINITE DIFFERENCE METHOD FOR THE WAVE EQUATION\n\n`); > fprintf(OUP, ` I X(I) W(X(I),%12.6e)\n`, FT); > for I from 1 to M1 do > X := (I-1)*H; > fprintf(OUP, `%3d %11.8f %13.8f\n`, I, X, W[I-1,N1-1]); > od; > if OUP <> default then > fclose(OUP): > printf(`Output file %s created successfully`,NAME); > fi; > fi; > # Step 7 > RETURN(0); > end; > alg124();