Not enough input arguements in a function

Hello, I have a problem in my ode45 solver I can't solve, I am fairly new to MATLAB and so far the code makes sense to me but I can't get it working.
clc; clear;
p = [9.81 , 12 , 4000 , 100 , 8 , 6 , 6 , 1.5 , 7.5];
g = p(1);
m_B = p(2); M_CW = p(3);
M_P = p(4); L_B = p(5);
H = p(6); L_S = p(7);
L_BC = p(8); L_BP = p(9);
%z1(1)=thet
%z1(2)=omega%
dz = @(t1,z1) [z1(2);
-R1fun(z1(1),z1(2),p)/M1fun(z1(1),p)];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
IC = [acos((-H)/(L_BP)),0]; T = 10;
opts = odeset('RelTol',1e-6,'Events',@COND);
[t1,z1] = ode45(dz,0:T,IC,opts);
Other than ode outputting complex values for angle and angular velocity, the code is not working with a given condition.
function [value,term,direction] = COND(t1,z1,p)
G = -R1fun(z1(1),z1(2),p)/M1fun(z1(1),p);
dv_s = dv_Sfun(z1(1),z1(2),G,p);
value = (dv_s/tan(acos((p(6)-p(9)*sin(z1(1)-pi/2))/p(7))))-p(1);
term = 1;
direction = 1;
end
Functions M1fun and R1fun are predefined and seem to be working good, R1 is taking 3 inputs (thet,omega,p) and M1 is taking (thet,p).
Error I am getting is:
Not enough input arguments.
Error in COND (line 3)
G = -R1fun(z1(1),z1(2),p)/M1fun(z1(1),p);
Error in odeevents (line 28)
eventValue = feval(eventFcn,t0,y0,eventArgs{:});
Error in ode45 (line 139)
odeevents(FcnHandlesUsed,odeFcn,t0,y0,options,varargin);
Error in aaabbbccc (line 22)
[t1,z1] = ode45(dz,0:T,IC,opts);
I appreciate all help, thank you in advance :)

Risposte (1)

Stephen23
Stephen23 il 15 Nov 2021
Modificato: Stephen23 il 15 Nov 2021
The Events function is defined to have only two input arguments (not three like COND):
Solution: you need to parameterize the COND function handle, e.g.:
opts = odeset('RelTol',1e-6,'Events',@(t,y)COND(t,y,p));

11 Commenti

