% % Applies a Modified Euler (Runge-Kutta) method to: % % y' = f(y,t) y(t0)=y0 % % taking n step sizes of length h. % % w(n+1) = w(n) + h/2 [f(t_n,w_n) + f(t_{n+1},w_n+hf(t_n,w_n))] % % % OUTPUT: vectors (t,Y) where Y(n) is % the approximation of y(t) at % t(n)=t0+(n-1)*h, n=1,2,3,... % % function [t,w]=ModEul(F,h,n,t0,y0) t(1)=t0; w(1)=y0; for i=2:(n+1) k1 = h*feval(F,t(i-1),w(i-1)); k2 = h*feval(F,t(i-1)+h,w(i-1)+k1); w(i)=w(i-1) + (k1 + k2)/2; t(i)=t(i-1)+h; end