在Matlab中获取拟合函数的函数句柄并分配拟合参数 [英] Get function handle of fit function in matlab and assign fit parameters

查看:508
本文介绍了在Matlab中获取拟合函数的函数句柄并分配拟合参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将自定义函数适合我的数据. 获得适合度后,我想获得类似于我的适合度函数的函数句柄,包括设置为适合度所找到的参数. 我知道我可以使用

I'm fitting custom functions to my data. After obtaining the fit I would like to get something like a function handle of my fit function including the parameters set to the ones found by the fit. I know I can get the model with

formula(fit) 

我可以使用

coeffvalues(fit) 

但是有什么简单的方法可以将两者合而为一?

but is there any easy way to combine the two in one step?

推荐答案

这个小循环可以解决问题:

This little loop will do the 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);

for 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 to be adapted to the number of coefficients of your fit.

为了完全回答问题,做了两个小改动.

two slight changes in order to fully answer the question.

fhandle = @(x) eval(output)

返回一个函数句柄.其次,您的过程给出的输出无效,因为幂运算读取.^而不是x,显然可以用

returns a function handle. Secondly output as given by your procedure doesn't work, as the power operation reads .^ instead of x, which can obviously be replaced by

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

这篇关于在Matlab中获取拟合函数的函数句柄并分配拟合参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