Project

Effect of time step size on the solution of 1D linear equation solved by time marching method .

Objective : To observe the effect of time step size on the solution of 1D linear equation. It is in sequence with the previous report : here

Parameters changed : To observe the different time steps of same solution and understand the time it takes to simulate that solution, we create a function that accepts time step as an arguement , and provide that arguement in the main code where it plots with those arguements in the same plot . And the use of tic toc command to calculate the time taken for each of the solution with its respective time step . (plotted in bar)

Program :

function [nx,uold] = time_steps(dt)

%% Specifying parameters
L = 1;                              % domain length
n = 80;
nx = linspace(0,L,n);               % Number of steps in grid
T = 0.4 ; 
c = 1;                              % velocity
                         
dx = nx(2)-nx(1);                   % width of space 
nt = T/dt;                          % number of steps in time
u = zeros(1,n);                    % preallocating space

%% define start and end points for square vave 
 for i = 1:n
     
    if ((0.1<=nx(i))&&(nx(i)<=0.3))
       u(i) = 2;
     else 
         u(i) = 1;
         
    end
 end


uold = u;


%%
for k = 1:nt
    
    %space loop
    for i = 2:n
       u(i) = uold(i) - (c*dt/dx)*(uold(i)-uold(i-1));
    end

    %update old velocities 
    uold = u;
end

end
  1. This function solves the 1D linear equation with explicit form , first order forward differencing for time derivative and 1st order backward differencing for space derivative .
  2. T is set to be 0.4 , using formula T = nt*dt for the for loop nt (time loop). Giving 0.4 seconds for any time step .
  3. Start and end points are obtained for the square wave using if else logic
  4. The equation is solved after that .
  5. dt is taken from the main code , as arguement .

Main program :

%%   1-D Linear eqn  model by time marching method of (Finite difference Method )
% This code accepts time step as an arguement and solves the problem for a
% number of time steps. 
%Code by Aadil Shaikh 
clear all 
close all
clc


%% using tic toc command to call function in the x and y values of plot to be from the function ,at the time steps (1e-1,1e-2,1e-3,1e-4) serially
% tic toc calculates the time of each time step  
tic 
[nx1,uold1] = time_steps(1e-1);
toc 
t1 = toc;

tic 
[nx2,uold2]= time_steps(1e-2);
toc 
t2 =toc;

tic 
[nx3,uold3] = time_steps(1e-3);
toc 
t3 =toc;

tic 
[nx4,uold4] = time_steps(1e-4);
toc 
t4 =toc;

%% plotting the velocities and comparing the effect for different time steps
    figure(1)
    hold all
    loglog(nx1,uold1,'g','linewidth',1)
    loglog(nx2,uold2,'-o','linewidth',.5)
    loglog(nx3,uold3,'-s','linewidth',.5)
    loglog(nx4,uold4,'b','linewidth',.5)
    axis([0 1.2 0.8 2.2])
    title({['1-D Linear Convection at C = 1 & n = 80' ];['time = 0.4 sec ']})
    legend('1e-1','le-2','1e-3','1e-4')
    xlabel('Grid Co-ordinate (x) \rightarrow')
    ylabel('Velocity (u) \rightarrow')
   
 %% plotting Simulation time as function of time step
total_time_of_simulation = [t1 t2 t3 t4];
h = categorical({'1e-1','1e-2','1e-3','1e-4'});
figure(2)
bar(h,total_time_of_simulation,'g')
xlabel('Time step')
ylabel('Simulation time')
title('Simulation time')

  1. tic toc command caculates the time for solving the equation and its simulation time basically.
  2. nx and uold , are grid spacing and velocities obtained resp. theyre varying for each time step respectively from 1e-1 to 1e-4 . the values of nx and uold are called from previous function code using two outputs . thus nx1 and uold1 and so forth are given similar names to plot the same values .
  3. loglog graph for every velocitiy is plotted for each time step in the same plot to observe variation.
  4. appropriate labels , titles and legends are provided .
  5. Bar graph is plotted for comparing the Time of simulation for each time step.

Plots :

Comparison for different time steps :

Explanation of the behaviour of plot(solution comparison)

  1. The solution for 1e-1 shows up first then it blows up and gives an incorrect graph . It happens because of Courant–Friedrichs–Lewy (CFL) condition .
  2. The CFL principle states : The principle behind the condition is that, for example, if a wave is moving across a discrete spatial grid and we want to compute its amplitude at discrete time steps of equal duration, then this duration must be less than the time for the wave to travel to adjacent grid points.
  3. The corollary states : when the grid point separation is reduced, the upper limit for the time step also decreases.
  4. So whats happening here is , the time step 1e-1 is possibly greater than the time for the wave to travel to adjacent grid point hence the solution blows up .
  5. while for 1e-2, The solution stood up better than all, the square curve will diffuse the slowest for this one . this time step is closest to the duration of the time for the wave to travel to adjacent grid points.
  6. Followed by 1e-3 and 1e-4 time steps .

Comparison of simulation time :

  1. The simulation time is most for the first time step 1e-1 , as the solution blows up it takes more computation time .
  2. The 1e-2 and 1e-3 give better simulation time as they give better result for the equation as per cfl principle .
  3. the time step '1e-4' becomes very small for the time for the wave to travel to adjacent grid points. hence it takes more computation time .
  4. Simulation time output :

Conclusion :

  1. 1e-2 and 1e-3 time step give accurate and faster result as compared to 1e-1 and 1e-4 time steps for the 1D linear equation by time marching method .

References :

https://en.wikipedia.org/wiki/Courant%E2%80%93Friedrichs%E2%80%93Lewy_condition

---------------------------------------THE END -----------------------------------------

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.