% Usage: y = rk4step(f,t,x,h) % One step of Runge-Kutta method of order 4 for initial value problems % % Input: % f - Matlab inline function f(t,y) % t - initial time % x - initial condition % h - stepsize % % Output: % y - computed solution at t+h % % Examples: % y=rk4step(@myfunc,0,1,0.1); here 'myfunc' is a user-defined function in M-file % y=rk4step(inline('sin(y*t)','t','y'),0,1,0.1); % f=inline('sin(y(1))-cos(y(2))','t','y'); % y=rk4step(f,0,1,0.1); function y = rk4step(f,tx,h) halfh = h / 2; s1 = f(t, x); s2 = f(t + halfh, x + halfh * s1); s3 = f(t + halfh, x + halfh * s2); s4 = f(t + h, x + h * s3); y = x + (s1 + 2 * s2 + 2 * s3 + s4) * h / 6