Clear Filters
Clear Filters

Phase plane plot second order ODE

12 views (last 30 days)
Hi,
I don't have enough experience with Matlab, so forgive me for the question, it's probably pretty basic. have an ode of the following form where are know constants. How can I plot the phase plane?
this is the code I've used with and
% Define the system of first-order ODEs
function dydt = second_order_ode(t, y)
% y(1) = x, y(2) = dx/dt
dydt = [y(2); -y(1) - 0.5*y(2)];
end
% Define time span
tspan = [0 20];
% Define initial conditions
y0 = [0.5; 0]; % initial position and velocity
% Solve the ODE system
[t, y] = ode45(@second_order_ode, tspan, y0);
% Plot the phase plane
figure;
plot(y(:,1), y(:,2));
xlabel('x');
ylabel('dx/dt');
title('Phase Plane Plot for x'' + x'' + 0.5x = 0');
grid on;
But I think there's something wrong with it because the plot generated is not what I expected (straight lines since )
Thank for your help

Accepted Answer

Sam Chak
Sam Chak on 24 Apr 2024
Edited: Sam Chak on 24 Apr 2024
Looks correct! No issue because I checked with the built-in @odephas2 function to generate the 2-D phase plane plot.
Edit: Upon rechecking your original code, I found that there were no errors during execution. However, I discovered that the code within the second_order_ode() function was incorrect. Please refer to the comment within the code for the necessary corrections.
ODE:
State-space:
%% Define the system of first-order ODEs
function dydt = second_order_ode(t, y, param)
% parameters
alpha = param.alpha;
beta = param.beta;
gamma = param.gamma;
% ODEs
dydt = [y(2);
gamma - alpha*y(2) - beta*y(1)]; % <-- Corrections in this line
end
%% set parameters
param.alpha = 1;
param.beta = 0.5;
param.gamma = 0;
%% Define time span
tspan = [0 20];
% Define initial conditions
y0 = [0.5; 0]; % initial position and velocity
%% Solve the ODE system
opts = odeset('OutputFcn',@odephas2, 'Stats', 'on');
[t, y] = ode45(@(t, y) second_order_ode(t, y, param), tspan, y0, opts);
22 successful steps 0 failed attempts 133 function evaluations
  5 Comments
Sam Chak
Sam Chak on 24 Apr 2024
The short answer is because the solution is a decaying sinusoidal function.
A phase plane plot, also known as a phase portrait or phase diagram, is a graphical representation used in the field of dynamical systems to visualize the behavior of a system of differential equations. It provides insight into the qualitative behavior and stability of the system.
In a phase plane plot, the state variables of the system are typically represented on the x and y axes. Each point in the plot corresponds to a specific combination of values for the state variables at a given time. By plotting many points over time, the evolution of the system can be observed.
By the way, I have edited the code in my Answer above as the dynamics was incorrect in the second_order_ode() function.
syms y(t)
alpha = 1;
beta = 0.5;
gamma = 0;
dy = diff(y,t);
ddy = diff(y,t,2);
eqn = ddy + alpha*dy + beta*y == gamma;
cond = [y(0)==0.5, dy(0)==0];
ySol(t) = dsolve(eqn, cond)
ySol(t) = 
dySol(t) = diff(ySol)
dySol(t) = 
Elinor Ginzburg
Elinor Ginzburg on 24 Apr 2024
I think I got it. Thank you for your help! I appreciate it very much.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming 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!