Project

Numerical Solution for the 1D Linear Convection Equation with varying time steps using MATLAB.

Aim: To write a code in MATLAB that demonstrates the Numerical solution for the One-Dimensional Linear Convection Equation.

Objectives:

  1. To solve for the following One-Dimensional Linear Convection Equation using Numerical Solutions:`(∂u)/(∂t)+c(∂u)/(∂x)=0`
  2. The solution is to be calculated using a different number of time steps.
  3. Keeping the initial velocity constant, the final velocity is compared across the different time steps.

One-Dimensional Linear Convection Equation:

The equation under consideration for our problem is the One-Dimensional Linear Convection Equations, which is as follows:

`(∂u)/(∂t)+c(∂u)/(∂x)=0`

Using the Numerical Solution methods, the Finite Difference Method to be more specific, we will solve this equation using a time marching procedure, i.e., we will solve for the velocity at all nodes for each time-step and we will do so for all the pre-defined number of time-steps:

The initial conditions are pre-defined at time-step t0 and it is used to compute the velocity profiles for the next time step. Similarly, the further time steps are computed using the conditions of the previous time-step.

The velocity at node 2 for time-step 2 is calculated as follows:

`(∂u)/(∂t)+c(∂u)/(∂x)|_2`

⇒ `(u_2^2-u_2^1)/(Δt)+c(u_2^1-u_1^1)/(Δx)=0`

⇒ `u_2^2=u_2^1-c(Δt)/(Δx)[u_2^1-u_1^1]`

Similarly, the velocity at node 3 for time-step 3 is calculated as follows:

`(u_2^3-u_2^2)/(Δt)+c(u_2^2-u_1^2)/(Δx)=0`

⇒ `u_2^3=u_2^2-c(Δt)/(Δx)u_2^2-u_1^2`

The same can be generalised as follows for all the nodes for time-step2:

`(u_2^(n+1)-u_2^n)/(Δt)+c(u_2^n-u_1^n)/(Δx)=0`

⇒ `u_2^(n+1)=u_2^n-c(Δt)/(Δx)u_2^n-u_1^n`

In the same way we calculate the velocities at all the nodes at further time-steps. And a generalised equation for the same is as follows:

`(∂u)/(∂t)+c(∂u)/(∂x)|_T`

⇒ `(u_T^(n+1)-u_T^n)/(Δt)+c(u_T^n-u_T^n)/(Δx)=0`

⇒ `u_T^(n+1)=u_T^n-c(Δt)/(Δx)u_T^n-u_T^n`

MATLAB program to implement the above-explained method to solve for 1D linear convection equation.

The initial conditions for our code are as follows:

  1. The domain length L = 1m.
  2. The initial velocity profile is a step function where the velocity is equal to 2 m/s between x = 0.1 and 0.3 and 1m/s everywhere else.

Other relevant values are taken as follows:

  1. Constant convection co-efficient = 1m/s.
  2. Time steps are varied as, dt = [1e-4s, 1e-3s, 1e-2s, 1e-1s]
  3. Simulation run time, t = 0.4s
  4. The number of nodes is taken as 81 (we take it as 81 and not 80 for the sake of clearly defining the node points where x = 0.1 and 0.3)

The MATLAB code to calculate what is described above is as follows:

%MATLAB code to slove for 1D convection for varing time-steps.
clear all
close all
clc

%defining initial values
L = 1;
n = 81;
c = 1;
t = 0.4;
dt = [1e-4 1e-3 1e-2 1e-1];
x = linspace(0,L,n);
dx = x(2) - x(1);

%loop for the different values of Time-Steps.
for k = 1:length(dt) 
    
    nt(k) = t/dt(k);
    
       
%following set of lines are used to get the node number for the step-profile for velocity in initial time-step.    
for i = 1:n
    
    if(x(i) == 0.1)
       n_start = i; 
    end
    if(x(i) == 0.3)
        n_stop = i;
    end
end

%following lines are used to define the initial conditions.
u = 1*ones(1,n);
u(n_start:n_stop) = 2;

%following lines copy the initial velocity for future updation and reference.
u_old = u;
u_initial = u;

%loop starts for the time step integration.
for j = 1:nt(k)  
    
%loop to calculate the velocity at each node at a given time-step.
for i = 2:n
    u(i) = u_old(i) - (c*dt(k)/dx)*(u_old(i)-u_old(i-1));
end

%updating the velocity value to be taken as a reference for the next time step.
u_old = u;

end

%following set of lines are used to plot the velocity profile at the given time-step.
figure(1)

plot(x,u_initial);

hold on;

plot(x,u);
axis([0 1 0 2.1]);
grid on;
xlabel('x');
ylabel('Velocity');
legend(dt);
hold on;

end
legend('Initail Profile','1e-4','1e-3','1e-2','1e-1');

The output plot for the above program is as follows:

Thus the 1D linear convection is solved of the given conditions in 4 different time-steps and the results of the different time-steps are plotted.

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.