% % Applies a 2nd order Runge-Kutta method to: % % y' = f(y,t) y(t0)=y0 % % taking n step sizes of length h. % % w(n+1) = w(n) + h f(t(n)+h/2,w(n)+h/2*f(t(n),w(n))) % % Specifically,this is the Modified Euler-Cauchy Rule % (or the Midpoint Method as it is sometimes called) % % INPUT: F string variable containing % the name of the function file % where f(t,y) is stored. % h step size % n number of steps % t0 initial value of the t-variable % y0 initial value of the y-variable % % % OUTPUT: vectors (t,w) where w(n) is % the approximation of y(t) at % t(n)=t0+(n-1)*h, n=1,2,3,... % % function [t,w]=RK(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=feval(F,t(i-1)+h/2,w(i-1)+k1/2); w(i)=w(i-1)+h*k2; t(i)=t(i-1)+h; end