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
Show
24 older comments
N/A
on 10 Dec 2021
Help,help, I can't do it.
Anna
on 20 Oct 2023
usefully skills learned while doing problems.
Christian Schröder
on 20 Oct 2023
@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 Solvers2165
Suggested Problems
-
Make the vector [1 2 3 4 5 6 7 8 9 10]
50223 Solvers
-
Find all elements less than 0 or greater than 10 and replace them with NaN
15613 Solvers
-
2763 Solvers
-
Vectorize the digits of an Integer
325 Solvers
-
626 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!