The following MATLAB code represents the solution of two first order ODEs:
dy1 = @(x,y1,y2) y2;
dy2 = @(x,y1,y2) 4*x + 10*sin(x) - y1;
%initial values
x0 = pi;
xn = 2*pi;
y1 = 0; % initial value of y1
y2 = 2; % initial value of y2
h = 0.1; % step size
for x = x0 : h : xn-h
L1 = h*dy2(x, y1, y2);
K1 = h*dy1(x, y1, y2);
L2 = h*dy2(x + h/2, y1 + K1/2, y2 + L1/2);
K2 = h*dy1(x + h/2, y1 + K1/2, y2 + L1/2);
L3 = h*dy2(x + h/2, y1 + K2/2, y2 + L2/2);
K3 = h*dy1(x + h/2, y1 + K2/2, y2 + L2/2);
L4 = h*dy2(x + h, y1 + K3, y2 + L3);
K4 = h*dy1(x + h, y1 + K3, y2 + L3);
y2 = y2 + (L1 + 2*L2 + 2*L3 + L4)/6;
y1 = y1 + (K1 + 2*K2 + 2*K3 + K4)/6;
x = x + h;
fprintf ('%f \t %f\t %f\n',x,y2,y1);
end
How can I make this code general so it could easily manage to solve m number of decomposed first order ODEs (i.e., dy1, dy2, ....., dym)?
Many thanks for help.