Effect of time step size on the solution of 1-D linear convection.
Effect of time step size on the solution of 1-D linear convection.
Aim: To see the effect of time step size on the solution of 1D linear wave equation, by using Time-marching method of finite difference method.
Theory: The governing equation of 1D linear wave equation is given as
`(∂u)/(∂t)=c.(∂u)/(∂x)`.........(1)
here, c is constant
∂u/∂t is time derivative
∂u/∂x is space term
Matlab code: In this code we have solve 1D linear wave equation by discritizing the equation(1). In discretization, we discretize the time derivative term by using first order forward differencing scheme and first order backward differencing scheme for space term and then solve by using Time-marching method in the form of dt and the by using function we called this code in main code to see the effect of time step size on the solution of 1D linear wave equation.
function [x,uold]=Time_steps(dt)
%input parameters
L=1; %domain Length
n=80; %no. of grid
c=1; %constant
t=0.4; %time
nt = t/dt; %no. of time step
%Calculation;
x=linspace(0,L,n);
dx=L/(n-1);
%Defining the start and end position of square wave;
x_start=0.1;
x_end=0.3;
n_start=round(x_start/dx)+1;
n_end=round(x_end/dx)+1;
%Initialize arrays and apply boundary conditions;
u=ones(1,n);
u(n_start:n_end)=2;
%Boundary Conditions;
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
%updating velocities
uold=u;
end
end
Main code: In the following code, first we have allocated the values of time steps(dt) and then we called the above code and then this main code accepts time step as an argument. And the Tic-toc command is use to calculate the simulation for each time steps.
clear all
close all
clc
dt=[1e-1 1e-2 1e-3 1e-4];
%calling time_step function and computing simulation time for each time
%step by tic-toc command;
tic
[x1,uold1]=Time_steps(1e-1);
toc
t1=toc;
tic
[x2,uold2]=Time_steps(1e-2);
toc
t2=toc;
tic
[x3,uold3]=Time_steps(1e-3);
toc
t3=toc;
tic
[x4,uold4]=Time_steps(1e-4);
toc
t4=toc;
%Plotting velocity profile for various time steps;
figure(1)
hold all
plot(x1,uold1,'linewidth',1,'color','r')
plot(x2,uold2,'linewidth',1,'color','b')
plot(x3,uold3,'linewidth',1,'color','g')
plot(x4,uold4,'linewidth',1,'color','y')
axis([0 1 0 2.2])
title({['1D linear convection velocity profile at C=1,t=0.4s & n=80']})
%Labelling for velocity profile for various time steps;
xlabel('X axis')
ylabel('Y axis')
legend('1e-1','1e-2','1e-3','1e-4')
grid on
%Bar graph of simulation time versus different time steps;
Simulation_time=[t1 t2 t3 t4]
h=categorical({'1e-1','1e-2','1e-3','1e-4'})
figure(2)
bar(h,Simulation_time,'b')
%labelling for simulation time versus different time steps;
xlabel('Time steps')
ylabel('Simulation time')
Output:
Plot: Comparision of velocity profile at different time steps and at t=0.4sec.
Observation:
- The velocity profile for dt=1e-1, shows most unstable solution. Since, as we can see there are many wiggles this due to Gibbs phenomenon.
- The velocity profile for dt=1e-2, shows most stabe solution the velocity profile is much more closer to original velocity profile(square wave).
- The velocity profile for dt=1e-3 and dt=1e-4, is almost same, the profile for this step get diffuse from its original profile.
Bar graph: Comparsion of simulation time for different time steps at t=0.4sec.
Observation:
- The 1e-1 takes more simulation time, because its solution gets below up in the above plot.
- The 1e-2 and 1e-3 gives better simulation time as they give better solution as shown in the above plot.
- the 1e-4 takes more simulation time as compared to 1e-2 and 1e-3, because 1e-4 is very small time step. So, it takes more time travel all grids.
Conclusion:
After Observing above plot and bar graph, we can conclude that larger value of dt gives most unstable solution and simulation time. And as we decrease the value of dt the solution gets stable and the simulation time gets low. But, when we decrease the value of dt beyond a certain value the velocity profile get diffuse and simulation get increase as per CFL number.