Mod 1 Assign
Mod 1 Assign
clc;clear;close
% L = 1 Normalised length corresponding to L = 1 m
% T0 initial temperatre at x = 0 , t = 0
% Tf final temperature at x = L , t = tf
L=1; % Length on the rod
T=1;% Total time
alpha = 0.01;% Thermal diffusivity constant
% Discretization
Nx=5;% Number of spatial grid points
Nt=15;% Number of time step size
dt=T/Nt; % Time step size
dx=L/Nx;
x= linspace(0,L,Nx+1); % Spatial Grid
t=linspace(0,T,Nt+1); % Time Grid
% Initial condition:u(x,0)=T0 + (TL-T0)*(x/L)^2;
U0=0;
% Initialize solution matrix
U = zeros(Nx,Nt);
U(Nx,:) = 100;% Initial condition
U_new = U;
epsilon = 0.0000001; %
Residual = 1;
while Residual > epsilon
% Finite Difference Mehtod
for n=1:Nt
for i = 2:Nx-1
U_new(i,n+1)=U(i,n)+alpha*dt/(dx)^2*(U(i+1,n)-2*U(i,n)+U(i-1,n));
end
end
% Calculate the error/residual
Residual = sum(sum(abs(U_new(i,n) - U(i,n))));
% Transfer the new values into older variable
U = U_new; % Iterations can take place
end