% integrates the van der Pol equation, making phase space and time evolution plots
% we use ode45, which is a Runge-Kutta method (basically a fancy Euler method)
% for Matlab reasons the ODE function itself needs to be defined at the end of the script

% DEFINE PARAMETERS
mu = 1;

% set up start and end times of the integration
tstart = 0;
tend = 100;

% timestep 
dt = 0.01;

% initial condition 
x_0 = [1 1]; 

% INTEGRATE AND PLOT
t = linspace(tstart,tend,floor((tend-tstart)/dt));

[t_out,x] = ode45(@(t,x) odefcn(t,x,mu),t,x_0);

plot(t_out,x(:,1));
xlabel('Time')
ylabel('x')

figure
plot(x(:,1),x(:,2));
xlabel('x')
ylabel('dx/dt')

% DEFINE ODE FUNCTION
function dxdt = odefcn(t,x,mu)
    dxdt = zeros(2,1);
    dxdt(1) = x(2);
    dxdt(2) = -mu*x(2)*(x(1)^2-1)-x(1);
end

