Project

Program that computes and compares difference between initial and final velocity profile,where n=20,40,80 and 160.

Program that computes difference between initial and final velocity profile,where n=20,40,80 and 160

clear all
close all
clc

%input for the linear convection problem
L=1;
%Make the comparison for n=20,40,80 and 160
n=[20 40 80 100];
c=0.1;
dt= 1e-2;
%Total time in sec
T= 0.4 ;
% Total number of time steps
nt=T/dt ;
%Calculations
x=linspace(0,L,n(1));
dx = x(2)-x(1);

n_start = n(1)*0.1;
n_stop = n(1)*0.3;
% U is a matrix which consist of set of elements which element is stored at particular index number 
u=0.1*ones(1,n(1));
u(n_start:n_stop) = 0.2;
%BC
%Applying boundary condition
%passing 'u' matrix in a variable called uold
uold = u;
%And also passing u matrix initailly to a variable u_initial
u_initial=u;
%plot(x,u_initial,'r');
%Number of time steps
nt ;
%Time loop
for k=1:nt
  %Space loop
  for i=2:n(1)
    u(i) = uold(i)-(c*dt/dx)*(uold(i)-uold(i-1));
  end
  %Update old velocity uold
  uold = u ;
  subplot(2,2,1);
  %Plotting old velocity along with L=1 and in number of divisions 'n'
  plot(x,u_initial,'r');
  xlabel('x=number of grid points for n=20');
  ylabel('Velocity in m/s');
  hold on;
  %subplot(2,2,1);
  plot(x,u,'b');
  hold off;
  axis([0 1 0 0.4])
  pause(0.03); 

end
hold on;

%For n(2)
x=linspace(0,L,n(2));
dx = x(2)-x(1);

n_start = n(2)*0.1;
n_stop = n(2)*0.3;
% U is a matrix which consist of set of elements which element is stored at particular index number 
u=0.1*ones(1,n(2));
u(n_start:n_stop) = 0.2;
%BC
%Applying boundary condition
%passing 'u' matrix in a variable called uold
uold = u;
%And also passing u matrix initailly to a variable u_initial
u_initial=u;
%plot(x,u_initial,'r');
%Number of time steps
nt ;
%Time loop
for k=1:nt
  %Space loop
  for i=2:n(2)
    u(i) = uold(i)-(c*dt/dx)*(uold(i)-uold(i-1));
  end
  %Update old velocity uold
  uold = u ;
  subplot(2,2,2);
  %Plotting old velocity along with L=1 and in number of divisions 'n'
  plot(x,u_initial,'r');
  xlabel('x=number of grid points for n=40');
  ylabel('Velocity in m/s');
  hold on;
  %subplot(2,2,2);
  plot(x,u,'b');
  hold off;
  axis([0 1 0 0.4])
  pause(0.03); 

end
 hold on;
 
 %Calculations
x=linspace(0,L,n(3));
dx = x(2)-x(1);

n_start = n(3)*0.1;
n_stop = n(3)*0.3;
% U is a matrix which consist of set of elements which element is stored at particular index number 
u=0.1*ones(1,n(3));
u(n_start:n_stop) = 0.2;
%BC
%Applying boundary condition
%passing 'u' matrix in a variable called uold
uold = u;
%And also passing u matrix initailly to a variable u_initial
u_initial=u;
%plot(x,u_initial,'r');
%Number of time steps
nt ;
%Time loop
for k=1:nt
  %Space loop
  for i=2:n(3)
    u(i) = uold(i)-(c*dt/dx)*(uold(i)-uold(i-1));
  end
  %Update old velocity uold
  uold = u ;
  subplot(2,2,3);
  %Plotting old velocity along with L=1 and in number of divisions 'n'
  plot(x,u_initial,'r');
  xlabel('x=number of grid points for n=80');
  ylabel('Velocity in m/s');
  hold on;
  %subplot(2,2,3);
  plot(x,u,'b');
  hold off;
  axis([0 1 0 0.4])
  pause(0.03); 

end
hold on;

x=linspace(0,L,n(4));
dx = x(2)-x(1);

n_start = n(4)*0.1;
n_stop = n(4)*0.3;
% U is a matrix which consist of set of elements which element is stored at particular index number 
u=0.1*ones(1,n(4));
u(n_start:n_stop) = 0.2;
%BC
%Applying boundary condition
%passing 'u' matrix in a variable called uold
uold = u;
%And also passing u matrix initailly to a variable u_initial
u_initial=u;
%plot(x,u_initial,'r');
%Number of time steps
nt ;
%Time loop
for k=1:nt
  %Space loop
  for i=2:n(4)
    u(i) = uold(i)-(c*dt/dx)*(uold(i)-uold(i-1));
  end
  %Update old velocity uold
  uold = u ;
  subplot(2,2,4);
  %Plotting old velocity along with L=1 and in number of divisions 'n'
  plot(x,u_initial,'r');
  xlabel('x=number of grid points for n=160');
  ylabel('Velocity in m/s');
  hold on;
  %subplot(2,2,4);
  plot(x,u,'b');
  hold off;
  axis([0 1 0 0.4])
  pause(0.03); 

end




PLOT:

EXPLANATION:

In the script mentioned above,initial conditions which are mentioned in the problem are taken into account where domain length 'L' is 1 meter,c is 0.1
time-step,'dt'is 1e^-2 ,total time 'T' is 0.4 sec

n is array for number of grid points which contain
values such as 20,40.80 and 160.

Now for every set of grid points main objective is to calculate velocity profile 'U'and is compared with initial velocity profile i.e.'U_initial'

Inorder to calculate 'U' we need to discretise number of grid points suppose for n(1)=20 and to calculate position of each and every point by using linspace command.and we need to create step function as mentioned in the problem.

function,'n_start = n(1)*0.1'
'n_stop = n(1)*0.3' is used to find out the start and stop grid points for velocity of 2m/s in velocity profiles

firstl velocity profile is stored into to two variables i.e. uold and u_initial.

now for each and evcery grid points velocity profile is calulated using for loop which is also known as space loop.

Now space loop is furthur looped inside time loop.

Here 'nt' is the total number of time loop,calulated by dividing total time 'T' by time step,'dt'.

Plotting is done and shown by comparing the profile and appropriate legends are being used.

Same procedure is being followed for the other 3 set of grid point values.

OBSERVATION:

As number of grid points increases,profiles are more sharper and more appropriate square waves are obtained.Best result observed is for n=160

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.