MATLAB: Coupled ODE error- too many inputs

differential equationsode45

I've been trying to solve a set of 4 coupled differential equations that are pretty nasty and then plot solutions. I keep getting errors when using ode45 to solve the system. Here's the important code:
g=9.8; l1=1; l2=1; m1=1; m2=1;
diffeq = @(t,y,g,m1,m2,l1,l2) [eq1;eq2;eq3;eq4];
odeWrapper = @(t,y) diffeq(t,y,g,m1,m2,l1,l2);
[t,yi] = ode45(odeWrapper,[0,1],[45,0,0,0]);
plot(t,yi(:,1));
I'm not going to bother to actually type out the equations I put into diffeq because they're ridiculously long, but I have checked to make sure parentheses match up correctly and stuff like that.
I keep getting the same error saying I have too many input arguments to the ode function:
Error using
ode>@(t,y)[(l2*y(3)-l1*y(4)*cos(y(1)-y(2)))/((l1^2)*l2*(m1+m2*(sin(y(1)-y(2))^2)));(l1*(m1+m2)*y(4)-l2*m2*y(3)*cos(y(1)-y(2)))/(l1*(l2^2)*m2*(m1+m2*(sin(y(1)-y(2))^2)));-(m1+m2)*g*l1*sin(y(1))-((y(3)*y(4)*sin(y(1)-y(2)))/(l1*l2(m1+m2*(sin(y(1)-y(2))^2))))+(((l2^2)*m2*(y(3)^2)+(l1^2)*(m1+m2)*(y(4)^2)-l1*l2*m2*y(3)*y(4)*cos(y(1)-y(2)))*sin(2*(y(1)-y(2))))/(2*(l1^2)*(l2^2)*(m1+m2*(sin(y(1)-y(2))^2))^2);-m2*g*l2*sin(y(2))+((y(3)*y(4)*sin(y(1)-y(2)))/(l1*l2(m1+m2*(sin(y(1)-y(2))^2))))-(((l2^2)*m2*(y(3)^2)+(l1^2)*(m1+m2)*(y(4)^2)-l1*l2*m2*y(3)*y(4)*cos(y(1)-y(2)))*sin(2*(y(1)-y(2))))/(2*(l1^2)*(l2^2)*(m1+m2*(sin(y(1)-y(2))^2))^2)]
Too many input arguments.
Error in ode>@(t,y)diffeq(t,y,g,m1,m2,l1,l2)
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in ode (line 30)
[t,yi] = ode45(@(t,y) diffeq(t,y,g,m1,m2,l1,l2),[0,1],[1,0,0,0]);
Any help would be much appreciated as I need this to work for a paper that is due soon!

Best Answer

Try it with an explicit instead of an anonymous function:
function dy = fcn(t, y)
g=9.8; l1=1; l2=1; m1=1; m2=1;
dy = [(l2*y(3)-l1*y(4)*cos(y(1)-y(2)))/((l1^2)*l2*(m1+m2*(sin(y(1)-y(2))^2))); ...
(l1*(m1+m2)*y(4)-l2*m2*y(3)*cos(y(1)-y(2)))/(l1*(l2^2)*m2*(m1+m2*(sin(y(1)-y(2))^2))); ...
-(m1+m2)*g*l1*sin(y(1))-((y(3)*y(4)*sin(y(1)-y(2)))/(l1*l2(m1+m2*(sin(y(1)-y(2))^2))))+ ...
(((l2^2)*m2*(y(3)^2)+(l1^2)*(m1+m2)*(y(4)^2)-l1*l2*m2*y(3)*y(4)*cos(y(1)-y(2)))* ...
sin(2*(y(1)-y(2))))/(2*(l1^2)*(l2^2)*(m1+m2*(sin(y(1)-y(2))^2))^2); ...
-m2*g*l2*sin(y(2))+((y(3)*y(4)*sin(y(1)-y(2)))/(l1*l2(m1+m2*(sin(y(1)-y(2))^2)))) - ...
(((l2^2)*m2*(y(3)^2)+(l1^2)*(m1+m2)*(y(4)^2)-l1*l2*m2*y(3)*y(4)*cos(y(1)-y(2)))* ...
sin(2*(y(1)-y(2))))/(2*(l1^2)*(l2^2)*(m1+m2*(sin(y(1)-y(2))^2))^2)];
end
And in the main code:
[t,yi] = ode45(@fcn,[0,1],[45,0,0,0]);
If this works, --- wow, no, it does not work. As soon as your write this as a function, you might see this part (twice!):
... l2(m1+m2*(sin(y(1)-y(2))^2) ...
This should be most likely:
l2 * (m1+m2*(sin(y(1)-y(2))^2)
^
With your code "l2" is treated as a function and this might cause the "Too many input arguments" error. Do you have a "l2.m" file in your path? If not, I'd expect a message message like "no such function found for input argument double" or "indices exceed array dimensions". So maybe there is another error in addition.
Such typos are very hard to identify an anonymous functions.
A simplified version of the function:
function dy = fcn(t, y)
g = 9.8; l1 = 1; l2 = 1; m1 = 1; m2 = 1;
c = cos(y(1) - y(2));
s = sin(y(1) - y(2));
d = m1 + m2 * s ^ 2;
f = y(3) * y(4) * s / (l1 * l2 * d);
h = (l2^2 * m2 * y(3)^2 + ...
l1^2 * (m1+m2) * y(4)^2 - ...
l1 * l2 * m2 * y(3) * y(4) * c) * ...
sin(2 * (y(1) - y(2))) / (2 * l1^2 * l2^2 * d^2);
dy = [(y(3) - l1/l2 * y(4) * c) / (l1^2 * d); ...
((m1+m2) * y(4) - l2/l1 * m2 * y(3) * c) / (l2^2 * m2 * d); ...
-(m1+m2) * g * l1 * sin(y(1)) - f + h; ...
-m2 * g * l2 * sin(y(2)) + f - h];
end
Looks less cruel in my eyes. Do you see the magic power of spaces? Now a typo like "l2()" would be easier to find and if unnecessary parentheses are removed, the meaning of the required ones gets more obvious.
Note: I did not run it to compare the results, so maybe I made a typo during inserting temporary variables. Check this by your own.
Afterwards it might be more useful to provide the parameters (but only the parameters, not the equation to integrate) by an anonymous function:
g=9.8; l1=1; l2=1; m1=1; m2=1;
diffeq = @(t,y) fcn(t, y, g, m1, m2, l1, l2);
...
function dy = fcn(t, y, g, m1, m2, l1, l2)
dy = ...