Project

1-D linear convection dt change

The main code is here:

clear all
close all
clc

%inputs for the linear convection problem
L = 1;
n = 81;
c = 1;
dt = [1e-4 1e-3 1e-2 1e-1];

%calculations
% mesh length = total length / no of grid points
x = linspace(0,L,n);
dx = x(2) - x(1);
%or dx = L/(n-1)


%defining start and end of square
%n_start = 3;
%n_end = 7;
x_start = 0.1;
x_end = 0.3;
[n_start,n_end] = start_end(x_start,x_end,x);

%initialize soln arrays and apply boundary condtns
u = ones(1,n);
u(n_start:n_end) = 2;

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



figure(1)
plot(x,u_initial,\'g\')
hold on

for p = 1:length(dt)
    
    tic
    
    nt = 0.4/dt(p);
    
    uold = u_initial;
    
   for k = 1:nt
    
        [u] = sol(n,c,uold,dx,dt(p)); 
    
    	%updating old velo
        uold = u;
        
   end
    
   time(p) = toc;
   
      

    plot(x,u)
    axis([0 1 0 2])
    hold on
    pause (0.03)
    
end
xlabel(\'L\');
ylabel(\'u\');
legend(\'initial\',\'1e-4\',\'1e-3\',\'1e-2\',\'1e-1\');

figure(2)
loglog(dt,time,\'-o\')
xlabel(\'dt\')
ylabel(\'time taken\')

Function to find n_start and n_end is here:

function [n1,n2] = start_end(x_start,x_end,x)
for o = 1:length(x)
    if x(o) == x_start
        n1 = o;
    elseif x(o) == x_end
        n2 = o;
    end
    
end

Function for time marching is here:

function [uf] = sol(n,c,uold,dx,dt)

uf = uold;

% time marching solution
for i = 2:n
    uf(i) = uold(i) - (c*(dt/dx)*(uold(i) - uold(i-1)));
end
end

Graph comparing the numerical solution at t=0.4 :

Explanation:

The green curve represents the initial velocity map(t=0).

The Blue curve represents the solution at t=0.4 when dt is 1e-4.

The Red curve represents the solution at t=0.4 when dt is 1e-3.

The Orange curve represents the solution at t=0.4 when dt is 1e-2

The Purple curve represents the solution at t=0.4 when dt is 1e-1.

It is found that as the time step increases the diffusion errors in the solution decreases. But when time step is increased too much solution becomes unstable. It can be concluded that making the time step too small makes the simulation run for too long and as a result diffusion error increases with each iteration and the final solution has a larger diffusion error. Time step of 0.01 has the least diffusivity out all the time steps. Time step of 0.1 is very large and makes the solution unstable.

The plot between different time steps and time taken for each to solve is here

I have also used a log plot to describe the same as the order of the time taken are very different for each time step. This can be found here.

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.