Lax-Wendroff method for advection equation with periodic boundary condition

29 次查看(过去 30 天)
I'm trying to solve advection equation in with and periodic boundary conditions . My approach is as follow:
clc,clear
xmin=0;
xmax=1;
m=400;%space
t=0;
h=1/(m+1);
n=10/(0.95*h);%time steps
a=1;
dt=0.95*h/a;
x=xmin:h:xmax;
a1=a*dt/(h);
% ic
u0=cos(2*pi*x)+.2*cos(10*pi*x);
% plot(x,u0,'k*')
% hold on
u=u0;
un=u0;
% v = VideoWriter('newfile2.avi');
% open(v)
for j=1:n%time
%bc
% u(1)=u(m+1);
for i=2:m+1%space
un(i)=u(i)-(a1/2)*(u(i+1)-u(i-1))+0.5*(a1)^2*(u(i+1)-2*u(i)+u(i-1));
end
un(1)=un(m+1);
un(m+2)=un(2);
u=un;
t=t+dt;
%exact
y_e=(cos(2*pi*(x-t))+.2*cos(10*pi*(x-t)));
plot(x,y_e)
hold on
plot(x,u,'bo-','MarkerFaceColor','r')
hold off
title(sprintf('time=%1.3f',t))
shg
pause(dt)
% frame = getframe(gcf);
% writeVideo(v,frame);
end
% close(v)
error=abs(max(y_e-un))
The code is running well. However, I'm not getting desired order of accuracy. Your help will be appreciated.

回答(1 个)

Alan Stevens
Alan Stevens 2022-3-13
If you make dt=0.1*h/a; instead of dt=0.95*h/a; your max error reduces to 0.0355.

类别

Help CenterFile Exchange 中查找有关 Spectral Measurements 的更多信息

产品


版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by