Azzera filtri
Azzera filtri

evaluation of anonymous function

33 visualizzazioni (ultimi 30 giorni)
632541 il 5 Mar 2021
Commentato: Steven Lord il 13 Mag 2024 alle 15:45
V=@(x) (0.5+0.5.*sign(r.^2-x.^2))*V0;
In this piece of code, how are the values assigned to V?
The fuction V is not used in any other block of code. What are the values of x? How is V evaluated?
Please clarify this?

Risposta accettata

Walter Roberson
Walter Roberson il 5 Mar 2021
In that code, V is never evaluated.
In that code, MATLAB sees the
@(x) (0.5+0.5.*sign(r.^2-x.^2))*V0
part and constructs an anonymous function. The construction of the anonymous function accepts the code (0.5+0.5.*sign(r.^2-x.^2))*V0 without evaluating it. It does, however, scan the code. Any variables that match the names given in the @() are replaced with an internal reference to the corresponding parameter position. Any variable that is not one of the parameters is looked for along the usual search path; if it is found, then a copy of the variable will be stored inside the data structure being constructed, and the data structure will be marked to look for that copy at that point in the code. If any named variable is not found, then the variable is marked as undefined in the data structure being constructed.
When the anonymous function handle is assigned to V, V is set to point to the data structure.
At no point in this is the code evaluated with any particular value for x.
The data structure constructed might look like
workspace{1}: struct('r', {1e-2}, 'V0', {1e-6});
code: times(plus(0.5, times(0.5, sign(minus(power(workspace{1}.r, 2),power(varargin{1},2))))), workspace{1}.V0)
formula: '(0.5+0.5.*sign(r.^2-x.^2))*V0'
file: ''
except the code is fully parsed and tokenized and a threaded datastructure is created. The structure used is the same as what MATLAB uses for regular functions (with static workspaces.)
Your code never tries to execute this function.
If, at some point, something called V() and passed in an a parameter, then the parameter would be submitted through the execution engine responsible for running the threaded interpreted datastructure. At that point, it will start execution. It will not try to look up the values of any variables at run time. For example, it will not check to see whether r has been assigned a new value: at the time the datastructure was built, the variables were copied into the datastructure and only that datastructure will be referred to. If a variable is encounted that was not defined at the time that the anonymous function was built, then at the time the datastructure was built, an "undefined" would have been built in to the datastructure, and an error will be triggered when the attempt is made to do the reference. This is true even if the variable was defined after the anonymous function was created.
  5 Commenti
Francisco il 13 Mag 2024 alle 15:30
Hi, sorry to reopen an old question, but I am intrigued.
Is there any command in Matlab (or third parties) to get the parsed "code" you mentioned in your answer? I mean, to get:
times(plus(0.5, times(0.5, sign(minus(power(workspace{1}.r, 2),power(varargin{1},2)))))
I have searched it wothout result.
Steven Lord
Steven Lord il 13 Mag 2024 alle 15:45
What would you hope to do with such information? It's possible there are alternate approaches you could use to achieve your goal without recreating the MATLAB parser.

Accedi per commentare.

Più risposte (1)

Jorg Woehl
Jorg Woehl il 5 Mar 2021
I am not quite sure if this is what you are asking, but I'll give it a shot:
The piece of code important to your question is
V=@(x) (0.5+0.5.*sign(r.^2-x.^2))*V0;
The sign function inside the anonymous function returns three possible values for real numbers: -1 for negative arguments, 0 if the argument is zero, and 1 for positive arguments.
Therefore, the result of the operation 0.5+0.5*sign(arg) is limited to 0 for negative arguments, 0.5 if arg is zero, and 1 for positive arguments. This is then multiplied by V0 and returned when you call the anonymous function with some argument x.
As a result, when calling V(x) the output can be one of the following:
  • 0 if , i.e. or
  • for
  • anywhere else, i.e.
For complex numbers, the sign function returns complex numbers with real and imaginary parts varying freely between -1 and 1, so the story is more interesting there.
  1 Commento
632541 il 5 Mar 2021
Thanks for the detailed analysis.
As a result, when calling V(x)
V(x) is not done in any part of the code. Used directly as a vector.
But in my code, value for x parameter is not passed.
Used as p=pressure( , V);
p is either vector or matrix.
Please try to clarify

Accedi per commentare.


Community Treasure Hunt

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

Start Hunting!

Translated by