Not enough input arguements in a function
Mostra commenti meno recenti
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)
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
Albert Kocik
il 15 Nov 2021
Albert Kocik
il 15 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.
Walter Roberson
il 16 Nov 2021
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.
Albert Kocik
il 16 Nov 2021
Albert Kocik
il 16 Nov 2021
Walter Roberson
il 16 Nov 2021
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.
Albert Kocik
il 17 Nov 2021
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);
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
Albert Kocik
il 17 Nov 2021
Categorie
Scopri di più su Number Theory in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!