MATLAB:错误的曲线拟合(Matlab :Error in Curve fitting)

2019-10-19 13:38发布

我所描述的模型,我想以适应曲线,我在这方面取得的错误,请审查。

function c = model(t, a1, a2, a3, b1, b2, b3, td, tmax)

c = zeros(size(t));

ind = (t > td) & (t < tmax);
c(ind) = (t(ind) - td) ./ (tmax - td) * (a1 + a2 + a3);

ind = (t >= tmax);
c(ind) = a1 * exp(-b1 * (t(ind) - tmax))+ a2 * exp(-b2 * (t(ind) - tmax)) + a3 * exp(-b3 * (t(ind) - tmax));




ft = fittype('model(t, a1, a2, a3, b1, b2, b3, td, tmax)', 'independent','t');
fo = fit(t, c, ft,'StartPoint', [20000, 20000, 20000, 0.01, 0.01, 0.01, 10, 30],'Lower', [0, 0, 0, 0, 0, 0, 0, 0]);
plot(t, c, 'x')
hold all
ts = 0:0.1:50;
plot(ts, model(ts, fo.a1, fo.a2, fo.a3, fo.b1, fo.b2, fo.b3, fo.td, fo.tmax))
axis([0 50 -2000 80000])
xlabel time
ylabel concentration    

end

以下就是我得到的错误

Error in fittype expression ==> model(t, a1, a2, a3, b1, b2, b3, td, tmax)
??? Expression model(t, a1, a2, a3, b1, b2, b3, td, tmax) is not a valid MATLAB
expression,
 has non-scalar coefficients, or cannot be evaluated:

能否请您查看我是否有正确使用fittype表达

Answer 1:

创建包含模型的函数:

function c = modelfnc(t, a1, a2, a3, b1, b2, b3, td, tmax)
...    
end

然后把代码的其余部分在主函数(其它文件)。 你也需要定义变量并启动安装过程之前,有一些数据。

define c,t,...
ft = fittype('modelfnc(t, a1, a2, a3, b1, b2, b3, td, tmax)', 'independent','t');
fo = fit(t, c, ft,'StartPoint', [20000, 20000, 20000, 0.01, 0.01, 0.01, 10, 30],'Lower', [0, 0, 0, 0, 0, 0, 0, 0]);
plot(t, c, 'x')


文章来源: Matlab :Error in Curve fitting