a new title
Different Time Step
Main Program
clear allclose allclc%InputL = 1;n = 80; C = 1;t = 0.4;dt = [1e-4 1e-3 1e-2 1e-1];%Initialize Arraysx = linspace(0,L,n);u = 1*ones(1,n);l = length(u);u = my_limit(x,l);u_ini = u;u_old = u; %Calculationfor k=1:4 u = time_marching(dt(k),u,l,n,C,t,x); plot(x,u); hold on;endlegend('t=1e-4','t=1e-3','t=1e-2','t=1e-1');xlabel('Length in m');ylabel('Velocity in m/s');Function time_marching
function u = time_marching(dt,u,L,n,C,t,x)%Calculationdx = x(2) - x(1);%Initialize Arraysu_ini = u;uold = u;nt = t/dt;%Time Marchingfor j=1:nt %Space Marching for i=2:n u(i) = uold(i)-C*(dt/dx)*(uold(i)-uold(i-1)); end %Update Velocity uold = u; end endFunction my_limit
function vel = my_limit(x,l)for i=1:l if(x(i)>=0.1 && x(i)<=0.3) vel(i) = 2; else vel(i) = 1; endendendResult
Conclusion
As the time step is smaller the greater value of velocity is obtained. So for the smallest time step 1e-4 the velocity of 1.8m/s is obtained at location of 0.6 meter in the domain, which is better solution.