Evaluate the output of a function returning functions at a given value

6 views (last 30 days)
I am currently working on some of the excercises proposed by the problem groups on the site and I came across one dealing with function handles and interation. To give you some context, the problem wants me to program a function 'iterate_fcn' that accepts a function handle fh and a nonnegative integer n as inputs, and outputs a new function handle fh2 such that fh2=fh^n (in other words fh2 is the composition of fh with itself n times). After several minutes of head scratching I came up with a code that works, except for one thing: say I want to evaluate the output fh2 at a value x. My first instinct was to type fh2(x), which of course doesn't work since the output of 'iterate_fcn' is stored in the variable 'ans'. My question then is: is there a way to store the output of a function in a different variable? The problem itself doesn't expect me to do it, and perhaps it even isn't useful at all, but I got curious.

Answers (1)

Stephen23
Stephen23 on 21 May 2020
Edited: Stephen23 on 22 May 2020
Storing the output of a function in a variable is explained in the Introductory Tutorials:
It states "Return output from a function by assigning it to a variable", and shows exaclty how.
For the situation that you explained in your question, you could simply do this:
fh = ...
fun = iterate_fcn(fh);
fun(x)
Yes I deliberately chose a different name for the output than the name you used inside the function workspace, to make it clear that they are different workspaces and that the output variable name is irrelevant. The fact that it is called something else inside the function makes no difference. Try it.
PS: relying on ans is not a good way to write code. You should always assign the output of a function to a variable.
  3 Comments
Steven Lord
Steven Lord on 22 May 2020
As Stephen said, the name of the variable inside the function that you return as the output and the name of the variable to which you assign that output when you call it do not need to match. They are independent. Let's say we had these two functions.
function qwerty = example529338(zxcvb)
qwerty = abracadabra(zxcvb+1);
end
function thisIsTheOutput = abracadabra(x)
thisIsTheOutput = 2*x;
end
example529338 neither knows nor cares what variable insides abracadabra it defines as its input or its output. abracadabra neither knows nor cares what variable or expression was used to calculate the value it received as input and neither knows nor cares to what variable or expression example529338 will assign the value it returns.
This is a bit of a simplification; in reality the functions can get access to a bit more information about each other, but I recommend sticking with this simplified version until you've developed a little more knowledge of MATLAB.
Stephen23
Stephen23 on 22 May 2020
Edited: Stephen23 on 22 May 2020
"However, I get an error saying that fh isn't defined,"
then you did not define fh and call the function like I showed you in my answer.
"whenever I write something like iterate_fcn(@(x)x+1,10)"
then you are not assigning the output to a variable. You should always assign any function output/s to a variable (and not rely on the default ans). How to assign the output to a variable is explained at the very start of my answer.
somevarname = anyfunction(...)
%^^^^^^^^^^^^ you need to do this!
Exactly as I wrote in my answer and as Steven Lord wrote in their comment, the names you used for any variables inside the function workspace are (or should be) irrelevant. This applies to your own functions, third-party functions, and to MATLAB functions too: for example, when we calculate the square root of a number:
val = sqrt(5)
do we know the names of any variables used inside that function? (hint: no) Does that matter? (hint: no)
We simply assign the output to whatever variable we want. You should do the same: forget about what happens inside your function, that is a totally different workspace:

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!