MATLAB: How to solve the differential equation numerically by fourth order Runge-Kutta Method

differential equationsrunge-kutta method

Suppose I have two dependent variable y_1 and y_2 and one independent variable x. The system is [dy_1/dx = y_1 + dy_2/dx; dy_2/dx = -x*dy_1/dx] where y_1(0)=1 and y_2(0)=0 with the range of x=0:0.5:2. This is a replica problem and my actual problem is highly complicated where the explicit forms of dy_1/dx and dy_2/dx in terms of x, y_1 and y_2 are not possible. I am facing problem due to the term dy_2/d_x in the first equation. How does we put the value of dy_2/dx at first step to get the values of y_1 and y_2 at next step and how does the terms dy_1/dx and dy_2/dx vary at each step so that the numerical solution through fourth order Runge-Kutta method would match with the analytical solution of the system? Please help.

Best Answer

Your system can be written in the form
M(x,y)*dy = f(x,y)
where M is a (2x2) matrix.
To apply the usual Runge-Kutta scheme, solve instead
dy = (M(x,y))^(-1)*f(x,y)
where (M(x,y))^(-1) is the inverse matrix of M(x,y).
This method should also be applicable for more complicated systems.
Best wishes
Torsten.
Related Question