Hi,
To create a phase portrait in MATLAB using “quiver” function, you need to first set up a grid of points over which you need to calculate the vector field.
In your case, you're working with a single-variable ODE, so you will be plotting a 1D phase portrait. “quiver” function is generally used for 2D vector fields and to set up a 2D grid that represents your system, you can start by:
- Defining a mesh grid over the area of interest.
- Calculate the derivative at each point in the grid.
- Use “quiver” function to plot the vector field.
Please refer to the following modified code using “quiver” to build a phase portrait of a function you have mentioned:
global a b r K;
a = 2;
b = 2;
r = 0.48;
K = 17;
% Define the function for the derivative
dPdt = @(P) r * P .* (1 - P / K) - a * P.^2 ./ (b^2 + P.^2);
% Create a grid of points for P
P = linspace(0, 20, 20);
% Calculate the derivative at each point
dP = dPdt(P);
% Create a 2D grid for plotting
[P_grid, dP_grid] = meshgrid(P, dP);
% Use quiver to plot the direction field
figure;
quiver(P_grid, dP_grid, ones(size(P_grid)), dP_grid, 'AutoScale', 'on');
xlabel('P');
ylabel('dP/dt');
title('Phase Portrait');
grid on;
% Plot the nullclines
hold on;
y1 = r .* P .* (1 - P / K);
ya = a * P ./ (b^2 + P.^2);
plot(P, y1, 'k-', 'LineWidth', 2);
plot(P, ya, 'b--', 'LineWidth', 2);
legend('Vector Field', 'Nullcline 1', 'Nullcline 2');
I hope the solution provided above is helpful.