When I delete t1 as a possible input in the function, because it is not referenced anyway in COND event function, it produces this error message:
Index exceeds the number of array elements. Index must not exceed 1.
Error in COND (line 3)
G = -R1fun(z1(1),z1(2),p)/M1fun(z1(1),p);
Error in odeevents (line 28)
eventValue = feval(eventFcn,t0,y0,eventArgs{:});
Error in ode45 (line 139)
odeevents(FcnHandlesUsed,odeFcn,t0,y0,options,varargin);
Error in aaabbbccc (line 23)
[t1,z1] = ode45(dz,0:T,IC,opts);
Stephen23
Stephen23 il 15 Nov 2021
Modificato: Stephen23 il 15 Nov 2021
"When I delete t1 as a possible input in the function, "
Why did you delete the t1 input argument?
If you delete the t1 input argument then you will need to modify the input signature to COND as well.
To be honest with you, I don't know how to fix the code, I tried parametrising it, but now it says that there are too many arguments
Stephen23
Stephen23 il 16 Nov 2021
Modificato: Stephen23 il 16 Nov 2021
"I tried parametrising it, but now it says that there are too many arguments"
Did you try my answer? It should not give that error message, so you must have done something else.
If you want help debugging your modified code then you will have to show it to us. I guess from the sounds of that error message you removed one of the variables from the function parameterisation but not from the function signature. But guessing is the best I can do without seeing your modified code.
If you removed the time input from COND because it is not used inside COND, then you would need
opts = odeset('RelTol',1e-6,'Events',@(t,y)COND(y,p));
and your function COND would expect two parameters, with the time not being passed as the first parameter.
You can parameterize functions to add parameters such as p, but you can also parameterize functions to remove parameters that your called function does not expect.
Hello, here is the code after I made some modifications.
IC = [acos((-H)/(L_BP)),0]; T = 10;
opts = odeset('RelTol',1e-6,'Events',@(t1,z1) COND(z1,p));
[t1,z1] = ode45(dz,0:T,IC,opts);
Here is COND event function
function [value,term,direction] = COND(z1,p)
G = -R1fun(z1(1),z1(2),p)/M1fun(z1(1),p);
dv_s = dv_Sfun(z1(1),z1(2),G,p);
value = (dv_s/tan(acos((p(6)-p(9)*sin(z1(1)-pi/2))/p(7))))-p(1);
term = 1;
direction = 1;
end
Error message is
Error using odezero (line 49)
odezero: an event disappeared (internal error)
Error in ode45 (line 397)
odezero(@ntrp45,eventFcn,eventArgs,valt,t,y,tnew,ynew,t0,h,f,idxNonNegative);
Error in aaabbbccc (line 23)
[t1,z1] = ode45(dz,0:T,IC,opts);
Do you have any more tips, cause it seems I am lost at this point
I do not seem to see your R1fun or M1fun posted ?
Is it possible that they involve randomness, or that they have any if statements in them ?
Is it possible that they return vectors rather than scalars, and that your / between them in dz is a matrix division rather than element-by-element division, and that also that system is numerically unstable?
odezero: an event disappeared (internal error) implies that a zero or zero crossing was detected by COND, but that when ode45() tried to step through to find the exact boundary, that it was unable to find it. Perhaps COND returned different results when invoked twice on the same boundary, or perhaps the boundary was effectively point-like and only at that exact boundary the zero or zero crossing occurred.
M1 Is given as:
function M1 = M1fun(thet,p)
m_B = p(2);
M_CW = p(3);
M_P = p(4);
L_B = p(5);
H = p(6);
L_S = p(7);
L_BC = p(8);
L_BP = p(9);
M1 = M_P.*(L_BP.*sin(thet).*(H+L_BP.*cos(thet)).*1.0./sqrt(-(H+L_BP.*cos(thet)).^2+L_S.^2)-(sqrt(2.0).*L_BP.*sin(thet.*2.0).*1.0./sqrt(-cos(thet.*2.0)+1.0))./2.0).^2+L_BC.^2.*M_CW+(L_B.^3.*m_B)./3.0+L_B.*L_BC.^2.*m_B-L_B.^2.*L_BC.*m_B;
While R1 is given as:
function R1 = R1fun(thet,dthet,p)
g = p(1);
m_B = p(2);
M_CW = p(3);
M_P = p(4);
L_B = p(5);
H = p(6);
L_S = p(7);
L_BC = p(8);
L_BP = p(9);
R1 = M_P.*(L_BP.*sin(thet).*(H+L_BP.*cos(thet)).*1.0./sqrt(-(H+L_BP.*cos(thet)).^2+L_S.^2)-sqrt(2.0).*L_BP.*cos(thet).*sin(thet).*1.0./sqrt(cos(thet).^2.*-2.0+2.0)).*(L_BP.*dthet.^2.*(cos(thet).^2-cos(thet).^4).*1.0./(sin(thet).^2).^(3.0./2.0)+(L_BP.*dthet.^2.*sin(thet).^2)./abs(sin(thet))-L_BP.^2.*dthet.^2.*sin(thet).^2.*1.0./sqrt(-(H+L_BP.*cos(thet)).^2+L_S.^2)-L_BP.*dthet.^2.*cos(thet).^2.*1.0./sqrt(-cos(thet).^2+1.0)+L_BP.*dthet.^2.*cos(thet).*(H+L_BP.*cos(thet)).*1.0./sqrt(-(H+L_BP.*cos(thet)).^2+L_S.^2)-L_BP.^2.*dthet.^2.*sin(thet).^2.*(H+L_BP.*cos(thet)).^2.*1.0./(-(H+L_BP.*cos(thet)).^2+L_S.^2).^(3.0./2.0))+g.*sin(thet).*((L_B.^2.*m_B)./2.0+L_BC.*M_CW-L_B.*L_BC.*m_B);
I think they just return a scalar. Another problem caught my attention, when I remove event function COND, after running the script, ode45 produces complex outputs, while i don't think it should.
p = [9.81 , 12 , 4000 , 100 , 8 , 6 , 6 , 1.5 , 7.5];
g = p(1);
m_B = p(2); M_CW = p(3);
M_P = p(4); L_B = p(5);
H = p(6); L_S = p(7);
L_BC = p(8); L_BP = p(9);
%z1(1)=thet
%z1(2)=omega%
dz = @(t1,z1) [z1(2);
-R1fun(z1(1),z1(2),p)/M1fun(z1(1),p)];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
IC = [acos((-H)/(L_BP)),0]; T = 10;
opts = odeset('RelTol',1e-6,'Events',@(t1,z1) COND(z1,p));
[t1,z1] = ode45(dz,0:T,IC,opts);
Unrecognized function or variable 'dv_Sfun'.

