Matlab:曲线拟合错误

时间:2014-03-12 18:09:46

标签: matlab optimization curve-fitting estimation

我已经描述了一个模型,我想适应曲线,我在这个背景下得到一个错误,请仔细阅读。

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表达式

1 个答案:

答案 0 :(得分:1)

创建一个包含模型的函数:

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

然后将其余代码放在main函数中(另一个文件)。您还需要在开始拟合过程之前定义变量并获得一些数据。

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')