Project

MATLAB CODING TO SOLVE THE 1D LINEAR CONVECTION EQUATION

To solve:

one-dimensional linear convection equation using different grid sizes.

Final goal of the challenge:

Comparison between the intial velocity and final velocity profiles of the n-array.

  • Syntax to solve the PDE of 1D-linear convection equation with the data specified in challenge:
  
##Solving the one-dimensional linear convection equation  
  
  clear all   
  close  all
  clc

## Inputs to solve the 1-D linear convection equation 
  
  L=1;                     %Length of the domain             
  n=[20 40 80 160];        %array of grid points  or spatial points
  c=1;                     %linear convection velocity
  dt=1e-2;                 %time step    
  t=0.4;                   %total time
 
##Intializing the x-array for all the given grid points
 
  for i=1:length(n)
 
    x=linspace(0,1,n(i))   %linearly spacing the x-array or creating the mesh  
    dx=x(2)-x(1)           %grid spacing
 
##Intializing arrays of initial velocity and applying boundary conditions

 u=ones(1,n(i))         
      for j=1:length(x)
          if ((x(j)>=0.1)&&(x(j)<=0.3))
                    u(j)=2
            else
                    u(j)=1
          endif
      end 

 u_intial=u;   

 ## time marching solution 
 
 nt=40;             %number of temporal points  
  for t= 1:nt
    u_old=u;

 ## spatial   loop
 
     for k=2:n(i)
       u(k)=u_old(k)-(c*(dt/dx)*(u_old(k)-u_old(k-1))); 
       
 
 end 
 
## the plot commands are given inside the time loop to observe the velocity profile... ...across the mesh for different spatial settings  
 figure(i)
 plot(x,u_intial,'r')
 hold on
 plot(x,u)
 xlabel('length of domain')
 ylabel('velocity')
 pause(0.01)
 grid on 
 axis([0 1 0 2])
 
  end 
 
 end
 

Comparison plots of initial and final velocity profiles for different grid points are depicted below :

Condition(1):

Number of grid points:

n=20

Spatial setting:

x=linspace(0,1,20)

Initial and final velocity comparison of 1D -linear convection equation :

fig(1)

Diffusion across the spatial coordinate:

fig (1.1)

Condition(2):

Number of grid points:

n=40

Spatial setting:

x=linspace(0,1,40)

Initial and final velocity comparison of 1D -linear convection equation :

Diffusion across the spatial coordinate:

Condition(3):

Number of grid points:

n=80

Spatial setting:

x=linspace(0,1,80)

Initial and final velocity comparison of 1D -linear convection equation :

Reduction in diffusion across the spatial coordinate:

Condition(4):

Number of grid points:

n=160

Spatial setting:

x=linspace(0,1,160)

Initial and final velocity comparison of 1D -linear convection equation :

Solution being blown up across the spatial coordinate:

Explanation of velocity profile for different gridpoints:

1)In the first and second plot where the grid points are minimum and grid size is maximum compared to third and fourth plots ,we can observe that the square wave is losing its sharpness and gradients dying down.

2)Here, this drop in peak velocity is because of diffusion phenomenon which is occuring due to the error term from the numerical approximation of PDE.

3)When the grid size(dx) is reduced in third and fourth plots we can observe the velocity profile being covected at 2m/s in the third plot and the same for few moments initially in the fourth plot and later the solution is being blownup due to minimal grid size.

4)So we can reduce the diffusion phenomenon by decreasing the dx value which is directly proportional to the reduction in error value which is inducting this artificial difffusion into the convection.

------------------------------------------------------------------------------------------------------------------

Code showing the effect of time step on 1-D linear convection velocity

To solve

One-dimensional linear convection equation using different time steps

Final goal of the challenge

Comparison between the intial and final velocity profiles when different time steps are taken to reach a total time of 0.4 seconds.

  • Syntax to solve the PDE of linear convection equation with the data and conditions specified in challenge
clear all
close all
clc

##inputs and boundary conditions 

##Array of time_steps
dt=[1e-4 1e-3 1e-2 1e-1];

##for loop is used to output the velocity profile at different time_step value

for i=1:length(dt)
  
  tic                 ##timer was set to measure the simulation time for different time_step values
  
l=1;                  ##domain length

n=80;                 ##Number of grid points

x=linspace(0,1,n);    ##spatial setting

u=ones(1,n);          ##intialization of velocity array

x_start=0.1;          ##square profile starts at 

x_stop=0.3;           ##square  profile ends at

n_start=9;            ##location of the start point on grid

n_stop=24;            ##location of stop point on grid   

t=0.4;                ##total time

c=1;                  ##linear convection velocity

dx=x(2)-x(1);         ##spacing of grid points

u(n_start:n_stop)=2;  ##applying the boundary conditions

u_old=u;              

u_intial=u;

 nt(i)=0.4./dt(i);     ##total time=number of time_steps*time_step
## time marching solution
   for j=1:nt(i)
     u_old=u;
## spatial loop
     
     for k=2:n
        u(k)=u_old(k)-(c*dt(i)/dx)*(u_old(k)-u_old(k-1));
      end
 
  end
 

##  Displaying the output in plots
    figure(i)
    plot(x,u_intial)
    hold on
    plot(x,u,'linewidth',3)                     
    hold on
    axis([0 1 1 2]);
    pause(0.1)
    grid on
    
    xlabel('location','fontsize',25)
    ylabel('velocity','fontsize',25)
    legend( sprintf('u-initial, dt = %d', dt(i)), sprintf('u-final, dt = %d', dt(i)) );
    title('Velocityprofile for different TimeSteps','fontsize',15)
    simulation_time(i)=toc                      ##elapsed time for each time value                                      
   
   
 
 end 

Comparison plots of intial and final velocity profiles when the solution marches along through time with different time step size:

Condition(1):

dt=1e-4

FIG 1.0

Condition(2):

dt=1e-3

FIG 2.0

Condition(3):

dt=1e-2

FIG 3.0

Condition(4):

dt=1e-1

FIG 4.0

Summary:

The profile is smooth and seen diffusing in the first two plots(i.e.,dt=[1e-4 1e-3]) due to low size of the time step where as in the latter two plots(i.e., dt=[1e-2 1e-1]) the profile travels with the same velocity and almost similar in the last profile.

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.