Dynamical systems 319
30
31
32
33 function [xout,yout] = ivp RK4(x0,y0,n,h,i out)
34 % x0 = initial value for x
35 % y0 = initial value for y
36 % n = number of steps to take
37 % h = step size
45 for i = 1:n
46 k1 = getf(x,y);
47 k2 = getf(x + 0.5*h,y + 0.5*h*k1);
48 k3 = getf(x + 0.5*h,y + 0.5*h*k2);
49 k4 = getf(x + h, y + h*k3);
55 yout(count,:) = y;
56 end
57 end
58
59 function out = getf(x,y)
The program uses RK4 to make the phase plane for points in the interval [−2,2]
for both variables. This is sufficient to solve the problem. There is nothing spe-
cial that you need to do to implement RK4, so you do not need to provide any
equations here. All you needed to do was set the equation to
and use the standard algorithm for systems of equations.
The output file is