Project

Linear convection week 4 last challenge

  • MAIN CODE
function [x,u] = simple_convection(dt)

% inputs for the linear convection

L=1;
c=1;
n=80;
t_end=0.4;
nt=(0.01:dt:t_end);


x=linspace(0,L,n);
dx=x(2)-x(1);

% define start and end position of the square wave
x_start= 0.1;
x_end=0.3;

% function to calculate n_start and n_stop

n_start= func_start(x,x_start);
n_stop= func_end(x,x_end);

% initialize arrays and apply the boundary conditions
u=ones(1,n);
u(n_start: n_stop) =2;

%BC
u(1)=1;
uold=u;
u_initial= u;

% Time loop
 for k= 1: length (nt)
     
     % space forward difference scheme
     
     for i=2:n
         u(i)= uold(i)-(c*dt/dx)* (uold(i)-uold(i-1));
     end
     
     % update old velocity
     uold=u;
     
 end
 
 hold on
 plot(x,u);
 axis([0 1 0.8 2])
 
end
  • Function to calculate n_start
function xdot= func_start(x, x_start)
 for i= 1:length(x);
     a(i)= abs(x_start-x(i));
     
 end
 
 [num, xdot] = min(a);
 
end
  • Function to calculate n_stop
function xdot= func_end(x,x_end)

for i= 1:length(x);
    
    a(i)= abs(x_end-x(i));
end

[num, xdot] = min(a);

end
  • CODE FOR GRAPHS PLOTTING
figure(1)
title(\'comparison of solution at T=0.4 \');
hold on
% Time step size of 1e-1
subplot(2,2,1)
tic
[u0,x0]= simple_convection (1e-1);
plot(x0,u0)
t1=toc;
xlabel(\'grid size\')
ylabel(\'velocity\')
legend(\'1e-1\')
grid minor

% Time step size of 1e-2
subplot(2,2,2)
tic
[u1,x1]= simple_convection (1e-2);
plot(x1,u1)
t2=toc;
xlabel(\'grid size\')
ylabel(\'velocity\')
legend(\'1e-2\')
grid minor

% Time step size of 1e-3
subplot(2,2,3)
tic
[u2,x2]= simple_convection (1e-3);
plot(x2,u2)
t3=toc;
xlabel(\'grid size\')
ylabel(\'velocity\')
legend(\'1e-3\')
grid minor


% Time step size of 1e-4
subplot(2,2,4)
tic
[u3,x3]= simple_convection (1e-4);
plot(x3,u3)
t4=toc;
xlabel(\'grid size\')
ylabel(\'velocity\')
legend(\'1e-4\')
grid minor

% bar graph camparing the time taken by each process
time_step=  [1e-1, 1e-2, 1e-3, 1e-4];
simulation_time= [t1;t2;t3;t4];
figure(2)
bar(simulation_time);
xlabel(\'step size\');
ylabel(\'simulation time in seconds\');
set(gca,\'xticklabel\',{\'1e-1\',\'1e-2\',\'1e-3\',\'1e-4\'})

1) Graphs that compare the numerical solution at different time steps

From the above graphs it can be concluded that,

For step size of 1e-1, the solution is completely blown up.

Step size 1e-2 is about go get the stability and looks close to achieve stability.

Step size 1e-3 & 1e-4 looks quite similar seems have achieved the stability.

2) Plot the simulation time as a function of the time step

Bar graph presents the time taken by different cases.

1e-1 time step has taken maximum time followed by 1e-4.

Time steps 1e-2 and 1e-3 almost took a similar time.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.