When implementing the Runge-Kutta order 4 scheme(rk4) u1,…,un are computed using the algorithm:
for i = 0,1,…,n-1
k1 = f (ti, ui),
k2 = f (ti + h/2, ui + h/2*k1),
k3 = f (ti + h/2, ui + h/2*k2),
k4 = f (ti + h, ui + h*k3),
ui+1 = ui + h/6 (k1 + 2k2 + 2k3 + k4).
Would part of this code be written in matlab as the following:
for i = 1:nk1 = h * feval ( f, u(:,i) );k2 = h * feval ( f, u(:,i) + k1/2 ); k3 = h * feval ( f, u(:,i) + k2/2 ); k4 = h * feval ( f, u(:,i) + k3 ); u(:,i+1) = u(:,i) + h*( k1 + 2*k2 + 2*k3 + k4 ) / 6; end;
I'm not too sure if the "h" in the last line (u(:,i+1)) should be there are not.
Any help would be very much appreciated.
Best Answer