% RK4PLOT.M % This estimates and plots numerical solutions to an ODE using a 4th order % Runge-Kutta method. It calls the function RK4.M. Both M-files get initial % information from INITN.M. cla axis(initax); tmatr=[]; xmatr=[]; tsize=[]; tvec=[]; xvec=[]; tmax=t0; xmin=x0; xmax=x0; tspan=[t0,tf]; for j=1:max(size(hvec)) [t,x]=rk4(ftx,tspan,x0,hvec(j)) t=t'; x=x'; if min(x)>-inf if xmin>min(x) xmin=min(x); end end if max(x)max(size(t)) t=[t,zeros(1,v-max(size(t)))]; else tmatr=[tmatr,zeros(u,max(size(t))-v)]; end tmatr=[tmatr;t]; [u,v]=size(xmatr); if v>max(size(x)) x=[x,zeros(1,v-max(size(x)))]; else xmatr=[xmatr,zeros(u,max(size(x))-v)]; end xmatr=[xmatr;x]; if j==minhindex tmax=max(t); tvec=t; xvec=x; end end axis([t0,tf,xmin,xmax]); ax=axis; plotlabel=['RK4: x'' = ',ftx,', h = [',num2str(hvec),']']; slopef(ftx,N); %hold on for j=1:max(size(hvec)) plot(tmatr(j,1:tsize(j)),xmatr(j,1:tsize(j))) end xlabel(plotlabel) hold off ax=[t0,tf,xmin,xmax]; clear y