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.
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 govering equation of 1D linear wave equation is given as
........... (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 discretizing 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 then 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 parameter;
L=1; %domain length
n=80; %no of grid
c=1; %constant
t=0.4; %time
nt=t/dt; %number 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 condition;
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 times steps(dt) & 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_steps function and computing simulation time for each time step by using tic-toc command;
%For time step dt=1e-1;
tic
[x1,uold1]=Time_steps(1e-1);
toc
t1=toc; %Simulation time for dt=1e-1;
tic
[x2,uold2]=Time_steps(1e-2);
toc
t2=toc; %Simulation time for dt=1e-2;
tic
[x3,uold3]=Time_steps(1e-3);
toc
t3=toc; %Simulation time for dt=1e-3;
tic
[x4,uold4]=Time_steps(1e-4);
toc
t4=toc; %Simulation time for dt=1e-4;
%Plottng Velocity profile for various time steps;
figure(1)
hold all
plot(x1,uold1,"LineWidth",1,"Color",'r')
plot(x2,uold2,"LineWidth",1,"Color",'bl')
plot(x3,uold3,"LineWidth",1,"Color",'g')
plot(x4,uold4,"LineWidth",1,"Color",'b')
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('Velocity ')
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:-Comparsion of velocity profile at different time steps & at t=0.4s.
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 stable solution the velocity profile is much more closer to original velocity profile(square wave).
- The velocity profile for dt=1e-3 & 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.4s.
Observation:-
- The 1e-1 takes more simulation time,because its solution gets blows up as shown in the above plot.
- The 1e-2 & 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 & 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.