CHAPTER 9A SURVEY OF NUMERICAL METHODS FOR PARTIAL DIFFERENTIAL EQUATIONS
9.1 DIFFERENCE METHODS FOR THE DIFFUSION EQUATION
Exercises:
- Write a program to use the explicit method to solve the diffusion equation
which has exact solution
(The student should check that this is indeed the exact solution.) Use , and take as large as possible for stability. Confirm that the approximate solution is as accurate as the theory predicts. Compute out to .
Solution: The following MATLAB script will solve the problem. The author makes no claims of being the most sophisticated MATLAB programmer.
clear
n = input('number of points? ')
h = 1/n;
dt = 0.5*h^2;
%
% Index shift since Matlab doesn't allow zero subscripts
%
np = n+1;
x = h*[0:n];
uo = sin(pi*x);
nt = floor(0.1/dt);
disp(nt)
disp('steps')
r = dt/(h*h);
pause
for k=1:nt
t = k*dt;
u(1) = 0;
ue(1) = 0;
for j=2:n
u(j) = uo(j) + r*(uo(j-1) - 2*uo(j) + uo(j+1));
ue(j) = exp(-pi*pi*t)*sin(pi*(j-1)*h);
error(j) = ue(j) - u(j);
end
Get Solutions Manual to accompany An Introduction to Numerical Methods and Analysis, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.