ySol(t) =
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);