Matlab program with the explicit Lax method

    科技2022-07-13  132

    https://www.youtube.com/watch?v=ONDp4Rdj0OY 

    Matlab program with the explicit Lax method for the advection equation 

    %Square-wave test for the lax method to solve the advection equation clear; %Parameters needed to Implement the advection Lmax = 1.0; % Maxinum length Tmax = 1.; % Maximum time c = 1.0; % Advection velocity %Parameters needed to Implement the Lax Method Nt = 300; % Number of time steps Dt = Tmax / Nt; % Time Step Nx = 300; % Number of space steps Dx = Lmax / Nx; %Space step b = c * Dt/(2.*Dx); % beta parameter in the finite-difference imp %Remember that the lax method is stable for b =< 1/2 %But it gets diffussed unless abs(b) = 1/2 %Initial Condition Nfront = round(Nx / 2); % Square wave for i = 1:(Nx + 1) if i < Nfront u(i,1) = 1.; else u(i,1) = 0.; end x(i) = (i-1)*Dx; % We also define vector x, due to the space discretization end %Boundary Condition for k = 1:Nt + 1 u(1,k) = 1.; u(Nx + k) = 0.; t(k) = (k-1)*Dt; % We also define vector t end %Implement of the lax method for k = 1:Nt for i = 2:Nx u(i,k+1) = 0.5*(u(i+1,k)+u(i-1,k))+b*(u(i+1,k)-u(i-1,k)); end end plot(x,u(:,1),'-b'); %time = 1 plot(x,u(:,round(Nt/10)),'--g'); %time = Nt / 10 plot(x,u(:,round(Nt/3)),':b'); %time = Nt / 3 plot(x,u(:,Nt),'-.r'); %time = Nt xlabel('X'); ylabel('Amp(X)');

    首先Nt = 300,这样b刚好等于0.5,介于稳定和不稳定之间 

    Nt = 400,这样b就小于0.5,可以说是稳定了

    Nt = 290,b就大于0.5,不稳定

    Processed: 0.011, SQL: 8