Undefined unary operator '-' for input arguments of type 'function_handle'.

5 visualizaciones (últimos 30 días)
Maurilio Matracia
Maurilio Matracia el 24 de Feb. de 2020
Comentada: Adam el 24 de Feb. de 2020
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 comentarios
Maurilio Matracia
Maurilio Matracia el 24 de Feb. de 2020
Thank you, but I can't see the 'pause on errors' option, I only see the options attached.
Adam
Adam el 24 de Feb. de 2020
There's a black down arrow under the Run button. Click that and it should be an option.

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 24 de Feb. de 2020
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 comentarios
Maurilio Matracia
Maurilio Matracia el 24 de Feb. de 2020
Thank you, but actually tau is just a constant, whereas La and PDFa are functions of y... any other suggestion?
Walter Roberson
Walter Roberson el 24 de Feb. de 2020
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Structures en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by