Undefined unary operator '-' for input arguments of type 'function_handle'.
4 次查看(过去 30 天)
显示 更早的评论
Hello everyone,
When trying to compute Pc = integral(integrand_Pca, 0, 1) + integral(integrand_Pcb, 1, inf) [ NOTE: integrand_Pca and integrand_Pcb are functions of just y]
it is displayed the following error:
Undefined operator '-' for input arguments of type 'function_handle'.
Error in SimulationAnalysis_INH_CDFofX>@(y)exp(-tau/p*y.^alpha*N0/2).*La.*PDFa
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 75)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);
Error in SimulationAnalysis_INH_CDFofX (line 107)
Pc = integral(integrand_Pca, 0, 1) + integral(integrand_Pcb, 1, inf) ;
Thank you in advance for your help!
4 个评论
Adam
2020-2-24
There's a black down arrow under the Run button. Click that and it should be an option.
回答(1 个)
Walter Roberson
2020-2-24
I predict that tau is a function handle and so -tau is trying to use the operator - on the function handle. You cannot do arithmetic operations on function handles.
The operations that are permitted on function handles are (working from memory):
- copying (including passing as a parameter)
- func2str() to get a character version of the handle
- display
- the little known functions() method that can give information about the function handle
- storing into an indexed variable, a structure field, a cell array entry, index (1) of an unindexed variableoor structure field, or into a {} index of a cell array, but not any () index other than 1 of an unindexed variable or structure field
- @ to take a copy of the handle
- or followed by () to invoke the function handle on a (possibly empty) list of parameters.
Note that if you have a function handle with no arguments, and list its name in an expression without () following, then that is a request for a copy operation, not a request to invoke it with no parameters. For example,
3 + rand %invokes rand with no arguments
F=@rand; 3+F %is a copy request for the handle together with a forbidden addition operation on the handle
F=@rand; 3+F() %invokes handle with no argument which invokes rand
2 个评论
Walter Roberson
2020-2-24
Note that if you have a function handle with no arguments, and list its name in an expression without () following, then that is a request for a copy operation, not a request to invoke it with no parameters.
Therefore when La is encountered in your code, that is a request to copy the handle for La, not a request to invoke La with no parameters. You then ask to multiply the handle by something, which is not a permitted operation.
Furthermore, function handles are self-contained. If you have a function handle defined with @(y) then that is a dummy variable name for whatever value is passed in to the handle when the handle is invoked. If I define
F = @(Q) Q.^2
Then that is the same as
F = @(Internal_6716755) Internal_6716755.^2
And either way if I were to define
Q = 11
A = F
Then because there is no () parameter list after the function handle name then that is a request to take a copy of the function handle, and is never a request to look through the current environment to see if there is a variable with the same name as was used in the @(Variable) part of the handle definition for the purpose of invoking the handle with the current value of that variable. The dummy variable name is in a different name-space than the calling environment and the Q in @(Q) would never trigger looking to see if it can find some variable with the same name in-scope in order to use that value when you have simply mentioned the name of the handle without invoking the handle. In every case if you have a function handle that you want to invoke, you must use () explicitly after the function handle name and pass in whatever value you want to invoke on.
As I pointed out earlier this differs from pure function names that are not function handles: I showed the example with rand where just mentioning the pure function name outside of a @ construct is enough to invoke it with no parameters. That does not happen for function handles.
Somehow I suspect that you are still going to miss the point despite my having explained the theory:
If you want your function handle La to be invoked then you must have () after it and the list of values to pass in.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!