not enough input arguments when using function handle

5 vues (au cours des 30 derniers jours)
Susan_Y
Susan_Y le 8 Août 2016
Commenté : Susan_Y le 8 Août 2016
I write three functions, and one of them use the other two. When I run 'curve_p_16_1_1' and 'curve_p_16_1_2' alone, they work fine. However, when I run 'curve_p_16_1', it says 'Not enough input arguments.'. Could anyone help me to figure out the problem?
function p = curve_p_16_1_1(t)
%curve function of example 16, NACA 0012
%inner air foil, upper part
%INPUT:
%t: parameter, [0,1], counterclockwise
%OUTPUT:
%p: [x y], point coordinate on curve
%x = sqrt(X) to avoid singularity at xmin
xmin = 0;
xmax = sqrt(1.008930411365);
y = @(x) 0.6*(0.2969*x-0.1260*x^2-0.3516*x^4+0.2843*x^6-0.1015*x^8);
syms s
dy = matlabFunction(diff(0.6*(0.2969*s-0.1260*s^2 ...
-0.3516*s^4+0.2843*s^6-0.1015*s^8),s));
f = @(x) sqrt(1+dy(x).^2);
l = integral(f,xmin,xmax); %total curve length
g = @(x) integral(f,xmin,x)-(1-t)*l;
x = fzero(g,[xmin xmax]);
p = [x^2 y(x)];
function p = curve_p_16_1_2(t)
%curve function of example 16, NACA 0012
%inner air foil, lower part
%INPUT:
%t: parameter, [0,1], counterclockwise
%OUTPUT:
%p: [x y], point coordinate on curve
%x = sqrt(X) to avoid singularity at xmin
xmin = 0;
xmax = sqrt(1.008930411365);
y = @(x) -0.6*(0.2969*x-0.1260*x^2-0.3516*x^4+0.2843*x^6-0.1015*x^8);
syms s
dy = matlabFunction(diff(0.6*(0.2969*s-0.1260*s^2 ...
-0.3516*s^4+0.2843*s^6-0.1015*s^8),s));
f = @(x) sqrt(1+dy(x).^2);
l = integral(f,xmin,xmax); %total curve length
g = @(x) integral(f,xmin,x)-t*l;
x = fzero(g,[xmin xmax]);
p = [x^2 y(x)];
function p = curve_p_16_1(T)
n = 2; %number of curves
for i = 1:n
if T >= (i-1)/n && T <= i/n
t = T*n-(i-1);
str = ['@(t) curve_p_16_1_' num2str(i)];
f = str2func(str);
p = f(t);
end
end
>> curve_p_16_1(0)
Not enough input arguments.
Error in curve_p_16_1_1 (line 18)
g = @(x) integral(f,xmin,x)-(1-t)*l;
Error in curve_p_16_1>@(t)curve_p_16_1_1
Error in curve_p_16_1 (line 9)
p = f(t);
  3 commentaires
Stephen23
Stephen23 le 8 Août 2016
Modifié(e) : Stephen23 le 8 Août 2016
@Susan_Y: please show us the complete error message. This means all of the red text. It would be useful to have some information on how you are calling these functions too.
Susan_Y
Susan_Y le 8 Août 2016
@Stephen Cobeldick, @Adam : I have updated the question with running information.

Connectez-vous pour commenter.

Réponse acceptée

per isakson
per isakson le 8 Août 2016
Modifié(e) : per isakson le 8 Août 2016
Replace
str = ['@(t) curve_p_16_1_' num2str(i)];
f = str2func(str);
p = f(t);
in curve_p_16_1 by
p = feval( sprintf( 'curve_p_16_1_%1d', i ), t );
or
switch i
case 1
p = curve_p_16_1_1( t );
case 2
p = curve_p_16_1_2( t );
otherwise
error( 'Cannot find the function, "%s".' ...
, sprintf( 'curve_p_16_1_%1d', i ) )
end
With one of these replacements the code returns a result
>> curve_p_16_1(0)
ans =
1.0089 0.0000
The original code doesn't pass the argument, t, - it seems.
  3 commentaires
per isakson
per isakson le 8 Août 2016
The original code contains a "trivial" mistake, which I missed. The expression, &nbsp ['@(t) curve_p_16_1_' num2str(i)], &nbsp doesn't include t in the call of the function, curve_p_ ....
Try replacing
str = ['@(t) curve_p_16_1_' num2str(i)];
by
str = ['@(t) curve_p_16_1_', num2str(i), '(t)' ];
or
str = ['@curve_p_16_1_', num2str(i) ];
or
str = ['curve_p_16_1_', num2str(i) ];
All three makes the code work. The second is the "preferred" one.
The switch-case construct is the one that is easiest to debug.
Susan_Y
Susan_Y le 8 Août 2016
Thank you again. This is the true reason. I should be careful about the format of function handle.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by