I'm trying to define a sequence of functions recursively. The example below (which is obviously silly) illustrates the idea, which is to construct
f{:} such that f{i}(x) = f{ii-1}(x)^2
The problem with the code below is that
f{3} = f{2} = @(x)RecursiveFun(x,f{ii-1})
whereas I want
f{2} = @(x)RecursiveFun(x,f{1})f{3} = @(x)RecursiveFun(x,f{2})
In short, for some reason matlab is not substituting in the passed value of ii.
Obviously I can accomplish what I want by creating a loop of strings and eval'ing each string to create the desired anonymous functions, but everybody on this forum always says not to use eval's.
Thanks for any help!
Here's the code
f{1} = @(x) x^2; Recursivefun = @(x,F) F(x)^2; for ii=2:3; f{ii} = @(x) RecursiveFun(x,f{ii-1}); end; disp(['f{2}=']); f{2} disp(['f{3}=']); f{3}
Best Answer