问题
I am trying to get the direction field and phase portrait shown on this wiki page:
Van der Pol oscillator in wikipedia
My code:
options = odeset('MaxStep',0.5);
temp = inputdlg('Enter mu value');
mu = str2double(temp{1,1});
[t,y] = ode45(@(t,y) vdp1_1(t,y,mu),[0 10],[2; 0],options);
plot(y(:,1),y(:,2));
hold on
quiver(y(:,1), y(:,2), gradient(y(:,1)), gradient(y(:,2) ))
hold off
function dydt = vdp1_1(t,y,mu)
dydt = zeros(2,1);
dydt(1) = y(2);
dydt(2) = [mu * (1-y(1)^2)*y(2)-y(1)];
end
Current output:
Desired Output: How to get the direction field on top of this as shown in the wiki page figure
Thanks,
Gopi
回答1:
You need to calculate the vector field at every point you want an arrow to be shown. And then you plot this with quiver. For example.
[Xs,Ys]=meshgrid(-5:5,-5:5); % Will define the positions where we want to plot
Us=Ys; % From your equations, these are the values of the field at each point
Vs=mu*(1-Xs.^2).*Ys-Xs;
quiver(Xs,Ys,Us,Vs) % Should plot the field you want, just add the trajectory on top
来源:https://stackoverflow.com/questions/42969046/how-to-draw-the-direction-field-of-van-der-pol-oscillator