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