Using the Lax Method to solve 1D wave equation
Show older comments
Needing help to see why my code will only plot the exact solution and breaks during the numerical solution, thanks.

% Lax Method
N=40; %No. of grid points
Tmax=1; % time period
alpha=1; %given
h=1; %given value of delX
% Lax Method
delt=1;
maxt=Tmax/delt; %Number of time steps
c=alpha*delt/h;
u=zeros(N+1,maxt+1);
x=zeros(N+1,1);
% Initial condition
for i=1:N+1
x(i)=(i-1)*h;
u(i,1)=sin((2*pi*x(i)/40));
end
for k=1:maxt
u0 = u(N,k);
u(1,k+1)=(1-c)/2*(u(2,k)-u0)+(1+c)/2*(u(2,k));
for i=2:N
u(i,k+1) =(1-c)/2*(u(i+1,k)+(1+c))/2*(u(i+1,k));
end
uNp2 = u(2,k);
u(N+1,k+1) =(1-c)/2*(uNp2-u(N,k))+(1+c)/2*(uNp2-2*u(N+1,k));
end
plot(x,u)
Answers (0)
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!