matlab - What is the right way to add function handles recursive and see the right output after printing it? -


this tried:

f = @(x) 0; = 1:n     f = @(x) f(x) + x^i; end 

it seems right thing, when test putting in values.

but when print f got output f = @(x) f(x) + x^i

edit: how output want see, summands apear in function handle, when print f.

you can use symbolic functions (symfun) want:

% create symbolic variable syms x  % define order of polynom n = 3;  % create polynom fs(x) = 0*x; = 1:n     fs(x) = fs(x) + x.^i; end  % convert symbolic function function handle f = matlabfunction(fs) 

results in:

f =      @(x)x+x.^2+x.^3 

edit: here approach without using symbolic math toolbox:

% define order of polynom n = 3;  % create polynom string fstring = '0'; = 1:n     fstring = [fstring, '+x.^',num2str(i)]; end  f = str2func(['@(x)',fstring]); 

results in:

f =      @(x)0+x.^1+x.^2+x.^3 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -