3

Having data of an exponential decay available, I would like to fit a curve through it. How can I do that in Matlab?

Ingo
  • 1,732
  • 10
  • 26
  • 34
  • 4
    There is a nice demo on the Statistics Toolbox product page that discusses problems that can occur if you transform a nonlinear model to a linear one. http://www.mathworks.com/products/statistics/demos.html?file=/products/demos/shipping/stats/xform2lineardemo.html The preferred method is to use nonlinear regression; deriving a set a "smart" starting conditions using the "linearize, least squares, delinearize" technique. – richard willey Mar 24 '11 at 17:40
  • 3
    The comment by Richard Willey should really be the (accepted) answer. Linearizing and applying least squares as recommended in some of the answers is not a good idea since the transformation will give too much weight to small values. Better use nlinfit. – Lukas Mar 26 '12 at 12:54
  • Link from @richardwilley has been broken by The MathWorks. Updated version appears to be [here](http://www.mathworks.com/help/stats/examples/curve-fitting-and-distribution-fitting.html). – horchler May 15 '15 at 20:41

5 Answers5

5

Try this:

ft=fittype('exp1');
cf=fit(time,data,ft)

This is when time and data are your data vectors; time is the independent variable and data is the dependent variable.

This will give you the coefficients of the exponential decay curve.

eggy
  • 2,836
  • 3
  • 23
  • 37
Adiel
  • 3,071
  • 15
  • 21
  • how would one plot this? – Nick Dec 11 '14 at 11:53
  • @Adiel When i tried this I get an error as `Undefined function 'diff' for input arguments of type 'cfit'. Error in cfit/plot (line 64) if any(diff(xdata)<0) Error in slopeFirst2hours (line 6) plot(time, cf, 'r') ` Can you tell me what I am doing wrong – clarkson Sep 05 '17 at 11:23
  • @clarkson You are right. It should be plotted with a single command- `plot(cf,time,data)` – Adiel Sep 05 '17 at 12:06
2

If by fit you mean least squares, you should try lsqcurvefit

user389419
  • 243
  • 1
  • 2
  • 8
1

cftool(X,Y) is the way to go. here's some linkage:

LINK1 LINK2

Marcin
  • 3,437
  • 1
  • 22
  • 15
0

Matlab has a function called polyfit. It can fit curve to a data which can be represented in the form a*X^n+b*X^(n-1)+.....z. However if you are sure that the data is of some exponential decay you can try taking logarithm of the data first and then using the polyfit function. I thing that will work.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
bubble
  • 3,408
  • 5
  • 29
  • 51
0

Linearise, least squares, delinearise :-)

regularfry
  • 3,248
  • 2
  • 21
  • 27
  • You are right, although I thought there might be a package that does that for me automatically it is probably less time consuming to just do it manually rather than searching for it :) – Ingo Mar 23 '11 at 10:10
  • This is pretty short for an answer, but I upvoted it from -1 because it is helpful for users without the Curve Fitting toolbox. – FvD Jan 06 '15 at 08:11