MATLAB: How to use ODE 45 to solve for the motion of a baton without structures

differential equationsMATLABode45

I am trying to copy the results from a Matlab example seen here: https://www.mathworks.com/help/matlab/math/solve-equations-of-motion-for-baton.html
I am explicitly not trying to turn the whole thing into a structures problem. I will be extending the code I am able to create here to a more complicated differential equation problem which I need to solve. So far I have been able to creat a function which defined inputs, just to keep them out of the way, I have created a function which creates the mass matrix which I am using for the solution and to avoid creating a structure using odeset as an input to the ode45 function I simply multiplied the inverse of the mass function by my state variable matrix. The error I get is:
Error in ode45 (line 115) odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
function[m1, m2, L, g]= constants()
m1 = 0.1;
m2 = 0.1;
L = 1;
g = 9.81;
end
function M = mass_baton
[m1, m2, L, g]= constants();
M = zeros(6,6);
M(1,1) = 1;
M(2,2) = m1 + m2;
M(2,6) = -m2*L*sin(q(5));
M(3,3) = 1;
M(4,4) = m1 + m2;
M(4,6) = m2*L*cos(q(5));
M(5,5) = 1;
M(6,2) = -L*sin(q(5));
M(6,4) = L*cos(q(5));
M(6,6) = L^2;
end
function dydt = dydt_baton(t,q)
[m1, m2, L, g]= constants();
q_matrix = [q(2)
m2*L*q(6)^2*cos(q(5))
q(4)
m2*L*q(6)^2*sin(q(5))-(m1+m2)*g
q(6)
-g*L*cos(q(5))];
dydt = inv(M)*q_matrix;
end
And when I try to run the script below, I get an error about not having enough inputs. What am I missing?
[m1, m2, L, g]= constants();
tspan = linspace(0,4,25);
y0 = [0; 4; L; 20; -pi/2; 2];
[t,y] = ode45(@dydt_baton,tspan,y0);

Best Answer

[~, ~, L, ~]= constants;
tspan = linspace(0,4,25);
y0 = [0; 4; L; 20; -pi/2; 2];
[t,y] = ode45(@dydt_baton,tspan,y0);
plot(t,y)
function[m1, m2, L, g]= constants
m1 = 0.1;
m2 = 0.1;
L = 1;
g = 9.81;
end
function M = mass_baton(q)
[m1, m2, L, ~]= constants;
M = zeros(6,6);
M(1,1) = 1;
M(2,2) = m1 + m2;
M(2,6) = -m2*L*sin(q(5));
M(3,3) = 1;
M(4,4) = m1 + m2;
M(4,6) = m2*L*cos(q(5));
M(5,5) = 1;
M(6,2) = -L*sin(q(5));
M(6,4) = L*cos(q(5));
M(6,6) = L^2;
end
function dydt = dydt_baton(~,q)
[m1, m2, L, g]= constants();
M = mass_baton(q);
q_matrix = [q(2)
m2*L*q(6)^2*cos(q(5))
q(4)
m2*L*q(6)^2*sin(q(5))-(m1+m2)*g
q(6)
-g*L*cos(q(5))];
dydt = M\q_matrix;
end