Linear Convection Effect of Mesh points
LinearConvection_main.m
clear all
close all
clc
for n=[20, 40, 80, 160] %Looping over List of Grid Points
%Inputs for linear convection problem
L = 1;
c = 1;
dt = 0.01; %Time Step
nt = 0:dt:0.4; %No of Time Steps
x = linspace(0,L,n);
dx = L /(n-1);
%start and end of square wave
x_start = 0.1;
x_stop = 0.3;
n_start = nearest_value(x,x_start);
n_stop = nearest_value(x,x_stop) ;
%Initialize arrays and boundary conditions
u = 0.1 * ones(1, n);
u(n_start:n_stop) = 0.2;
uold = u;
u_initial = u;
%Time loop
for k=1:length(nt) %where nt is num of time steps
%Space loop
for i=2:n
u(i) = uold(i) - c*(dt/dx) * (uold(i) - uold(i-1));
endfor
%update old velocities
uold = u;
%Plotting the velocities
#plot(x, u_initial, \'r\');
#hold on;
t1 = plot(x, u,\'b\') ;
xlabel(\'Length of Domain\')
ylabel(\'Velocity\')
axis([0 5 0 0.3]) ;
pause(0.003)
endfor
baseFileName = sprintf(\'%d_GridPoints_LinearConvetion.png\', i);
legend_string = sprintf(\'%d Grid Points\', i);
legend(legend_string);
saveas(t1, baseFileName, \'png\');
endfor
nearest_value.m
function output = nearest_value(x, value_under_test)
output = find(x - value_under_test > 0 & x - value_under_test < 0.1, 1);
endfunction
Plots:
Explanation:
As seen in the plots the solution is slowly diffusing because of artificial diffusion or false diffusion. So, if we make the mesh finer like in case of n=40 and n=80, the diffusivity decreases and the curve maintains the original velocity profile.
However, the solution blows up after n=100 because of which the plot for n=160 has ringing effects around the points where the function is discontinuous.