%% Calculate Solution of Heat Equation with Initial Condition
% Where u(x,t) represents the temperature at position x and time t, the heat equation is
% \frac{\partial u}{\partial t} = c\frac{\partial^2 u}{\partial x^2},
% where c is a constant.
% For a material with heat coefficient k, and for the initial condition u(x,0) = a for x > b and u(x,0) = 0 elsewhere, the solution to the heat equation is
% u(x,t) = \frac{a}{2} \biggl(\rm erf \biggl( \frac{x-b}{\sqrt{4kt}}
% \biggr) \biggr).
% For k = 2, a = 5, and b = 1, plot the solution of the heat equation at times t = 0.1, 5, and 100.

x = -4:0.01:6;
t = [0.1 5 100];
a = 5;
k = 2;
b = 1;
figure(1)
hold on
for i = 1:3
    u(i,:) = (a/2)*(erf((x-b)/sqrt(4*k*t(i))));
    plot(x,u(i,:))
end
grid on
xlabel('x')
ylabel('Temperature')
legend('t = 0.1','t = 5','t = 100','Location','best')
title('Temperatures across material at t = 0.1, t = 5, and t = 100')

% Copyright 2015 The MathWorks, Inc.