How to fix the must be a column vector error?

6 ビュー (過去 30 日間)
Sneh
Sneh 2023 年 4 月 6 日
回答済み: James Tursa 2023 年 4 月 6 日
The following code gives me an error: euler_eqns must be a column vector. How do I fix this?
% Define the initial conditions
q0 = [0; 0; 0; 1];
w0 = [1; 2; 1];
% Define the time span
tspan = [0 20];
% Integrate the Euler equations using ode45
[t, y] = ode45(@euler_eqns, tspan, q0);
Error using odearguments
EULER_EQNS must return a column vector.

Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
% Extract the Euler parameters and angular velocities
q = y(:, 1:4);
% Calculate the precession, nutation, and spin angles
nutation = acos(1 - 2.*q(:,1).^2 - 2.*q(:,3).^2);
precession = asin((2*(q(:,1).*q(:,2) - q(:,3).*q(:,4)))./(sin(nutation)));
spin = asin((2*(q(:,1).*q(:,2) + q(:,3).*q(:,4)))./(sin(nutation)));
% Convert to degrees
psi = rad2deg(precession);
theta = rad2deg(nutation);
phi = rad2deg(spin);
% Plot the angles over time
plot(t, psi, t, theta, t, phi)
legend('Precession', 'Nutation', 'Spin')
xlabel('Time (s)')
ylabel('Angle (deg)')
function dqdt = euler_eqns(t, q)
% Extract the Euler parameters and angular velocities
w = [1; 2; 1;0];
E = [1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1];
% Calculate the time derivatives of the Euler parameters
dqdt = 0.5 * w.* E;
end

回答 (2 件)

Matt J
Matt J 2023 年 4 月 6 日
You have not inspected what euler_eqns s returning. If you do, you will see that it is not a column vector.

James Tursa
James Tursa 2023 年 4 月 6 日
You should double check your derivative function. Shouldn't the result be a 4-element column vector that is a function of q? Neither of these is true for what you have written. Also, if this is supposed to be a quaternion integration this is not a very good way to do it because ode45( ) will not keep the resulting q normalized. You may want to write your own scheme that can do this.

カテゴリ

Help Center および File ExchangeNumerical Integration and Differential Equations についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by