function y_2 = odeStep_rk4(x_1,y_1,h,deriv) %y_2 = odeStep_rk4(x_1,y_1,h,deriv); % A self-starting, simple, single Runge-Kutta 4th-order step function % x_1 --> initial independent variable (scalar, typically time) % y_1 --> initial dependent variable (vector) % h --> step size in x %deriv --> function handle for derivative function (annonymous call % if derivatives are functions of parameters other than x & y) % y_2 <== final dependent variable; i.e., y(x_1+h) % (c) 2007 Curt A. L. Szuberla % University of Alaska Fairbanks, all rights reserved % ver 1.01 % 21 December 2007 % implementation of standard Runge-Kutta 4th-order formula k_1 = h * deriv(x_1,y_1); % step w/ slope at start k_2 = h * deriv(x_1 + h/2 , y_1 + k_1/2); % step w/ slope in middle k_3 = h * deriv(x_1 + h/2 , y_1 + k_2/2); % same but using second result k_4 = h * deriv(x_1 + h , y_1 + k_3); % step w/ slope at end y_2 = y_1 + (k_1 + 2*k_2 + 2*k_3 + k_4)/6; % weighted avg. of steps return