Effect of different time steps on the one dimensional Linear Convection equation
The one dimension linear convection equation to find the numerical solution is,
`du/dt + [c] du/dx=0`
Using backward differencing and explicit method, we get the following equation,
`u(i) = uold(i) - (c*dx/dt)*(uold(i)-uold(i-1))`
Using the previous conditions, we have
domain length, L=1m
Constant velocity, C=1m/s
Time step, dt= [1e-4,1e-3,1e-2,1e-1]
The velocity profile is a step function where equal to 2m/s at x=0.1 & 0.3 and rest 1m/s
Total time, t = 0.4sec
number of grid points, n =80
To find the numerical solution for different time steps, a function delta_t is being coded as below:
function [u,x] = delta_t(dt)
%inputs for linear convection problem
L = 1; %domain lenghth
n = 80; %nymber if grid points
c = 1; %constant velocity
t=0.4;
nt = t/dt;
%calculations
x = linspace(0,L,n);
dx = x(2) - x(1);
%initialize arrays and apply boundary conditions
u = ones(1,n);
for j= 1:length(x)
if (x(j) >=0.1 && x(j)<=0.3)
u(j) = 2;
else
u(j) =1;
end
end
%BC
u(1) = 1;
uold = u;
%time loop
for k= 1:nt
%space loop;
for i =2:n
u(i) = uold(i) - (c*dt/dx)*(uold(i)-uold(i-1));
end
uold = u;
end
end
Calling this function with arguments of dt is being coded below:
close all
clear all
clc
tic
[u1,x1] = delta_t(1e-4);
toc
t1 = toc;
tic
[u2,x2] = delta_t(1e-3);
toc
t2 = toc;
tic
[u3,x3] = delta_t(1e-2);
toc
t3 = toc;
tic
[u4,x4] = delta_t(1e-1);
toc
t4 = toc;
simulated_time = [t1, t2, t3, t4];
time_step = categorical({'1e-4','1e-3','1e-2','1e-1'});
figure(1)
plot(x1,u1,'r','linewidth',1)
hold on
plot(x2,u2,'b','linewidth',1)
hold on
plot(x3,u3,'black','linewidth',1)
hold on
plot(x4,u4,'g','linewidth',1)
xlabel('Lenght of the domain')
ylabel('velocity')
axis([0 1 0 2])
legend('1e-4','1e-3','1e-2','1e-1')
title('comparing the effect of the time step on the numerical solution')
grid on
hold on
figure(2)
bar(time_step,simulated_time)
xlabel('dt')
ylabel('simulated time')
legend('Time')
title('comparision between simulated time and time step')
Output:
we have plotted all the dt values and compared with varying values of dt. the results are shown below:
This plot compares for all the variables of dt
The above bar graph represents the time taken for simulation.
Observation:
From the above plot, we could see the 1e-1 solution is blown up and has least simulation time.
1e-2 and 1e-3 have low simulation times as well compared to 1e-4, where the simulation time for 1e-4 is maximum.
We could also say from this that, the higher the dt the more the simulation time and vice versa.
But also to consider the lowest value of dt has the solution unstable even though it took least simulation time.
So we should also consider this while solving any Linear convection equations that higher and lower dt values are not always preferable.