-
Notifications
You must be signed in to change notification settings - Fork 9
/
phase_plot2.m
53 lines (45 loc) · 1.35 KB
/
phase_plot2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
function phase_plot2(f, intial_values, range, simtime, scale)
% Phase portrait plot for a SECOND order ODE
% f is the system function that will besolve using ode45, it must return
% a column vector (2x1).
%
% intial_values is ithe initial states of th system (vector of 2x1)
%
% simtime is the simulation time
%
% scale is used to adjust the dimension of the arrows
% this corresponds to the AutoScale property of the quiver function
%
% References:
% http://matlab.cheme.cmu.edu/2011/08/09/phase-portraits-of-a-system-of-odes/
if nargin < 5
scale = 0.5;
end
%% Solve the ODE
[~, x] = ode45(f, 0:0.001:simtime, intial_values); % ode45 at 1 khz
%% Vector field
% 2nd order ODE
x1 = linspace(range(1, 1), range(1, 2), 10);
x2 = linspace(range(2, 1), range(2, 2), 10);
[X1, X2] = meshgrid(x1, x2);
u = zeros(size(X1));
v = zeros(size(X2));
t = 0;
for i = 1:numel(X1)
X_DOT = f(t,[X1(i); X2(i)]);
Vmod = sqrt(X_DOT(1)^2 + X_DOT(2)^2);
u(i) = X_DOT(1)/Vmod;
v(i) = X_DOT(2)/Vmod;
end
% Drawing
h = quiver(X1, X2, u, v, 'r');
h.AutoScaleFactor = scale;
hold on;
plot(x(:,1), x(:,2), 'b', 'LineWidth', 3);
xlabel('$x_1$', 'interpreter', 'latex')
ylabel('$x_2$', 'interpreter', 'latex')
axis tight equal;
xlim(range(1,:));
ylim(range(2,:));
hold off;
end