Beruflich Dokumente
Kultur Dokumente
Handout 1
1D Advection-Diffusion MATLAB Code and Results
time = 0.0;
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
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;
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:
2
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert
Solution 2:
Solution 3:
Solution 4:
3
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert
Solution 5:
Solution 6:
𝑁 = 41 (Δ𝑥 = 0.05), 𝚫𝒕 = 𝟎. 𝟎𝟔
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:
5
METU, Dept. of Mechanical Eng.
ME 705, Fall 2016, Dr. C. Sert
Solution 8:
Solution 9: