Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

#Matlab program on Implicit method

#Crank-Nicolson method
%
Tn=[0 20 40 60 80 100];n=length(Tn);
n1=n-1;Tn1=Tn;
b=2.5*ones(1,n);
c=-0.25*ones(1,n);a=c;a(n)=2*a(n);
r=ones(1,n);
t=0;
fprintf('t = %8.1f, T = %8.2f %8.2f %8.2f %8.2f %8.2f %8.2f\n',t,Tn)
for k=1 : 10
t=t+0.5;
for i=2:n1;
r(i)=0.25*Tn(i-1)+1.5*Tn(i)+0.25*Tn(i+1);
end
r(6)=0.5*Tn(n1)+1.5*Tn(n);
beta=c;gam=c;y=c;
beta(2)=b(2);gam(2)=r(2)/beta(2);
for i=3:n
beta(i)=b(i)-a(i)*c(i-1)/beta(i-1);
gam(i)=(r(i)-a(i)*gam(i-1))/beta(i);
end
Tn1(n)=gam(n);
for j=1:n1-1
Tn1(n-j)=gam(n-j)-c(n-j)*Tn1(n-j+1)/beta(n-j);
end
Tn=Tn1;
fprintf('t = %8.1f, T = %8.2f %8.2f %8.2f %8.2f %8.2f %8.2f\n',t,Tn)
end

OUTPUT

t= 0.0, T = 0.00 20.00 40.00 60.00 80.00 100.00


t= 0.5, T = 0.00 20.00 39.99 59.92 79.18 91.84
t= 1.0, T = 0.00 19.99 39.94 59.59 77.28 86.39
t= 1.5, T = 0.00 19.97 39.82 58.97 75.09 82.31
t= 2.0, T = 0.00 19.92 39.59 58.12 72.89 78.98
t= 2.5, T = 0.00 19.84 39.25 57.12 70.77 76.12
t= 3.0, T = 0.00 19.71 38.82 56.03 68.75 73.58
t= 3.5, T = 0.00 19.54 38.31 54.89 66.83 71.26
t= 4.0, T = 0.00 19.33 37.73 53.72 64.99 69.12
t= 4.5, T = 0.00 19.08 37.11 52.54 63.25 67.12
t= 5.0, T = 0.00 18.80 36.44 51.36 61.57 65.24

You might also like