clear all;
close all;
Delta_t=2.0;
t=0:Delta_t:8.0;
%Preallocating memory
x=zeros(size(t));
%Initial condition
x(1)=0.0;
for n=1:length(t)-1
x(n+1)=((1-Delta_t/2)*x(n)+Delta_t)/(1+Delta_t/2);
end
plot(t,x,’ko-‘,’linewidth’,2,’Markersize’,10)
hold on
tplot=0:0.01:8.0;
truesolution=@(t)(1-exp(-t))
plot(tplot,truesolution(tplot),’b-‘,’linewidth’,2)
xlabel(‘t’);
ylabel(‘x’);
legend(‘Crank-Nicolson’,’True’);
axis([0 8 0 2])