function mnhf_stiff1(h,Nmax) %MNHF_STIFF1 illustrates the use of explicit Euler in solving the stiff % non-autonomous ODE % y'=-sin(t)-200*(y-cos(t)) with y(0)=0. % % h - time step % Nmax - number of iterations % % Usage mnhf_stiff1(0.01,50) %% Initialize independent variable array. t=linspace(0.0,h*Nmax,Nmax+1); %% Initialize dependent variable array; specify the initial condition. yEE=zeros(size(t)); yEE(1)=0.0; for ij=1:Nmax %% Calculate solution based on explicit Euler equation. yEE(ij+1)=yEE(ij)+h*( -sin(t(ij))-200.0*( yEE(ij)-cos(t(ij)) ) ); end %% Plot results. figure(1); hold on; box on set(gca,'fontsize',18) plot(t,yEE,'k-','linewidth',2) xlabel('{\it t}'); ylabel('{\it y}') title(['time step, {\it h} = ' num2str(h)]) %% Show exact solution. y=cos(t)-exp(-200.0*t); plot(t,y,'k--') legend('explicit Euler','exact','location','best')