问题
I have described a model and i would like to fit in the Curve,I am obtaining an Error in this context , kindly review it.
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
Following is the error i am obtaining
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:
Could you please review if i have used fittype expression correctly
回答1:
Create a function that holds the model:
function c = modelfnc(t, a1, a2, a3, b1, b2, b3, td, tmax)
...
end
Then put the rest of the code in the main function (another file). Also you need to define the variables and have some data before starting the fitting process.
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')
来源:https://stackoverflow.com/questions/22360239/matlab-error-in-curve-fitting