Linear Convection part 2
⇒Here for n=80 and time steps to use =1e-4, 1e-3, 1e-2, 1e-1 plots can be shown as
⇒Plot comparing initial and final velocity profile for time step 1e-4 is shown below
⇒Plot comparing initial and final velocity profile for time step 1e-3 is shown below
⇒Plot comparing initial and final velocity profile for time step 1e-2 is shown below
⇒Plot comparing initial and final velocity profile for time step 1e-1 is shown below
⇒Plot comparing initial and final velocity profiles for all the time steps is shown below
⇒Code for these plots is given below
clear all
close all
clc
L=1;
n=80; % Number of grid points
x=linspace(0,L,n);
c=1;
dx=x(2)-x(1); %Difference between two grid points
dt=1e-3; %Time Step
% u is velocity
u=1*ones(1,n);
%x_start is when step starts
%x_finish is when step ends
x_start=round((0.1/dx) +1)
x_finish=round((0.3/dx) +1)
u(x_start:x_finish)=2;
u_initial=u; % Initial velocity profile
dt1=1e-4; %First Time step is dt1=1e-4
u1=f(dt1); %u1 is the final velocity profile for time step dt1
dt2=1e-3; %Second Time step is dt2=1e-3
u2=f(dt2); %u2 is the final velocity profile for time step dt2
dt3=1e-2; %Third Time step is dt3=1e-2
u3=f(dt3); %u3 is the final velocity profile for time step dt3
dt4=1e-1; %Fourth Time step is dt4=1e-1
u4=f(dt4); %u4 is the final velocity profile for time step dt4
figure(1)
plot(x,u_initial)
hold on
plot(x,u1,\'r\')
hold on
plot(x,u2,\'g\')
hold on
plot(x,u3,\'k\')
hold on
plot(x,u4,\'m\')
axis([0 1 1 2])
xlabel(\'x-axis\')
ylabel(\'Velocity\')
legend(\'Initial velocity profile\',\'Final velocity profile of u1\',\'Final velocity profile of u2\',\'Final velocity profile of u3\',\'Final velocity profile of u4\')
figure(2)
plot(x,u_initial)
hold on
plot(x,u1,\'r\')
axis([0 1 1 2])
xlabel(\'x-axis\')
ylabel(\'Velocity\')
legend(\'Initial velocity profile\',\'Final velocity profile of u1\')
figure(3)
plot(x,u_initial)
hold on
plot(x,u2,\'r\')
axis([0 1 1 2])
xlabel(\'x-axis\')
ylabel(\'Velocity\')
legend(\'Initial velocity profile\',\'Final velocity profile of u2\')
figure(4)
plot(x,u_initial)
hold on
plot(x,u3,\'r\')
axis([0 1 1 2])
xlabel(\'x-axis\')
ylabel(\'Velocity\')
legend(\'Initial velocity profile\',\'Final velocity profile of u3\')
figure(5)
plot(x,u_initial)
hold on
plot(x,u4,\'r\')
axis([0 1 1 2])
xlabel(\'x-axis\')
ylabel(\'Velocity\')
legend(\'Initial velocity profile\',\'Final velocity profile of u4\')
Program for function f is shown below
function out=f(dt)
L=1;
%Source code for n=20,40,80,160 is same just change the value for n
% Here the program is written for n=2
n=80;
x=linspace(0,L,n);
c=1;
dx=x(2)-x(1);
% u is velocity
u=1*ones(1,n);
%x_start is when step starts
%x_finish is when step ends
x_start=round((0.1/dx) +1)
x_finish=round((0.3/dx) +1)
u(x_start:x_finish)=2;
uold= u;
u_initial=u; % Initial velocity profile
nt=0.4;
% Time Marching solution
for k=1:(nt/dt)
for i= 2:n
u(i)= uold(i) -((c*dx)/dt)*(uold(i)-uold(i-1));
end
uold=u;
end
out=u;
end