Error in solution>COND (line 43)
dv_s = dv_Sfun(z1(1),z1(2),G,p);

Error in solution (line 13)
opts = odeset('RelTol',1e-6,'Events',@(t1,z1) COND(z1,p));

Error in odeevents (line 28)
eventValue = feval(eventFcn,t0,y0,eventArgs{:});

Error in ode45 (line 139)
odeevents(FcnHandlesUsed,odeFcn,t0,y0,options,varargin);
function M1 = M1fun(thet,p)
m_B = p(2);
M_CW = p(3);
M_P = p(4);
L_B = p(5);
H = p(6);
L_S = p(7);
L_BC = p(8);
L_BP = p(9);
M1 = M_P.*(L_BP.*sin(thet).*(H+L_BP.*cos(thet)).*1.0./sqrt(-(H+L_BP.*cos(thet)).^2+L_S.^2)-(sqrt(2.0).*L_BP.*sin(thet.*2.0).*1.0./sqrt(-cos(thet.*2.0)+1.0))./2.0).^2+L_BC.^2.*M_CW+(L_B.^3.*m_B)./3.0+L_B.*L_BC.^2.*m_B-L_B.^2.*L_BC.*m_B;
end
function R1 = R1fun(thet,dthet,p)
g = p(1);
m_B = p(2);
M_CW = p(3);
M_P = p(4);
L_B = p(5);
H = p(6);
L_S = p(7);
L_BC = p(8);
L_BP = p(9);
R1 = M_P.*(L_BP.*sin(thet).*(H+L_BP.*cos(thet)).*1.0./sqrt(-(H+L_BP.*cos(thet)).^2+L_S.^2)-sqrt(2.0).*L_BP.*cos(thet).*sin(thet).*1.0./sqrt(cos(thet).^2.*-2.0+2.0)).*(L_BP.*dthet.^2.*(cos(thet).^2-cos(thet).^4).*1.0./(sin(thet).^2).^(3.0./2.0)+(L_BP.*dthet.^2.*sin(thet).^2)./abs(sin(thet))-L_BP.^2.*dthet.^2.*sin(thet).^2.*1.0./sqrt(-(H+L_BP.*cos(thet)).^2+L_S.^2)-L_BP.*dthet.^2.*cos(thet).^2.*1.0./sqrt(-cos(thet).^2+1.0)+L_BP.*dthet.^2.*cos(thet).*(H+L_BP.*cos(thet)).*1.0./sqrt(-(H+L_BP.*cos(thet)).^2+L_S.^2)-L_BP.^2.*dthet.^2.*sin(thet).^2.*(H+L_BP.*cos(thet)).^2.*1.0./(-(H+L_BP.*cos(thet)).^2+L_S.^2).^(3.0./2.0))+g.*sin(thet).*((L_B.^2.*m_B)./2.0+L_BC.*M_CW-L_B.*L_BC.*m_B);
end
function [value,term,direction] = COND(z1,p)
G = -R1fun(z1(1),z1(2),p)/M1fun(z1(1),p);
dv_s = dv_Sfun(z1(1),z1(2),G,p);
value = (dv_s/tan(acos((p(6)-p(9)*sin(z1(1)-pi/2))/p(7))))-p(1);
term = 1;
direction = 1;
end
Does it work, or do you have no clue?

Accedi per commentare.

Prodotti

Release

R2021b

Richiesto:

il 15 Nov 2021

Commentato:

il 17 Nov 2021

Community Treasure Hunt

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

Start Hunting!

Translated by