Rowan-Classes/5th-Semester-Fall-2023/EEMAGS/Homework/H04.m
2024-02-22 14:23:12 -05:00

37 lines
683 B
Matlab

clear;
clf;
N = 50;
L = 20.0;
h = L/N;
c = 1;
tau = h/c;
nstep = 50;
coef = c * tau / (2*h);
sig=0.1; % width of the pulse
z = ((1:N) - 1/2)*h - L/2; % value of z at time 0
phi = (sech(z / (2 * sig^2))).^2; % wave shape
phiset = zeros(nstep, N);
x = z;
t = 0:tau:(nstep - 1) * tau;
[xx, tt] = meshgrid(x, t);
for ist = 1:nstep
phiset(ist,:) = phi;
for k = 1:N
if ( k ~= 1 ) && ( k ~= N )
phinew(k) = (0.5-coef)*phi(k+1) + (0.5+coef)*phi(k-1);
else
phinew(1) = (0.5-coef)*phi(2) + (0.5+coef)*phi(N);
phinew(N) = (0.5-coef)*phi(1) + (0.5+coef)*phi(N-1);
end
end
phi = phinew;
end
surf(xx, tt, phiset);