Quiver not working and seemingly wrecking plots.

25 views (last 30 days)
i have my code
not including quiver:
including quiver:
please help
function chimytask1
%following code will allow you to construct the phase portraits
%typical solutions of the predator prey model
%first clear your workspace and close any open windows
clear all
close all
eps=0.8;
q=0.002;
F=linspace(0.8,1.2,8);
[X, Z] = meshgrid(0:0.1:2, 0:0.1:2);
for i=1:8
f=F(i);
Xdot = ((q-X./(X+q))*f.*Z+X-X^2)/eps;
Zdot = X-Z;
figure(1)
quiver(X,Z,Xdot,Zdot)
subplot(2,4,i)
% predator-prey Lotka-Volterra system
f = @(t,y) [((q-y(1)/(y(1)+q))*f*y(2)+y(1)-y(1)^2)/eps; y(1)-y(2)];
hold on
timespan=[0 1000];
%calculate the phase trajectories for different initial conditions
for l=1:3
x0=rand;
for j=0:3
y0=rand;
[ts, ys] = ode23t(f,timespan, [x0, y0]);
% plot of several trajectories
plot(ys(:,1), ys(:,2),'Linewidth', 3)
end
end
xline(0,'--', 'Color', 'k', 'LineWidth', 2); % Draw line for Y axis.
yline(0,'--', 'Color', 'k', 'LineWidth', 2); % Draw line for X axis.
hold off
xlabel('Chem X', 'FontSize',14)
ylabel('Chem Z', 'FontSize',14)
title("f=" + num2str(F(i)))
set(gca, 'FontSize', 16)
xlim([-0.5 1.5])
ylim([-0.5 1.5])
end

Accepted Answer

Walter Roberson
Walter Roberson on 8 Dec 2024 at 18:25
figure(1)
quiver(X,Z,Xdot,Zdot)
subplot(2,4,i)
By default, quiver() uses the current axes to plot in.
The first iteration, figure 1 is created, and then because there is no current axes, a new axes is created that spans the entire figure.
You then create a subplot that overlays that implicit current axes. It is a behaviour of subplot() that subplot() deletes any existing axes that the new axes overlays (unless the axes positions are exactly the same.) So the subplot(2,4,1) is going to start by deleting the first quiver plot.
Then you activate a subplot and that becomes the current axes. You plot several lines into it, and end with "hold off".
The next iteration of for i the current axes is due to the subplot() call previously made. "hold on" is not in effect, so the quiver plot erases the result of the plot().
Then you activate the next subplot(). This time it does not overlay any existing axes, so nothing is erased.
So... you quiver() once to the entire figure() and then that gets zapped by the first subplot(). After that you draw into subplots and then quiver() overtop of them. And that proceeds all the way to the last iteration. The last iteration draws the plot() into the last subplot() and then there is no quiver() done on top of it.
The end result is a bunch of quiver plots, all except for one final plot() result. Oh, and the quiver plots all "lag" one subplot() relative to where you expect to find them.
The cure for this all is simple: Exchange the position of the subplot() and quiver() calls.

More Answers (0)

Categories

Find more on Vector Fields in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!