fitting data with exponential + linear form

3 views (last 30 days)
찬 소
찬 소 on 20 Aug 2020
Answered: Md Shariful Islam on 22 Jun 2023
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
찬 소
찬 소 on 21 Aug 2020
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.
Alan Stevens
Alan Stevens on 21 Aug 2020
Edited: Alan Stevens on 21 Aug 2020
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.


Md Shariful Islam
Md Shariful Islam on 22 Jun 2023
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

Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!