Problem 24. Function Iterator
Given a handle fh to a function which takes a scalar input and returns a scalar output and an integer n >= 1, return a handle fh2 to a function which applies the given function n times.
Examples:
>> addOne = @(x)x+1; >> addTen = iterate_fcn(addOne, 10); >> addTen(3) ans = 13
>> squarer = @(a) a^2; >> fh2 = iterate_fcn(squarer, 3); >> fh2(3) ans = 6561
% Golden Ratio >> fh = @(y)sqrt(y+1); >> fh2 = iterate_fcn(fh,30); >> fh2(1) ans = 1.6180
Solution Stats
Problem Comments

27 Comments
This problem nicely illustrates a very frustrating aspect of Cody: if you have no idea how to solve a problem that involves some sort of 'trick', you are completely stuck with no chance to learn or improve upon your solution. I've spent an inordinate amount of time today trying a variety of solutions to this, and I suspect the correct solution involves something like arrayfun or cellfun or eval. But I can't get any solution to work, no matter how 'ugly' or 'brute force' it is. I'm really curious as to how to do this, since function handles clearly are an area I need to improve my skills in. At least with other problems you can usually come up with some sort of solution, go solve another problem, and then come back to look at all the 'elegant' solutions and hopefully learn from them. With problems like this that type of feedback cycle is impossible.
I would agree. I had an eval solution that worked on my older Matlab version, but using eval is prohibited by the test suite. Test suite also checks for more than 32 levels of parenthesis,ruling out a str2func solution :(
This is the kind of problem I love and hate at the same time, was worse than nightmare when I was a student. I was able to solve with great relief after searching by the keyword 'functions' in Matlab Help and looking at the examples presented there.
Right now, I'm in the same boat as Alan was when he wrote his comment.
Is there any chance of getting a "You win, Cody. Just tell me how to solve it, pretty please with sugar on top." button for some of these problems?
A little hint: funception
For anyone who gets stuck on this problem, or who wants more practice with function handles, I've created a few more similar problems: http://www.mathworks.com/matlabcentral/cody/problems/581functioncomposition, http://www.mathworks.com/matlabcentral/cody/problems/582functioncompositionharder, http://www.mathworks.com/matlabcentral/cody/problems/583implementacounter
@freddy: what is funception?
I really got stuck with this problem although I managed to solve those from David Hruska.
@CODY TEAM: I suggest that when a player reaches 1000 points , he receives a joker that he can use to get the solution for 1 problem ;)
@Aurelien Queffurust: My intention with funception was to combine func(tion) and (inc)eption  based on the film. Function Iterator is quite similar to this "new" word creation, but in the first case, one could use e.g. str2func solutions. My intention was point to think of a nested concept.
Ouf! I finally managed to solve it! FYI, I have created a trend for this issue with trendy : http://www.mathworks.com/matlabcentral/trendy/plots/862
to monitor how correct solutions are computed for this problem ;)
Now this happens to be the last problem in the Cody Challenge that I still have absolutley no idea how to solve. I skipped it it a while ago so I could work on the other problems. I am absolutely lost and feel very similar as Alan. Unfortunately, reading through the links you suggested also did not help. Would it be possible to at least provide a small hint on how to structure the code? I think I get what the code is supposed to do but I am completely in the dark about how to build it up. Any help will be greatly appreciated!
Don't give up Pascal. This problem has been THE nightmare for everybody. You can find some hints in the Loren's Blog (http://blogs.mathworks.com/loren/).
Still not making an progress. I know it's a little obtrusive, especially considering that this is just a game but would you mind if I wrote you an email explaining my thought processes so far?
I finally got it! Thanks again for your help :)
Hey..can sb help me? I'm stuck on Test1 and Test5. Why in Test1 the answer is pi? I thought it should be 50, as we pass 50 to the function and no matter how many iternations are it should return 50. And Test5  why 1.6180? We input 30 and iterate once so sqrt(31) is 5.5678. The rest of test seems to be ok for me.
this sort of problems i like to see more on Cody
Took me way longer than i thought it would.
phew! that was so hard for someone has little understanding of functions let alone sub functions, function handles and nested functions.Those are my tips by the way :) They're saying nested function is way to go but the hardest thing to nail for me was function handle actually!
Also first examples can be deceptive, at first I thought n was not the iteration time and found golden ratio to be sqrt(31) but actually they want you to iterate 1 with the golden ratio function, 30 times.
I don´t believe... I didn´t... Very interesting probleam really, easier to make in paper
Wow! This was a wild one to throw in this spot, after only 23 sort of basic problems for the ones that do the challenge in this order.
Shout out to those fellow members who pointed out to the nested functions documentation.
Probably it would help a lot if the explanation was a bit more clear. For example, that thing about "returns a scalar output and an integer n >= 1" is very confusing. The only thing that's returned is the scalar output. Probably you meant that the function takes a scalar input and an integer n>=1—which is actually true—but it's not clear.
Another thing, the examples can really be misleading, especially that addTen handle, which doesn't really mean "add ten" but "add one ten times."
Anyway, great problem. I see it served as an introduction function handles and nested functions for many, including myself.
That was tough !
New to subfunctions and getting the syntax correct (understanding it ) was tricky. Arguments in  out shake them all about.
new concept for me after 3 years. now gotta use
I solved the problem based off the following example:
http://matlab.izmiran.ru/help/techdoc/matlab_prog/ch_fun12.html
useful challenge!!!!
I don't know if anyone else has mentioned it but, is there a list of forbidden functions for each problem?
I had the whole thing worked out on my own Matlab install only to fail all the tests because I used func2str or str2func.
There have been other problems where a oneline solution was perfectly fine. It just doesn't seem to be following any kind of consistent rule.
Help，help， I can't do it.
usefully skills learned while doing problems.
@Yin Late reply, but: as far as I know, forbidden functions (on Cody in general) are, at least, assignin, builtin, dos, eval, evalc, evalin, inline, path, perl, rehash, str2func, system, and unix.
Solution Comments
Show commentsProblem Recent Solvers2122
Suggested Problems

1537 Solvers

Given a window, how many subsets of a vector sum positive
851 Solvers

14557 Solvers

1175 Solvers

Find the Oldest Person in a Room
17226 Solvers
More from this Author96
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!