not enough input arguments when using function handle
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
per isakson
2016-8-8
编辑:per isakson
2016-8-8
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 个评论
per isakson
2016-8-8
The original code contains a "trivial" mistake, which I missed. The expression,   ['@(t) curve_p_16_1_' num2str(i)],   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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!