I'm trying to implement RK algorithm applied to Van Der Pol equation. I tried this code
y = zeros(n, 2);
y(1, 1) = y0(1);
y(1, 2) = y0(2);
% Van der Pol equation
% y''(t) - (1 - y^2(t)) * y'(t) + y(t) = 0;
% y'(t) = y(2)
% y''(t) = (1-y(1)^2)*y(2)-y(1)
for i = 1:n-1
k1 = h*(y(i,2));
k2 = h*(y(i,2)+0.5*k1);
k3 = h*(y(i,2)+0.5*k2);
k4 = h*(y(i,2)+k3);
y(i+1,1) = y(i,1)+(k1+2*k2+2*k3+k4)/6;
y1 = y(i,1);
y2 = y(i,2);
k1 = h*((1-y1^2)*y2-y1);
y1 = y(i,1)+0.5*k1;
y2 = y(i,2)+0.5*k1;
k2 = h*((1-y1^2)*y2-y1);
y1 = y(i,1)+0.5*k2;
y2 = y(i,2)+0.5*k2;
k3 = h*((1-y1^2)*y2-y1);
y1 = y(i,1)+k3;
y2 = y(i,2)+k3;
k4 = h*((1-y1^2)*y2-y1);
y(i+1,2) = y(i,2)+(k1+2*k2+2*k3+k4)/6;
end
Trying to give different h, t_end, y0 as input but with
plot(y)
I never get a chaotic plot.
Source of the code: GitHub code
You need to implement RK4 code for the the coupled system as a coupled system. You can avoid the code bloat that would usually imply by utilizing the vector capabilities of the MATrix LABoratory
But staying with the component-wise code you should get
etc.
Note that the Van der Pol oscillator is an oscillator, that it, is has a limit cycle which is a deformation of the circle of radius $2$. If you want a halfway chaotic plot, you should implement the Lorenz system.