Sie sind auf Seite 1von 6

# METU, Dept. of Mechanical Eng.

## ME 705, Fall 2016, Dr. C. Sert

Handout 1
1D Advection-Diffusion MATLAB Code and Results

## % Based on Tryggvason's 2013 Lecture 2

This code will be available
% 1D advection-diffusion solution at the course web site.
clc % Clear the command window
close all % Close all previously opened figure windows
play with it.
clear all % Clear all previously generated variables

## N = 41; % Number of nodes

nStep = 27; % Number of time levels
L = 2.0; % Domain length
dx = L/(N-1); % Node spacing
dt = 0.06; % Time step
D = 0.05; % Diffusion coefficient
A = 1; % Amplitude of the initial sine wave
k = 1; % Frequency of the initial sine wave
U = 1; % Advection speed

## f = zeros(N,1); % Calculated solution at the new time level (level n+1)

fOld = zeros(N,1); % Calculated solution at the previous time level (level n)
exact = zeros(N,1); % Exact solution

time = 0.0;

## % Use the given initial condition

for j = 1:N % Node counter
x = dx*(j-1); % x coordinate of node i
fOld(j) = A * sin(2*pi * k * x);
end

## fprintf('Watch the progress of the solution in the figure window.\n\n')

fprintf('Press Ctrl-C in the command line window to terminate the code.\n\n')

## for n = 1:nStep % Time loop. n is the time level counter

time = time+dt

for j = 1:N
x = dx*(j-1);
exact(j) = exp(-(2*pi*k)^2 * D * time) * ...
A * sin(2*pi*k * (x - U * time)); % Exact solution of the current time level
end

% Calculate the solution of this time level, i.e. f_n+1, for inner points
for j = 2:N-1
f(j) = fOld(j) - U*dt/(2*dx) * (fOld(j+1) - fOld(j-1)) + ...
D*dt/dx^2 * (fOld(j+1) - 2*fOld(j) + fOld(j-1));
end

## % Calculate the solution of the last point (Right boundary)

f(N) = fOld(N) - U*dt/(2*dx) * (fOld(2) - fOld(N-1)) + ...
D*dt/dx^2 * (fOld(2) - 2*fOld(N) + fOld(N-1));

## f(1) = f(N); % Due to the periodic BC

Continues at the back . . .
1
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert

hold off;
plot(f, 'linewidth',2); % Plot the calculated solution
grid on;
axis([1 N -1.0, 1.0]);
xlabel('Node number');
ylabel('f');
title('1D Advection-Diffusion Solution with Periodic BCs')

hold on;
plot(exact,'r--','linewidth',2); % Plot exact solution of the previous time step
legend('Numerical','Exact');
pause(0.01);

% Calculate L2 error
Error = dx * sqrt(sum((f - exact).^2))

% Get ready for the new time level before the loop ends
fOld = f;

## fprintf('Done. The code is terminated successfuly.\n\n')

Following are the solutions of the 1D adv-diff equation studied in Chapter 1. Following parameters are used for all
the solutions.

𝐿 = 2, 𝐴 = 1, 𝑘 = 1, 𝑈 = 1, 𝐷 = 0.05

Solution 1:

plotted.

## Clear difference between the solutions.

2
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert

Solution 2:

plotted.

Solution 3:

plotted.

solution.

Solution 4:

plotted.

## Halving Δ𝑡 one more time resulted in very little

improvement. Looks like that the error is now
space discretization dominated and the 21
node grid can provide only this good.

3
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert

Solution 5:

plotted.

## This time we doubled the number of nodes

and the solution is very close to the exact
solution.

Solution 6:

𝑁 = 41 (Δ𝑥 = 0.05), 𝚫𝒕 = 𝟎. 𝟎𝟔

plotted.

## Here we try to demonstrate an unstable

solution. Oscillations start to appear. The
solution is not good.

(𝑡 = 1.62).

## Oscillations grow unboundedly and they are

out of control. The solution is unstable and will
blow up if we continue.

## For the selected problem parameters (𝑈 and

𝐷) and Δ𝑥 = 0.05, the used Δ𝑡 = 0.06 exceeds
the critical time step.

4
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert

Handout 1 (cont’d)

Spatial convergence analysis: Δ𝑡 is fixed at 10−4 and the number of points is varied between N=11 and N=641.
Totally 7 runs were made. Result for N=641 is not shown below (Note: Following figure is different than the one in
the printed version distributed in class. I already sent you an email about this change).

Line with
slope 2

Solution 7:

BTCS scheme.

## “Solution 6” with FTCS was unstable. Now

BTCS is stable. However, the solution is not
accurate. Although BTCS has no Δ𝑡
restriction, we still need to select a low
enough value due to accuracy concerns. For
example decrease Δ𝑡 10 times and run again.

5
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert

Solution 8:

## Same parameters as “Solution 5” are used,

but with the upwind scheme.

## The solution is excessively diffuse (as if

diffusivity is higher than 0.05). Low order
upwinding has this general problem. It
smoothens the solution excessively.

Solution 9:

## Same parameters as “Solution 8” are used,

but with 2nd order upwind scheme.

## Compared to “Solution 8” it is much better.

Compared to “Solution 5” it has a bit more
phase error (as if the wave is not moving at
the correct speed).