Phase plane plot second order ODE

조회 수: 37 (최근 30일)
Elinor Ginzburg
Elinor Ginzburg 2024년 4월 24일
댓글: Elinor Ginzburg 2024년 4월 24일
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

채택된 답변

Sam Chak
Sam Chak 2024년 4월 24일
편집: Sam Chak 2024년 4월 24일
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
Sam Chak
Sam Chak 2024년 4월 24일
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 2024년 4월 24일
I think I got it. Thank you for your help! I appreciate it very much.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by