evaluation of anonymous function

26 views (last 30 days)
632541
632541 on 5 Mar 2021
Commented: 632541 on 5 Mar 2021
r=1e-2;
V0=1e-6;
m=1;
if(m<4)
V=@(x) (0.5+0.5.*sign(r.^2-x.^2))*V0;
else
abort
end
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?

Accepted Answer

Walter Roberson
Walter Roberson on 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.
  3 Comments

Sign in to comment.

More Answers (1)

Jorg Woehl
Jorg Woehl on 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
r=1e-2;
V0=1e-6;
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 Comment
632541
632541 on 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

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!