Using the Runge-Kutta integration method in a system

x = 0:h:40;                                         
y = zeros(1,length(x)); 
y(1) = 0;                                         
F_xy = ;                    

for i=1:(length(x)-1)                              
    k_1 = F_xy(x(i),y(i));
    k_2 = F_xy(x(i)+0.5*h,y(i)+0.5*h*k_1);
    k_3 = F_xy((x(i)+0.5*h),(y(i)+0.5*h*k_2));
    k_4 = F_xy((x(i)+h),(y(i)+k_3*h));

    y(i+1) = y(i) + (1/6)*(k_1+2*k_2+2*k_3+k_4)*h;  

I have the following code, I think it's right. I know there's parts missing on the F_xy because this is my follow up question.

I have dx/dt = = −x(2 − y) with t_0 = 0, x(t_0) = 1

and dy/dt = y(1 − 2x) with t_0 = 0, y(t_0) = 2.

My question is that I don't know how to get these equations in to the code. All help appreciated

1 answer

  • answered 2018-03-14 03:20 Argyll

    Is F_xy your derivative function?

    If so, simply write it as a helper function or function handle. For example,


    Also note that your k_1, k_2, k_3, k_4, y(i) are all two-dimensional. You need to re-size your y and rewrite the indices in your iterating steps accordingly.