Project

Linear Convection with varing time step

\"time\"

* Bar graph presents the time taken by different cases time step 1e-1 and 1e-2 looks have taken the similar time but later is slightly taken more than former.

* time step 1e-4 has taken the highest time around 23 sec leaving step size 1e-3 way behind which ended up slighly less tha n 4 sec i guess.

\"Velocity\"

* 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.

Main code

% 1 d linear convection matlab function
% 
% function 
% arguments passed in the function 
% n is number of grids and f is the number of figure
function [x,u] =  simple_convection(dt,color)


 
% Inputs for the linear convention 
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);
%dx = L/(n-1)

% Define the start and end positions for 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);   

% Intialize arrays and apply boundary conditions
u = ones(1,n);
u(n_start:n_stop)= 2; %Velocity profile = 2m/s between 0.1 and 0.3
%BC
u(1) =1;
 uold =u;
 u_initial =u;

 
 
 %Time loop
 
 for k =1:length(nt)
     
     % space loop forward difference scheme

     for i =2:n
         u(i) = uold(i)-(c*dt/dx)*(uold(i) - uold(i-1)); % FDS for space loop
     end
     
  % Updating old velocity
    uold = u;
 
 end
 hold on
     plot(x,u,color);
     axis([0 1 0.8 3])
 
 end
 

Code of plot generation

clear all
close all
clc

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,\'g\');
   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,\'b\');
   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,\'r\');
   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,\'b\');
   plot(x3,u3)    
t4 = toc;
xlabel(\'grid size\')
ylabel(\'velocity\')
legend(\'1e-4\')
grid minor
% bar graph comparing 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(s)\');
title(\'comparison of time taken\')
set(gca,\'xticklabel\',{\'1e-1\',\'1e-2\',\'1e-3\',\'1e-4\'})
saveas(gca,\'comparison of time.png\')

function of end function:

function xdot = func_end(x,x_end)
 
for i = 1:length(x);

a(i) = abs(x_end-x(i)); %a is the difference

end


[num,xdot] = min(a);

end

function of start function:

function xdot = func_start(x,x_start)
 
for i = 1:length(x);

a(i) = abs(x_start-x(i)); %a is the difference

end


[num,xdot] = min(a);

end
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.