Project

linear convection equation: To solve 1D linear convection equation for different time steps using MATLAB coding

Aim: To plot velocity profiles at t=0.4 seconds for different values of time step i.e. 1e-1,1e-2,1e-3,1e-4.

To write a MATLAB code and solve 1D linear equation for different time steps.

Given, Conditions are as follows:

Convective coefficient =1

Domain length = 1m

Time step = (1e-1,1e-2,1e-3,1e-4)

Grid points, n = 80

Total simulation time = 0.4 sec

Initial velocity profile, x =0.1 to 0.3 is u = 2m/s elsewhere the velocity is 1m/s.

Solution:

The matlab code to determine the numerical solution at t=0.4 seconds for different time steps is a given below:

function code:

function [u,x] = time_space_loop(dt)
%defining_parameters
l=1;
n=80;
x=linspace(0,1,n);
dx=x(2)-x(1);
c=1;
t=0.4;
nt=t/dt;
%solution_initialization @t=0
u=1*ones(1,n);
%from x=0.1 to 0.3 ,u=2
x_start=0.1;
x_stop=0.3;
n_start=round((x_start/dx)+1);
n_stop=round((x_stop/dx)+1);
u(n_start:n_stop)=2;
init=u;
uold=u;
for j=1:nt
    for i=2:n
    u(i)=uold(i)+(((c*(dt)/(dx)))*(uold(i-1)-uold(i)));
    end
    uold=u;
end
CFL_number=c*(dt/dx)
end

main program:

clear all
close all
clc
%To calculate solution time for 1e-1
tic 
[u0,x0]=time_space_loop(0.1);
toc
solution_time1=toc;
%To calculate solution time for 1e-2
tic
[u1 ,x1]=time_space_loop(0.01);
toc
solution_time2=toc;
%To calculate solution time for 1e-3
tic
[u2, x2]=time_space_loop(0.001);
solution_time3=toc;
%To calculate solution time for 1e-4
tic
[u3, x3]=time_space_loop(0.0001);
solution_time4=toc;
figure(1)
plot(x0,u0,'b','linewidth',1)
hold on
plot(x1,u1,'g','linewidth',1)
hold on
plot(x2,u2,'k','linewidth',1)
hold on
plot(x3,u3,'m','linewidth',1)
grid on
axis([0 1 0 2.5])
legend('velocity at t=0.4 and dt=0.1','velocity at t=0.4 and dt=0.01','velocity at t=0.4 and dt=0.001','velocity at t=0.4 and dt=0.0001')
xlabel('x values')
ylabel('velocity values')
title('comparision for different time steps')

%bar graph of time step vs solution time
figure(2)
y=[solution_time1 solution_time2 solution_time3 solution_time4];
bar(y,0.3,'k')
ylabel('Solution time')
legend('Comparision of solution time with time step')

From the above figure 3 following conclusions can be obtained:

1) The solution time required at dt =0.1 is way higher than solution time at dt=0.01 and dt=0.001 which is due to the unstable result observed at dt=0.1.

2) The solution time increases with decrease in the time step size this is observed at dt =1e-2,1e-3,1e-4 respectively. In other words with smaller time step the time required to solve the given problem increases.

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.