Undefined operand in function_handle

When I enter ' H(3) ' in the command line, the following error is shown:
Undefined operator '-' for input arguments of type 'function_handle'.
Error in
Trial_H>@(m)d1+s*(cos(gama)+sum(cos(gama+(pi/2-phi_i))))+((2*r)-omega)*sin(B_j(m))+(r-omega)*sum(sin((1:m-1)*pi-sum(B_j(1:m-1))))
Here is the code:
% Variables
L = 10;
R = 1;
r = R/(m+1);
S = sqrt(2);
s = S/(m+1);
d1 = 1;
d2 = 0.25;
omega = R/(3*(m+1));
gama = atan(R/d1);
% Functions
phi_i = @(ii, m) atan( (d1+(ii*d2)./(m+1) )./(r/3) );
B_j = @(ii, m) 2*phi_i(ii, m);
H = @(m) d1 + s*( cos(gama) + sum(cos(gama+(pi/2-phi_i(1:m)))) ) + ( (2*r)-omega)*sin(B_j(m) ) + (r-omega)*sum(sin((1:m-1)*pi - sum(B_j(1:m-1))));

回答 (1 件)

Tommy
Tommy 2020 年 4 月 7 日

1 投票

Where you call phi_i or B_j within the definition of H, you need to supply the arguments ii and m. For example,
..(pi/2-phi_i)..
is problematic because MATLAB thinks you are trying to subtract the function phi_i from pi/2. If you pass arguments,
..(pi/2-phi_i(1,m))..
for example, MATLAB will instead subtract the output of phi_i from pi/2.

7 件のコメント

Jay
Jay 2020 年 4 月 7 日
I input avalue for the variable m.
I also want to substract phi from pi/2
Tommy
Tommy 2020 年 4 月 7 日
Run this from the command window:
>> B_j(3)
Not enough input arguments.
Error in answers>@(ii,m)2*phi_i(ii,m)
The function B_j takes two inputs, ii and m, and calls phi_i(ii, m). If you call B_j(3), you are only giving B_j the first input, ii. It does not know what m is, and when it tries to call phi_i, it cannot. You need to pass both arguments into B_j:
>> B_j(1,3)
ans =
2.9851
Now run this from the command window:
>> 2+phi_i
Undefined operator '+' for input arguments of type
'function_handle'.
phi_i is a function. You cannot add or subtract anything to/from it. You can, however, do this:
>> 2+phi_i(1,3)
ans =
3.4925
Fill all of your function calls with both arguments and you shouldn't get any errors.
Jay
Jay 2020 年 4 月 7 日
It's there
B_j = @(ii, m) 2*phi_i(ii, m)
and for the others too
Tommy
Tommy 2020 年 4 月 7 日
That is where you define B_j. You call B_j twice within H() (and you call phi_i once):
H = @(m) d1 + s*( cos(gama) + sum(cos(gama+(pi/2- ...
phi_i ... HERE
))) ) + ( (2*r)-omega)*sin( ...
B_j(m) ... HERE
) + (r-omega)*sum(sin((1:m-1)*pi - sum( ...
B_j(1:m-1) ... HERE
)));
All three of those lines need to be changed, as described above.
Something like this would work:
H = @(m) d1 + s*( cos(gama) + sum(cos(gama+(pi/2- ...
phi_i(1,m) ... HERE
))) ) + ( (2*r)-omega)*sin( ...
B_j(1,m) ... HERE
) + (r-omega)*sum(sin((1:m-1)*pi - sum( ...
B_j(1,1:m-1) ... HERE
)));
I have used 1 for ii in all three lines, but I do not know if that is the value for ii that you want to use.
Jay
Jay 2020 年 4 月 7 日
Yeah exactly I am using ii = 1. But, for the first B_j I'm using 'm' only
Tommy
Tommy 2020 年 4 月 7 日
You cannot use m only, because your definition of B_j
B_j = @(ii, m) 2*phi_i(ii, m)
requires both ii and m.
Steven Lord
Steven Lord 2020 年 4 月 7 日
Calling B_j, which requires two inputs, with just one would be like me asking you "The first number is 7. What is the sum of the two numbers?" There's not enough information to answer the question.

この質問は閉じられています。

製品

リリース

R2017b

質問済み:

Jay
2020 年 4 月 7 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by