% 1-D time dependent heat transfer
% model dT/dt = k*d2T/dx2
% arbitrary initial coditions with 
% Dirichlet boundary conditions - with dissapation along surface of wire
% heat transfer through an insulated wire of lenght 1, with one end held at T0 degrees 
% and the other at T1 degrees

k = 1;
a = 0.1;
Tambient = 100;

T = zeros(11,1);    % the estimated temperature values
To = zeros(11,1);    % old estimated temperature values   

BT0 = 0; % boundary temperature at x = 0
BT1 = 100; % boundary temperature at x = 1

dx = 1; % the grid points are 0, .1, .2 .3 .4 .5 .6 .7 .8 .9 1
         % note that there are 11 points in the grid numbered 1 2 3 .... 11
xi=0:dx:10;

dt = .1;  % the time subinterval lenght
N = 50;  % number of iterations


% set initial conditions

T = [0 1 2 3 4 5 6 7 8 9 10];

plot(xi,T);
hold on

for n=1:N
 
    To = T;
    for i=2:10
        T(i) = To(i) + dt*(To(i+1) -2*To(i) + To(i-1))/dx^2 -a*(To(i) - Tambient);
    end
    
    
    if mod(n,10)==0
        plot(xi,T)
    end
end



xlabel('x')
ylabel('T')
hold off



