Get function handle of fit function in matlab and assign fit parameters -


i'm fitting custom functions data. after obtaining fit function handle of fit function including parameters set ones found fit. know can model

formula(fit)  

and can parameters

coeffvalues(fit)  

but there easy way combine 2 in 1 step?

this little loop trick:

x = (1:100).';  %' y = 1*x.^5 + 2*x.^4 + 3*x.^3 + 4*x.^2 + 5*x + 6; fitobject = fit(x,y,'poly5');    cvalues = coeffvalues(fitobject); cnames = coeffnames(fitobject); output = formula(fitobject);  ii=1:1:numel(cvalues)     cname = cnames{ii};     cvalue = num2str(cvalues(ii));     output = strrep(output, cname , cvalue); end  output = 1*x^5 + 2*x^4 + 3*x^3 + 4*x^2 + 5*x + 6 

the loop needs adapted number of coefficients of fit.

edit: 2 slight changes in order answer question.

fhandle = @(x) eval(output) 

returns function handle. secondly output given procedure doesn't work, power operation reads .^ instead of x, can replaced

strrep(output, '^', '.^'); 

Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -