Effect of time step on the numerical solution for a linear convection equation
Aim: Our objective is to solve the PDE through MatLab code and compare the effect of time step on the solution and compare the simulation time taken for different time steps.
Step 1:Here we are creating a function which gives postion of node on whole length and final velocity value at that node as output by taking dt as input.
Step 2: giving inputs for the linear convection problem
'L' domain length = 1 meter
t = 0.4 sec
c=1
n=80
Step 3:
U array has been defined as U is 1 at all the X values excepts 0.1 to 0.3 band
So we created a function that predict the nodes at 0.1 and 0.3 and gives you the node number.
So using range operator we defined the value of U in between these nodes as 2
Step 4 :
The function has been carried out through two loops one for to move the step function spacely and other timely, where we used first-order forward differencing for time and backwards differencing for space.
step 5:
Then in the main program we are calling this function and calculating the values of final velocity at each node for each time step.then we also calculate the simulation time and compare them at different time steps.
Result:
figure 1
In figure 1 the graph shows the numerical solutions at t = 0.4 seconds for different time steps are compared
figure 2
Figure 2 shows the simulation time as a function of the time step.
Here the small step size took a large simulation time as the number of iterations gets increased nt=t/dt where t is a constant value of 0.4
when
t=0.1 thevelocity profile gets to blow up because of CFL value getting increased more than 1
t=0.01 the velocity profile looks great and no blow up and simulation time is less
t=0.001 as the step time gets lowered the finer the velocity profile becomes and simulation time is more than for 0.01
t=0.0001 it is even more smooth and the simulation time will be very high as the number of iterations is high.
program :
function:
% function which gives postion of node on whole length and final velocity value at that node as output by taking dt as input
function [x,uold] = timestep(dt)
%inputs for the linear convection problem
L=1; %1 meter length
n=80;
c=1;
x=linspace(0,L,n);
%finding dx based on whole length
dx=L/(n-1);
x_start=0.1;
x_end=0.3;
n_start= round((x_start/dx)+1)
n_end=round((x_end/dx)+1)
%calculations
%Creating an array of u as per the given values
u=1*ones(1,n);
u(1)=1;
u(n_start:n_end)=2;
uold = u;
t=0.4;
%Since we need to find out the solution till 0.4 second we have to carry out t/dt number of iterations
nt=t/dt;
%time loop
for k=1:nt
% space loop
for i=2:n
%first order backward differencing for space term and forward differencing for time
u(i)= uold(i)-(c*dt/dx)*(uold(i)-uold(i-1));
end
uold=u;
end
end
main program:
clear all
close all
clc
dt=[1e-4,1e-3,1e-2,1e-1]
% calling the function and finding the simulation time
tic
[x1,u1] = timestep(1e-4)
toc
t1=toc;
tic
[x2,u2] = timestep(1e-3)
toc
t2=toc;
tic
[x3,u3] = timestep(1e-2)
toc
t3=toc;
tic
[x4,u4] = timestep(1e-1)
toc
t4=toc;
figure(1)
% plotting the velocity profile for different time steps
plot(x1, u1,'color', 'g')
hold on
plot(x2,u2,'color', 'y');
hold on
plot(x3, u3,'color', 'r')
hold on
plot(x4,u4,'color', 'b');
axis([0 1 0 2]);
xlabel('length of domain');
ylabel('velocity');
legend('1e-4','1e-3','1e-2','1e-1');
figure(2)
% comparing the simulation time for different time steps on bar graph
simulation_time = [t1 t2 t3 t4]
bar(simulation_time,'g')
set(gca,'xticklabel', {'1e-4','1e-3','1e-2','1e-1'});
xlabel('time steps');
ylabel('simulation time');
title(' simulation time as a function of the time step');
legend('simulation time');