fitting data with exponential + linear form

I want to fit my data into
y= a*exp(-x/b)+cx+d (exponential linear form).
I got number of data set of about 600.
If I put it in
y= a*exp(-x)+cx+d, it works although it doesn't fit 'best'
explinearfit = fittype({'exp(-x)','x','1'})
fo = fitoptions(explinearfit);
fo.normalize = 'on';
myFit = fit(t2,n,explinearfit);
plot(myFit,t2,n)
xlabel('time(t2)')
ylabel('electrons(n)')
legend('Data','Fitted graph')
title('Data and Fitted Curve')
clear opts
However, if I put 'b' inside the exponential, it goes way wrong.
explinearfit = fittype('a*exp(-x/b)+c*x+d')
fo = fitoptions(explinearfit);
fo.normalize = 'on';
myFit = fit(t2,n,explinearfit);
plot(myFit,t2,n)
xlabel('time(t2)')
ylabel('electrons(n)')
legend('Data','Fitted graph')
title('Data and Fitted Curve')
clear opts
How can I fix this?
The warning sign says that there are no starting points so they were randomly chosen.

Answers (2)

Alan Stevens
Alan Stevens on 20 Aug 2020
Edited: Alan Stevens on 20 Aug 2020
Try starting with the following guesses: a = 6E10, b = 4E-4, c = 0, d = -1.57E12.
with
explinearfit = fittype( @(a,b,c,d,x) a*exp(-x/b)+c*x+d );
(I'm guessing at the fittype as I don't have the Curve fitting toolbox myself!).

4 Comments

Hi, Alan, refer to the results below:
Root of Mean Square Error (RMSE): 1390205473.08086
Sum of Squared Residual: 1.16926611071731E21
Correlation Coef. (R): 0.992002295260181
R-Square: 0.984068553801468
Adjusted R-Square: 0.984015625408781
Parameter Best Estimate
---------- -------------
a 50867056286.6369
b 0.000427936677731703
c -1152953437385.39
d -1565359446337.44
Looks like a good fit!
Yeap! I got the reasonable fit as well.
But how did you guess the initial values?
I think I might also have other sets of data.
First I looked at x = 0 and set a+d to a value near the top of the points. Then at the other end it looked as if the curve might be asymptotically horizontal, so, with x set at infinity, I set c = 0, and d to a value near the bottom of the points (thus allowing me to get a as well). I then just plotted the resulting curve against the points, starting with b = 1, and adjusted b until I got what looked like a reasonable fit by eye (it took about four guesses) . The constants were never going to be perfect, but were likely to provide a decent initial guess.

Sign in to comment.

Hi,
In this code, do we just need to declare the initial guesses(a = 6E10, b = 4E-4, c = 0, d = -1.57E12) before following code?
I was trying to reproduce the plot with best fit as I encounter similar issues. However, I could not get a good fit rather I am getting a fit like 2nd image.
May I get a working code for this problem?
"
explinearfit = fittype('a*exp(-x/b)+c*x+d')
fo = fitoptions(explinearfit);
fo.normalize = 'on';
myFit = fit(t2,n,explinearfit);
plot(myFit,t2,n)
xlabel('time(t2)')
ylabel('electrons(n)')
legend('Data','Fitted graph')
title('Data and Fitted Curve')
"

Categories

Products

Release

R2020a

Asked:

on 20 Aug 2020

Answered:

on 22 Jun 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!