How to fix the must be a column vector error?
7 views (last 30 days)
Show older comments
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);
% 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
0 Comments
Answers (2)
Matt J
on 6 Apr 2023
You have not inspected what euler_eqns s returning. If you do, you will see that it is not a column vector.
0 Comments
James Tursa
on 6 Apr 2023
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.
0 Comments
See Also
Categories
Find more on Numerical Integration and Differential Equations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!