简体   繁体   中英

Custom Curve Fitting in MatLab

I am trying to define a custom Curve Fit in MatLab. The curve which I am trying to fit to is as follows:

A*exp(B*x)+C

The reason this fit is needed is because my data decays exponentially, however this data does not approach zero (in this data set, the curve should decay and approach some number above zero). Using fittype('exp2') is also not an option because it overfits the data (curves upwards towards the end when it should remain flat somewhere above 0). I define the fit type using the following code:

ft = fittype('(A*exp(B*x)) + C','coefficients', {'A', 'B', 'C'});

However, when I try to fit data to this curve I get the following error message:

"Error using cfit/subsref>iDotReference (line 66) Name is neither a coefficient or a problem parameter.

Error in cfit/subsref (line 19) out = iDotReference( obj, currsubs );"

I can't figure out exactly what MatLab is complaining about in this case. Sometimes, for reasons I do not know, the code will run but the fit is just terrible. Am I doing something wrong here? Is this the best way to an exponential that decays to some above 0 value?

The following code is how I try to run my fit:

[cf, gof] = fit(time', testArray', fittype);

Providing an initial guess helps the fit a great deal. What I found is that the correct sign of the initial values is especially important.

Anyway, I dont run into the problem you have if I run this code:

ft = fittype('(A*exp(B*x)) + C', 'coefficients', {'A', 'B', 'C'});

time = 0:0.1:20;
testArray = (4*exp(-.2*time) + 10) + normrnd(0,1,size(time));
[cf, gof] = fit(time', testArray', ft, 'StartPoint', [17 -.6 100]);

% check:
figure(1)
clf
hold on
plot(time,testArray,'k.')
plot(time, cf.A*exp(cf.B*time) + cf.C, 'r');

But I dont know if this solved your problem, since I cant reproduce your error.

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