Project

Linear Convection for varing grid points

Numerical Solution to the wave equation.

Below is the function for the plot

% function for numerical solution
function out=simple_linear_convection(n,h)

%clear all
 %close all
 %clc
 
% Inputs for the linear convention 
L=1;
c=1;
dt =1e-2;
t_end = 0.4;
nt = (0.01:dt:t_end); 
%n =20;
%calculation

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

% Define the start and end positions for the square wave
 x_start = 0.1;
 x_end = 0.3;
 
% n_start =f(x_start)
% n_end = f(x_end)

n_start = func_start(x,x_start); %Function to calculate n_start
n_stop =  func_end(x,x_end);   %Function to calculate n_stop

% Intialize arrays and apply boundary conditions
u = ones(1,n);
u(n_start:n_stop)= 2; %Velocity profile = 2m/s between 0.1 and 0.3
%BC
u(1) =1;
 uold =u;
 u_initial =u;

 
 
 %Time loop
 
 for k =1:length(nt)
     
     % space loop
     % du/dt = -c*du/dx
     % du/dx - FDS,CDS, BDS, skewed schemes
     for i =2:n
         u(i) = uold(i)-(c*dt/dx)*(uold(i) - uold(i-1)); % FDS for space loop
     end
     
     % Update old velocitites
     uold = u;
     
     %Plotting the velocties
     
     % h is the figure number.
     
  figure(h)
     g1 = plot(x, u_initial,\'r\',\'marker\',\'o\',\'MarkerFacecolor\',\'c\',\'linewidth\',2);
     hold on 
     g2 = plot(x,u,\'b\',\'marker\',\'*\',\'MarkerFaceColor\',\'b\',\'linewidth\',1);
     grid minor
     axis ([0 1 0 3])
     xlabel(\'Grid points\');
     ylabel(\'Veloctiy\');
     legend(\'Intital Velocity Profile\',\'Final Velocity Profile\');
     caption = [\'Velocity profile for number of grids = \', num2str(n)];
     title(caption);
     hold on
  pause(0.05);
  
 
 end
 
 %Saving the plots
 
 temp=[\'Velocity profile when grid points are \',num2str(n),\'.png\']; 
 saveas(gca,temp);
 
 out=figure();
 


 

Functions to find values of start and end positions of wave.

function out = func_start(x,x_start)
 
for i = 1:length(x);

Z(i) = abs(x_start-x(i)); %Z is the difference

end


[num,out] = min(Z);

end
function out = func_start(x,x_start)
 
for i = 1:length(x);

Z(i) = abs(x_start-x(i)); %Z is the difference

end


[num,out] = min(Z);

end

Script for the program

clear all
close all
clc

figure(1)

number_of_grid_20 = simple_linear_convection(20,1);


figure(2)

number_of_grid_40 = simple_linear_convection(40,2);

figure(3)

number_of_grid_60 = simple_linear_convection(80,3);


figure(4)

number_of_grid_160 = simple_linear_convection(160,4);




Rsults are as follows:

Above plot shows the velocity profile for the numerical solution of wave equation when number grid is 20.

1. The initial velocity profile is shown by the red line and evolution of velocity by blue line

2. We can observe that as profile progresses the peak velocity drops and the wave is getting diffused due to a phenomenon know as artificial diffusion.

Above plot shows the velocity profile for the numerical solution of wave equation when number grid is 40.

1. The initial velocity profile is shown by the red line and evolution of velocity by blue line

2. We can observe that as profile progresses the peak velocity drops and the wave is getting diffused due to a phenomenon know as artificial diffusion.

3. As compared to the plot where grid number is 20 in this plot peak velocity drops at lower rate and profile is sharper.

Above plot shows the velocity profile for the numerical solution of wave equation when number grid is 80.

1. The initial velocity profile is shown by the red line and evolution of velocity by blue line

2. We can observe that as profile progresses there is not much difference between the initial velocity profile and final velocity profile and also we cannot observe any artificial diffusion. So this solution is very stable.

Above plot shows the velocity profile for the numerical solution of wave equation when number grid is 160.

1. The initial velocity profile is shown by the red line and evolution of velocity by blue line

2. We can observe that as profile is completely distorted and the solution is unstable.

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.