% Solve u'(t)=lambda u, with u(0)=1 % Exact solution is u(t)=exp(lambda t) % Use theta method with different values of theta uexact=@(t,lambda) exp(lambda*t); % first run with N=10 and lambda=1 N=10; lambda=1; figure(1), clf fplot(@(t) uexact(t,lambda),[0,1],'b','linewidth',2), hold on % explicit Euler [tn,Uexp]=theta_method1(lambda,N,0); % implicit Euler [tn,Uimp]=theta_method1(lambda,N,1); % Crank Nicolson [tn,Ucn]=theta_method1(lambda,N,0.5); plot(tn,Uexp,'r',tn,Uimp,'g',tn,Ucn,'k','linewidth',2) xlabel('t','fontsize',20), ylabel('u','fontsize',20) legend('exact solution','explicit Euler','implicit Euler','Crank Nicolson','location','NorthWest','fontsize',20) title(['Solution with N=',num2str(N),' and lambda=',num2str(lambda)],'fontsize',20) % exact solution at grid points Ue=uexact(tn,lambda); figure(2),clf plot(tn,(Uexp-Ue),'r',tn,(Uimp-Ue),'g',tn,(Ucn-Ue),'k','linewidth',2) xlabel('t','fontsize',20), ylabel('error','fontsize',20) legend('error in explicit Euler','error in implicit Euler','error in Crank Nicolson','location','NorthWest','fontsize',20) title(['Error with N=',num2str(N),' and lambda=',num2str(lambda)],'fontsize',20) % repeat but with N=100 and lambda=1 N=100; lambda=1; figure(3), clf fplot(@(t) uexact(t,lambda),[0,1],'b','linewidth',2), hold on % explicit Euler [tn,Uexp]=theta_method1(lambda,N,0); % implicit Euler [tn,Uimp]=theta_method1(lambda,N,1); % Crank Nicolson [tn,Ucn]=theta_method1(lambda,N,0.5); plot(tn,Uexp,'r',tn,Uimp,'g',tn,Ucn,'k','linewidth',2) xlabel('t','fontsize',20), ylabel('u','fontsize',20) legend('exact solution','explicit Euler','implicit Euler','Crank Nicolson','location','NorthWest','fontsize',20) title(['Solution with N=',num2str(N),' and lambda=',num2str(lambda)],'fontsize',20) % exact solution at grid points Ue=uexact(tn,lambda); figure(4),clf plot(tn,(Uexp-Ue),'r',tn,(Uimp-Ue),'g',tn,(Ucn-Ue),'k','linewidth',2) xlabel('t','fontsize',20), ylabel('error','fontsize',20) legend('error in explicit Euler','error in implicit Euler','error in Crank Nicolson','location','NorthWest','fontsize',20) title(['Error with N=',num2str(N),' and lambda=',num2str(lambda)],'fontsize',20) % final run with N=10 and lambda=-25 N=10; lambda=-25; figure(5), clf fplot(@(t) uexact(t,lambda),[0,1],'b','linewidth',2), hold on % explicit Euler [tn,Uexp]=theta_method1(lambda,N,0); % implicit Euler [tn,Uimp]=theta_method1(lambda,N,1); % Crank Nicolson [tn,Ucn]=theta_method1(lambda,N,0.5); plot(tn,Uexp,'r',tn,Uimp,'g',tn,Ucn,'k','linewidth',2) xlabel('t','fontsize',20), ylabel('u','fontsize',20) legend('exact solution','explicit Euler','implicit Euler','Crank Nicolson','location','NorthWest','fontsize',20) title(['Solution with N=',num2str(N),' and lambda=',num2str(lambda)],'fontsize',20) % exact solution at grid points Ue=uexact(tn,lambda); figure(6),clf plot(tn,(Uexp-Ue),'r',tn,(Uimp-Ue),'g',tn,(Ucn-Ue),'k','linewidth',2) xlabel('t','fontsize',20), ylabel('error','fontsize',20) legend('error in explicit Euler','error in implicit Euler','error in Crank Nicolson','location','NorthWest','fontsize',20) title(['Error with N=',num2str(N),' and lambda=',num2str(lambda)],'fontsize',20)