简体   繁体   中英

Matlab :Error in Curve fitting

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM