# Drawing phase plane diagram of a system of 3 coupled odes - MATLAB

9 views (last 30 days)
UserCJ on 20 Mar 2022
Commented: UserCJ on 21 Mar 2022
I have a system of 3 coupled odes and this is how I solve the system.
Tend = 100;
Nt = 100;
% Define RHS functions
RHS = @(t,x)RHS(t,x,param);
%Execution-----------------------------------------------------------------
x0 = [0.004, 0.05, 0.1]; %Initial condition
t = linspace(0,Tend,Nt); %TSPAN
% Options with event function
[t, A] = ode45(RHS, t, x0);
I'd like to plot the phase plane diagram with the vector field for the solution of this model. I used quiver3 to create arrows for the system as follows:
N=20; %number of arrows along each dimension
%Next: Compute N evenly spaced points along each axis.
xvals=linspace(min(A(:,1)),max(A(:,1)),N);
yvals=linspace(min(A(:,2)),max(A(:,2)),N);
zvals=linspace(min(A(:,3)),max(A(:,3)),N);
%Next: Make vectors of length N^3 that contain the x,y,z locations of all the arrows.
[X,Y,Z]=meshgrid(xvals,yvals,zvals); %creates NxNxN arrays
%Next: Make X,Y,Z row vectors, since quiver3() won't accept NxNxN array.
X=reshape(X,1,N^3);
Y=reshape(Y,1,N^3);
Z=reshape(Z,1,N^3);
U=zeros(1,N^3); V=U; W=U; %pre-allocate arrays U,V,W
for i=1:N^3
%Next: Compute the direction components [U,V,W] for each arrow.
deriv=RHS(0,[X(i),Y(i),Z(i)]);
U(i)=deriv(1); V(i)=deriv(2); W(i)=deriv(3);
end
figure;
quiver3(X,Y,Z,U,V,W); %plot black arrows
This is the output of this code.
But I donot know how to plot the solution in this diagram as a surface or as trajectories to see the flow using arrows. Any help is much appreciated.
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Sam Chak on 21 Mar 2022
I think you should be able to plot the trajectory solution using plot3 after retaining the 3D diagram (quiver3):
hold on
plot3(A(:,1), A(:,2), A(:,3))
or
hold on
plot3(sol(:,1), sol(:,2), sol(:,3))
##### 1 CommentShowHide None
UserCJ on 21 Mar 2022
Thanks @Sam Chak
I accidentally used "plot" instead of "plot3". Thanks for your suggestion.

Sign in to comment.

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by