Function Iterator - MATLAB Cody - MATLAB Central

Problem 24. Function Iterator

Difficulty:Rate

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

38.73% Correct | 61.27% Incorrect
Last Solution submitted on May 09, 2025

Problem Comments

Solution Comments

Show comments
PIVlab surpasses 100K all-time File Exchange downloads
During the past twelve months, PIVlab, a MATLAB Community Toolbox for particle...
4
8
LLMs with MATLAB updated to support the latest OpenAI Models
Large Languge model with MATLAB, a free add-on that lets you access...
2
4

Problem Recent Solvers2183

Suggested Problems

More from this Author96

Community Treasure Hunt

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

Start Hunting!