MATLAB: How to compose a function n-times and want value for a particular value of n

functionsMATLAB Compilermatlab function

Suppose f(x)=x^2+1. Composition of two f, I mean f(f(x)), Similarly composition of 3 f is f(f(f(x))). n-th composition of f is denoted by f^n(x).
What is the general command to evaluate f^10(2) ?

Best Answer

f = @(x) x.^2-x;
n = 10;
x = 1/pi;
y = x;
for k=1:n
y = f(y)
end
% y is f^10(x)