Lax Friedrich for square wave
2 views (last 30 days)
Show older comments
Hello there! This is my first post here so I apologize if my introduction is not correct.
I'd like to ask clarifications concerning a Matlab code I wrote to analyze a square wave using Lax-Friedrich I do not understand why I have such big oscillations in the results, if not for too low value of Courant number
Thanks in advance!
%%Lax-Friedrich
a=0.5; %wave speed deltax=0.1; deltat=0.005; c=a*deltat/deltax; %courant number
X=-3:deltax:3; T=0:deltat:3;
%initial conditions u=zeros(length(X),1); i=find(X==-0.5); j=find(X==0.5); u(i:j)=1; un=[];
for t=T
[u]=laxfried(u,c); un=[un,u];
end
figure plot(X,un(:,1),'g') hold on plot(X,un(:,100),'b') plot(X,un(:,150),'r') plot(X,un(:,200),'m') plot(X,un(:,300),'k') plot(X,un(:,500),'k')
%%subroutine function
function [unew]=laxfried(u,c)
[m,n]=size(u); unew=zeros(m,n); for i=2:length(u)-1 unew(i)=-c*0.5*(u(i+1)-u(i-1))+0.5*(u(i+1)+u(i-1)); end

0 Comments
Answers (0)
See Also
Categories
Find more on Geometry and Mesh 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!