Solving linear convection equation (wave equation) by Lax Wendroff Scheme
79 views (last 30 days)
Show older comments
Hi!
I am trying to solve the problem in the text attached.
I am copying my MATLAB code to solve the Lax Wendroff scheme. I am struggling to put in the periodic boundary conditions. Maybe someone could guide?
Thanks!
% MAE 5093 Homework #5- Due November 13th, 2018
%%Problem 1
%%Code written by Saad Saleem
% % This MATLAB code solves linear convection equation
clear all
close all
N=100; %No. of grid points
Tmax=1; % time period=1
alpha=1; %given
h=0.01; %given value of delX
delt=0.005;
maxt=Tmax/delt; %Number of time steps
c=alpha*delt/h; %c=0.5
% Initial condition
for i=1:N+1;
x(i)=(i-1)*h;
u(i,1)=sin(2*pi*x(i));
end
% Temp at boundaries
for k=1:maxt+1;
u(1,k)=u(N+1,k);
end
u=zeros(maxt,N);
for k=1:maxt
for i=1:N %Space loop
u(i,k+1) =u(i,k)-c/2*(u(i+1,k)-u(i-1,k))+(c.^2)/2*(u(i+1,k)-2*u(i,k)+u(i-1,k));
end
end
0 Comments
Accepted Answer
Torsten
on 9 Nov 2018
Edited: Torsten
on 9 Nov 2018
N=100; %No. of grid points
Tmax=1; % time period=1
alpha=1; %given
h=0.01; %given value of delX
delt=0.005;
maxt=Tmax/delt; %Number of time steps
c=alpha*delt/h; %c=0.5
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));
end
for k=1:maxt
u0 = u(N,k);
u(1,k+1)=u(1,k)-c/2*(u(2,k)-u0)+c^2/2*(u(2,k)-2*u(1,k)+u0);
for i=2:N
u(i,k+1) =u(i,k)-c/2*(u(i+1,k)-u(i-1,k))+(c^2)/2*(u(i+1,k)-2*u(i,k)+u(i-1,k));
end
uNp2 = u(2,k);
u(N+1,k+1) =u(N+1,k)-c/2*(uNp2-u(N,k))+c^2/2*(uNp2-2*u(N+1,k)+u(N,k));
end
plot(x,u(:,10),x,u(:,20),x,u(:,30),x,u(:,40),x,u(:,50),x,u(:,60),x,u(:,70))
0 Comments
More Answers (0)
See Also
Categories
Find more on Boundary Conditions in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!