All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
Objective > Write a Matlab code to simulate a One-dimensional Linear Convection equation. Conditions > The initial velocity profile is a step function. It is equal to 2m/s between x= 0.1 and 0.3 and 1m/s everywhere else > length is L = 1m > First-order forward differencing for the time derivative…
Jeffry Raakesh
updated on 26 Aug 2021
Objective
> Write a Matlab code to simulate a One-dimensional Linear Convection equation.
Conditions
> The initial velocity profile is a step function. It is equal to 2m/s between x= 0.1 and 0.3 and 1m/s everywhere else
> length is L = 1m
> First-order forward differencing for the time derivative
> First-order backwards/rearward differencing for the space term.
> Constant convection coefficient, c = 1 m/s
> Time step, dt = 0.01
> Simulation run time, t = 0.4 seconds.
> To Perform the simulation for the Number of nodes, n=20,40,80 and 160.
Governing equation
> One-dimensional Linear Convection equation is
∂u∂t+c∂u∂x=o
where,
u = Velocity
c = Convection coefficient
> As per the condition, Forward differencing is done for time derivative and Rearward differencing is used for space derivatives
∂u∂t=un+1i−uniΔt
∂u∂x=uni−uni−1Δx
> Substituting the time and space derivatives in the 1D linear convection equation we get
un+1i−uniΔt+cuni−uni−1Δx=0
un+1i=uni−(cΔtΔx)⋅(uni−uni−1Δx)
> Courant-Friedrichs-Lewy condition to check stability of the above equation
C=c⋅(dtdx)≤1
where,
C = Courant number
c = Constant convection coefficient
dt = Time step
dx = Size of mesh
Programs
Function program
% Function to get the node points of the step function
function[ns,nst] = convec_func(L,n,xs,xst)
dx = L/(n-1);
ns = (xs/dx)+1;
nst = (xst/dx)+1;
end
Program explanation
> When the mesh(x) changes with the number of nodes(n), the start and stop positions of the square wave also changes.
> This function program gives the start and stop positions of the square wave despite the change in the number of nodes.
> The program takes the length(L), nodes(n), start(xs) and stop(xst) positions of the square wave and calculates the nodal start(ns) and nodal stop(nst) values.
> The difference(dx) for the mesh is calculated. The difference value changes according to the number of nodes in the mesh.
> As the value of 'n' get higher the 'dx' reduces
> Then the 'ns' and 'nst' values are acquired using the formula (xd)+1
Main program
% Simulation of One-dimensional Linear Convection equation with different number of nodes
clear all
close all
clc
% Inputs
L = 1;
n = [20 40 80 160];
xs = 0.1;
xst = 0.3;
t = 0.4;
dt = 1e-2;
t_m = t/dt;
c = 1;
for i = 1:length(n)
% Creating domain length 'x' and dx
x = linspace(0,L,n(i));
dx = L/(n(i)-1);
% Creating velocity variable 'u'
u = ones(1,n(i));
% Using function to get the start stop values respective to nodes
[start,stop] = convec_func(L,n(i),xs,xst);
u(start:stop) = 2;
% Storing the velocity values
u_old = u;
u_in = u;
% Calculating Courant number
C(i) = c*(dt/dx);
% Time loop
for m = 1:t_m
% Space loop
for k = 2:n(i)
u(k) = u_old(k) - (c*(dt/dx)*(u_old(k)-u_old(k-1)));
end
% Updating velocity profile
u_old = u;
% Plotting the time marching solution
figure(i)
plot(x,u_in,'b','linewidth',2)
hold on
plot(x,u,'k')
xlabel('Domain Length [m]')
ylabel('Velocity [m/s]')
title(sprintf('Simulation of 1D Linear Convection equation with n = %d',n(i)))
if m == t_m
figure(i+4)
plot(x,u_in,'b','linewidth',2)
hold on
plot(x,u,'-o')
xlabel('Domain Length [m]')
ylabel('Velocity [m/s]')
title(sprintf('Original and final velocity profiles with n = %d',n(i)))
end
end
end
Program explanation
> The length of the step function(L), number of nodes(n), the start(xs) and stop(xst) positions of the step function, the run time(t), the time step(dt), time marching period(t_m) and the constant convection coefficient(c) are given as inputs.
> A loop is created for each value of n
> Inside the loop, a mesh(x) for the length(L) is created according to the nodes(n).
> The distance between nodes(dx) is calculated.
> Then the velocity(u) is defined as 1m/s at all points.
> The function is called to get the start and stop positions of the square wave which is then used to designate the initial velocity condition.
> The initial velocity condition is stored in another variable (u_in) for plotting purposes.
> The Courant number C is calculated.
> A time loop is defined for the period of time marching
> Within the time loop the space loop is specified from 2 to n.
> The derived 1D linear convection equation is used to calculate the solution profile.
> The convected velocity profile acquired is stored for the next iteration till the end of the loop.
> The simulation for each n with its initial condition is plotted accordingly.
> The original and final time marched profile is plotted in separate plots for comparison.
Results
At n = 20
At n = 40
At n = 80
At n = 160
Courant number C
for - n=20, n=40, n=80, n=160 respectively are
Conclusion
> From the above plots, it is seen that as the number of nodes increases, the accuracy of the convected solution increases.
> As per the Courant-Friedrichs-Lewy condition, for an equation to be stable the courant number 'C' must be less than or equal to unity. If the Courant number is greater than 1, it means that the information propagates through more than one grid cell at each time step, making the solution inaccurate and potentially resulting in divergence of the solution
> It is clearly seen that when n=80, the value of C is closest to unity and when the value of n=160 the value of C>1.
> Therefore, at n=80 the solution is most accurate and it becomes unstable when n=160.
> A dissipative behaviour occurs in the solutions due to the truncation error caused by the even-order derivatives. The phenomenon is known as numerical dissipation interchangeably artificial viscosity.
> Due to the change in the grid size the peak velocity when n=20 and n=40 get numerically diffused or dissipated rapidly compared to when n=80.
> When the grid size is increased further, the courant number exceeds unity and the solution becomes unstable.
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
Solving 1D Linear convection equation using MATLAB
Objective > Write a Matlab code to simulate a One-dimensional Linear Convection equation. Conditions > The initial velocity profile is a step function. It is equal to 2m/s between x= 0.1 and 0.3 and 1m/s everywhere else > length is L = 1m > First-order forward differencing for the time derivative…
26 Aug 2021 12:45 PM IST
Deriving 4th order approximation of a 2nd order derivative by differencing schemes and evaluating them using MATLAB
Objective > Derive the following 4th order approximations of the second-order derivative. Central difference Skewed right-sided difference Skewed left-sided difference > To prove that the skewed schemes are fourth-order accurate. > Write a program in Matlab to evaluate the second-order derivative of the…
23 Aug 2021 07:21 AM IST
Curve fitting using Matlab
Objective > Write code to fit a linear and cubic polynomial for the given data. > To measure the fitness characteristics for both the curves. > Write code to fit a curve using splitwise method. > To explain best fit, perfect fit, and improvements for cuibc fit. Solution The code to fit a linear…
28 Mar 2021 04:00 PM IST
Breaking Ice with Air cushion Vehicle - Find minimum pressure with Newton-Raphson method in Python
Objective > To find out the value of pressure for h = 0.6 ft by the below given equation using Newton-Raphson method. p3(−β2)+(0.4hβ2−σh2r2)p2+(σ2h43r4)p−(σh23r2)3=0 p = Cushion pressure β = Width of the ice wedge …
28 Mar 2021 03:52 PM IST
Related Courses
0 Hours of Content
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.