Using the Lax Method to solve 1D wave equation
이전 댓글 표시
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)
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Calculus에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!