% Usage: y = heunstep(f,a,b,ya,n) % One step of Heun's (or 'explicit trapezoid') method 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=heunstep(@myfunc,0,1,0.1); here 'myfunc' is a user-defined function in M-file % y=heunstep(inline('sin(y*t)','t','y'),0,1,0.1); % f=inline('sin(y(1))-cos(y(2))','t','y'); % y=heunstep(f,0,1,0.1); function y = heunstep(f,t,x,h) g = f(t,x); z = x + h * g; y = x + 0.5 * h * ( g + f(t+h,z) );