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!):
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 = ...
Best Answer