Wave Equation Finite Difference-Numerical Analysis-MATLAB Code, Exercises of Mathematical Methods for Numerical Analysis and Optimization

This is solution to one of problems in Numerical Analysis. This is matlab code. Its helpful to students of Computer Science, Electrical and Mechanical Engineering. This code also help to understand algorithm and logic behind the problem. This code includes: Wave, Equation, Finite, Difference, Algorithm, Approximate, Boundary, Conditions, Initial, Constant, Endpoint, Integers

Typology: Exercises

2011/2012

Uploaded on 07/31/2012

saripella
saripella 🇮🇳

4.4

(132)

169 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
% 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.
syms('OK', 'FX', 'FT', 'ALPHA', 'M', 'N', 'M1', 'M2', 'N1', 'N2');
syms('H', 'K', 'V', 'J', 'W', 'I', 'FLAG', 'NAME', 'OUP', 'X', 's',
'x');
TRUE = 1;
FALSE = 0;
fprintf(1,'This is the Finite-Difference Method for the Wave
Equation.\n');
fprintf(1,'Input the functions F(X) and G(X) in terms of x, \n');
fprintf(1,'on separate lines. \n');
fprintf(1,'For example: sin(pi*x)\n');
fprintf(1,' 0 \n');
s = input(' ','s');
F = inline(s,'x');
s = input(' ','s');
G = inline(s,'x');
fprintf(1,'The lefthand endpoint on the X-axis is 0.\n');
OK = FALSE;
while OK == FALSE
fprintf(1,'Input the righthand endpoint on the X-axis.\n');
FX = input(' ');
if FX <= 0
fprintf(1,'Must be a positive number.\n');
else
OK = TRUE;
end;
end;
OK = FALSE;
while OK == FALSE
fprintf(1,'Input the maximum value of the time variable T.\n');
FT = input(' ');
if FT <= 0
fprintf(1,'Must be a positive number.\n');
else
OK = TRUE;
end;
end;
fprintf(1,'Input the constant alpha.\n');
ALPHA = input(' ');
OK = FALSE;
while OK == FALSE
fprintf(1,'Input integer m = number of intervals on X-axis\n');
fprintf(1,'and N = number of time intervals - on separate lines.\n');
docsity.com
pf3

Partial preview of the text

Download Wave Equation Finite Difference-Numerical Analysis-MATLAB Code and more Exercises Mathematical Methods for Numerical Analysis and Optimization in PDF only on Docsity!

% WAVE EQUATION FINITE-DIFFERENCE ALGORITHM 12.

% 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. syms('OK', 'FX', 'FT', 'ALPHA', 'M', 'N', 'M1', 'M2', 'N1', 'N2'); syms('H', 'K', 'V', 'J', 'W', 'I', 'FLAG', 'NAME', 'OUP', 'X', 's', 'x'); TRUE = 1; FALSE = 0; fprintf(1,'This is the Finite-Difference Method for the Wave Equation.\n'); fprintf(1,'Input the functions F(X) and G(X) in terms of x, \n'); fprintf(1,'on separate lines. \n'); fprintf(1,'For example: sin(pi*x)\n'); fprintf(1,' 0 \n'); s = input(' ','s'); F = inline(s,'x'); s = input(' ','s'); G = inline(s,'x'); fprintf(1,'The lefthand endpoint on the X-axis is 0.\n'); OK = FALSE; while OK == FALSE fprintf(1,'Input the righthand endpoint on the X-axis.\n'); FX = input(' '); if FX <= 0 fprintf(1,'Must be a positive number.\n'); else OK = TRUE; end; end; OK = FALSE; while OK == FALSE fprintf(1,'Input the maximum value of the time variable T.\n'); FT = input(' '); if FT <= 0 fprintf(1,'Must be a positive number.\n'); else OK = TRUE; end; end; fprintf(1,'Input the constant alpha.\n'); ALPHA = input(' '); OK = FALSE; while OK == FALSE fprintf(1,'Input integer m = number of intervals on X-axis\n'); fprintf(1,'and N = number of time intervals - on separate lines.\n');

fprintf(1,'Note that m must be 3 or larger.\n'); M = input(' '); N = input(' '); if M <= 2 | N <= 0 fprintf(1,'Numbers are not within correct range.\n'); else OK = TRUE; end; end; if OK == TRUE W = zeros(M+1,N+1); M1 = M+1; M2 = M-1; N1 = N+1; N2 = N-1; % STEP 1 % V is used for lambda H = FX/M; K = FT/N; V = ALPHAK/H; % STEP 2 for J = 2 : N W(1,J) = 0; W(M1,J) = 0; end; % STEP 3 W(1,1) = F(0); W(M1,1) = F(FX); % STEP 4 for I = 2 : M W(I,1) = F(H(I-1)); W(I,2) = (1-V^2)F(H(I-1))+V^2(F(IH)+F(H(I-2)))/2+KG(H(I-1)); end; % STEP 5 for J = 2 : N for I = 2 : M W(I,J+1) = 2(1-V^2)W(I,J)+V^2(W(I+1,J)+W(I-1,J))-W(I,J-1); end; end; % STEP 6 fprintf(1,'Choice of output method:\n'); fprintf(1,'1. Output to screen\n'); fprintf(1,'2. Output to text file\n'); fprintf(1,'Please enter 1 or 2.\n'); FLAG = input(' '); if FLAG == 2 fprintf(1,'Input the file name in the form - drive:\name.ext\n'); fprintf(1,'for example: A:\OUTPUT.DTA\n'); NAME = input(' ','s'); OUP = fopen(NAME,'wt'); else OUP = 1; end; fprintf(OUP, 'FINITE DIFFERENCE METHOD FOR THE WAVE EQUATION\n\n');