4th order RUNGE KUTTA METHOD for more that 3 coupled equations in MATLAB -
can please tell me how implement 4th order runge kutta method more 3 coupled equations in matlab.
regards
faiz
if have version vector valued ode 1 dimension, have dimensions. (syntax may have corrected errors, works under scilab, close, not idential matlab)
function [t, y] = rk4(odefunc, y0, t0, tf, n) t = tf-t0; h=t/n; time=t0; state=y0; t = zeros(n+1); y = zeros(n+1,length(y0)); t(1) = t0; y(1,:) = y0; i=2:n+1 k1 = odefunc(time , state ); k2 = odefunc(time + 0.5*h, state + 0.5*h*k1); k3 = odefunc(time + 0.5*h, state + 0.5*h*k2); k4 = odefunc(time + h, state + h*k3); state = state + (h/6)*(k1+2*k2+2*k3+k4); time = time + h; t(i) = time; y(i,:) = state; end endfunction function doty = lorenz(t, y, params) a=params(1); b=params(2); c=params(3); doty = [ * ( y(2) - y(1) ) -b * y(1) + y(1) * y(3) -c * y(1) - b * y(3) ]' endfunction function doty=odefunc(t,y) doty = lorenz(t,y,[0.5, 0.66, 1.4]) endfunction [t,y] = rk4(odefunc, [-1.2 0.2 0.8], 0, 10, 400)
Comments
Post a Comment