How to Draw Phase potrait using matlab? Explained with Example.

Problem: xdot= x+y and ydot= 4x-2y  write 

computer code to plot several

trajectories.

 
We need to time integrate this two equations using RK-4 method with initial guess. You will get two different time series one is for X and another for Y. Now you co-relate those time series values for X & Y for similar time stamp and draw the trajectory for the particular initial guess you took.
This will be repeated for another initial guess and you will get another trajectory. Like this way you keep on adding new guesses and new trajectories.

Here The RK-4 is done using ODE45 solver to reduce the code size.

Sample code:

clc; clear all; close all;
t=0:0.01:1;
format Long;
radius=10;
theta=linspace(0,2*pi,20);
x=radius*cos(theta);
y=radius*sin(theta);
for i=1:length(x)
options=odeset('RelTol',1e-8,'AbsTol',[1e-8,1e-8]);
[t,u]=ode45(@equation,t,[x(i) y(i)],options);
hold on;
plot(u(:,1),u(:,2));
grid on
fprintf('t x y \n');
for j=1:length(t)
fprintf('%f %f %f\n',t(i),u(i,1),u(i,2));
end
end
u=x+y;
v=4*x-2*y;
hold on;
quiver(x,y,u,v); grid on;
function dy=equation(t,y)
dy=zeros(2,1);
dy(1)=y(1)+y(2);
dy(2)=4*y(1)-2*y(2);
end


Results:









Comments