echo off;
% NUMERICAL METHODS: MATLAB Programs, (c) John H. Mathews 1994
% To accompany the text:
% NUMERICAL METHODS for Mathematics, Science and Engineering, 2nd Ed, 1992
% Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A.
% This free software is complements of the author.

% Algorithm 10.3 (Crank-Nicholson Method for the Heat Equation).
% Section 10.2, Parabolic Equations, Page 517
echo on; clc; format short; hold off; clear

% PARABOLIC EQUATIONS.

% Crank-Nicholson solution for the heat equation

% 2
% u (x,t) = c u (x,t)
% t xx

% u(x,0) = f(x) for 0 < x < a and

% u(0,t) = g1(t) and u(a,t) = g2(t) for 0 ² t ² b

% A numerical approximation is computed over the rectangle

% 0 ² x ² a , 0 ² t ² b.

pause % Press any key to continue.

clc;
% Store f(x), g1(x), g2(x) in f.m g1.m g2.m
% function z = f(x)
% z = sin(pi*x) + sin(3*pi*x);

% function z = g1(t)
% z = 0;

% function z = g2(t)
% z = 0;

delete f.m
diary f.m; disp('function z = f(x)');...
disp('z = sin(pi*x) + sin(3*pi*x);');...
diary off;

delete g1.m
diary g1.m; disp('function z = g1(t)');...
disp('z = 0;');...
diary off;

delete g2.m
diary g2.m; disp('function z = g2(t)');...
disp('z = 0;');...
diary off;

% Remark. f.m g1.m g2.m crnich.m trisys.m are used for Algorithm 10.3
f(0); g1(0); g2(0);
pause % Press any key to continue.

clc;
% Place the endpoint of [0,a] in a.

% Place the endpoint of [0,b] in b.

% For the heat equation, enter the constant c.

% Over [0,a] enter the number of grid points n.

% Over [0,b] enter the number of grid points m.

a = 1.0;
b = 0.1;
c = 1;
n = 11;
m = 11;

U = crnich('f','g1','g2',a,b,c,n,m);

pause % Press any key to see the solution.

Z = fliplr(U);
mesh(Z);...
Mx1 = 'The Crank-Nicholson solution to the heat equation.';...
title(Mx1);...
shg; pause % Press any key to continue.

W = U';
points = W(:,2:n-1);
clc,echo off,diary output,...
disp(' '),disp(Mx1),disp(' '),disp(points),...
diary off,echo on