## [Solved] Fitting exponential decay function

Asked by Grant Huckels

### Grant Huckels (view profile)

on 25 Jul 2019
Latest activity Answered by Image Analyst

### Image Analyst (view profile)

on 26 Jul 2019
I'm trying to fit an exponential decay to a dataset of x and y values (3001 each). Using other software I was able to calculate a k_off around 0.02 however using the fittype and fit to replicate this in MATLAB I get the following results:
Code:
s1 = sprintf('%f*exp(-koff*', y_equil); %(For y_equil = 0.148356)
s2 = 'x)+plateau'
eq_string = strcat(s1, s2);
f = fittype(eq_string);
f1 = fit(x,y, f)
plot(f1,x,y)
xlabel('Time (s)')
ylabel('Concentration')
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Results:
f1 =
General model:
f1(x) = 0.148356*exp(-koff*x)+plateau
Coefficients (with 95% confidence bounds):
koff = 5.888 (4.858, 6.917)
plateau = 0.1291 (0.1289, 0.1293)  This fit finds the correct value for the plateau but not for the k_off. For some reason the fit is horrible.
How do I correct this?
----------------------------------------------------------------
[Solved]: - I simply modified his code:
Credit to Jing Chen
https://www.mathworks.com/matlabcentral/fileexchange/37321-fitexponential-m
Corrected Code:
yBase = y_equil;
y = y - y_equil;
fh_objective = @(param) norm(param(2)+(param(3)-param(2))*exp(-param(1)*(x-x(1))) - y, 2);
initGuess(1) = -(y(2)-y(1))/(x(2)-x(1))/(y(1)-y(end));
initGuess(2) = y(end);
initGuess(3) = y(1);
param = fminsearch(fh_objective,initGuess);
k2 = param(1);
yInf = param(2) + yBase;
y0 = param(3) + yBase;
yFit2 = yInf + (y0-yInf) * exp(-k2*(x-x(1))); on 26 Jul 2019
Edited by dpb

### dpb (view profile)

on 26 Jul 2019

You forced the model to have f(0) = 0.148356 + 0.1291 = 0.2775.
It takes a really large decay constant to make up for that.
The initial value looks like it should be roughly that value of 0.148 so the multiplicating factor should be (0.148-plateau) if it is intended to fix that initial value rather than estimate three parameters instead of only two.
Try
y_equil=0.148356;
s=sprintf('(%f-plateau)*exp(-koff*x)+plateau', y_equil);
f = fittype(s)
f =
General model:
f(koff,plateau,x) = (0.148356-plateau)*exp(-koff*x)+plateau
>>
and see if don't have better luck

Grant Huckels

### Grant Huckels (view profile)

on 26 Jul 2019
Thank you for your time and effort! The k that I obtained using your code was around 0.8, and ~0.6 for the 'Robust' on.
However I found an alternative way thanks to Jing Chen's code. I left the updated material for future users that may encounter the same issues I did. ### Image Analyst (view profile)

Answer by Image Analyst

### Image Analyst (view profile)

on 26 Jul 2019

You can use fitnlm(). See attached demo. I can't use your actual data because you forgot to attach it. 