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

 Réponse acceptée

Sam Chak
Sam Chak le 24 Avr 2024
Modifié(e) : Sam Chak le 24 Avr 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 commentaires

Elinor Ginzburg
Elinor Ginzburg le 24 Avr 2024
Déplacé(e) : Sam Chak le 24 Avr 2024
Thank you! I probably misunderstood something in the mathematics.
Sam Chak
Sam Chak le 24 Avr 2024
I'm glad to hear that your problem has been resolved. If everything is now working as expected, would you mind clicking on the 'Accept' button to officially close the issue?
If you have any other ODE-related questions or need further assistance, please feel free to let me know.
Elinor Ginzburg
Elinor Ginzburg le 24 Avr 2024
Thank you, I accepted the answer. I actually have a problem understanding why the trajectory looks that way, I wasn't sure it's the place to ask, I hope that's ok
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 le 24 Avr 2024
I think I got it. Thank you for your help! I appreciate it very much.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Produits

Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by