Plateau followed by one phase decay

Good morning, I am trying to figure out how to compute tau constants from my data
My data could be be fitted by such plateau followed by one phase decay function:
I tried to implement it in MATLAB as follows:
x = 0:0.5:20; % time in seconds
Y0 = -0.6; % signal baseline value
Plateau = -1; % singnal plateu after trigger/stimulus, maximum change from baseline
tau = 0.6; % exponenential decay constant
K = 1/tau; % rate constant in units reciprocal of the x-axis units
X0 = 5; % trigger time
y = Plateau+(Y0-Plateau)*exp(-K*(x-X0));
figure;plot(x,y,'k');
However, I get the following result:
I would have 2 questions:
1) why cant I reproduce the one phase decay function?
2) would you know how to use the matlab funciton "fit" for such data with plateau followed by one phase decay function?
Thanks community for your kind support,
Best regards.

Answers (2)

Like this?
x = 0:0.5:20; % time in seconds
Y0 = -0.6; % signal baseline value
Plateau = -1; % singnal plateu after trigger/stimulus, maximum change from baseline
tau = 0.6; % exponenential decay constant
K = 1/tau; % rate constant in units reciprocal of the x-axis units
X0 = 5; % trigger time
y = Y0*(x<=X0)+(Plateau+(Y0-Plateau)*exp(-K*(x-X0))).*(x>X0);
figure;plot(x,y,'k');

5 Comments

Francesco
Francesco on 26 Feb 2024
Edited: Francesco on 26 Feb 2024
Thanks Alan, indeed it seems exactly what I needed and works, I tried to vary tau and I can see the slop changing accordingly :)
Would you know if it is possible to fit such function to experimental data?
Best regards.
Yes, you shouold be able to get a best fit using Matlab (your graph looks like you've done a reasonable job by eye!). What parameters do you want to fit - tau, tau and X0, tau, Xo and Y0, ...?
If you upload your data, I could have a go at fitting it.
Francesco
Francesco on 26 Feb 2024
Edited: Francesco on 26 Feb 2024
Thanks for the kind reply, in the example above I did not fit, I just plotted some randomized data on the fit for example purpose.
here some data to fit
x = [0 0.500000000000000 1 1.50000000000000 2 2.50000000000000 3 3.50000000000000 4 4.50000000000000 5 5.50000000000000 6 6.50000000000000 7 7.50000000000000 8 8.50000000000000 9 9.50000000000000 10 10.5000000000000 11 11.5000000000000 12 12.5000000000000 13 13.5000000000000 14 14.5000000000000 15 15.5000000000000 16 16.5000000000000 17 17.5000000000000 18 18.5000000000000 19 19.5000000000000 20]
y = [-0.137055262721364 -0.118841612584876 -0.274602636741299 -0.117324828772196 -0.173528150754918 -0.280491919000118 -0.244300356226590 -0.367583069701879 -0.423274105143034 -0.529129050767333 -0.774173830727337 -0.676677606159725 -0.730062482232667 -0.863905715495076 -0.831675679632950 -0.987303352625066 -0.949979744575626 -0.865710605996821 -0.901728879393798 -0.877082148456042 -0.944693953430828 -1.07404346760035 -0.915521627715257 -0.901789963321291 -0.955365771797851 -0.941530617721837 -0.945983148775748 -1.01735658137382 -0.965635004813717 -1.06321643780048 -0.956807780654745 -1.09208906741553 -1.04341265165344 -1.08982901817714 -1.07984413818039 -0.934740294823467 -0.960591807908718 -1.03623550995537 -0.909687220130007 -1.09290177705358 -1.01208835337351]
figure;plot(x,y)
I guess the tricky part would be to automatically compute Y0 and X0
Best regards
Here's a quick fit of tau and Y0. I'll leave you to tidy it up and extend it to fit X0 as well.
x = 0:0.5:20;
y = [-0.137055262721364 -0.118841612584876 -0.274602636741299 -0.117324828772196 ...
-0.173528150754918 -0.280491919000118 -0.244300356226590 -0.367583069701879 ...
-0.423274105143034 -0.529129050767333 -0.774173830727337 -0.676677606159725 ...
-0.730062482232667 -0.863905715495076 -0.831675679632950 -0.987303352625066 ...
-0.949979744575626 -0.865710605996821 -0.901728879393798 -0.877082148456042 ...
-0.944693953430828 -1.07404346760035 -0.915521627715257 -0.901789963321291 ...
-0.955365771797851 -0.941530617721837 -0.945983148775748 -1.01735658137382 ...
-0.965635004813717 -1.06321643780048 -0.956807780654745 -1.09208906741553 ...
-1.04341265165344 -1.08982901817714 -1.07984413818039 -0.934740294823467 ...
-0.960591807908718 -1.03623550995537 -0.909687220130007 -1.09290177705358 ...
-1.01208835337351];
Plateau = -1;
X0 = 2;
fn = @(x,tau,Y0)Y0*(x<=X0)+(Plateau+(Y0-Plateau)*exp(-(x-X0)/tau)).*(x>X0);
tauY0 = [1, -0.1]; % Initial guess
tauY = fminsearch(@(tauY) F(tauY,x,y), tauY0);
tau = tauY(1); Y0 = tauY(2);
yfit = fn(x,tau,Y0);
plot(x,y,'.',x,yfit), grid
xlabel('x'), ylabel('y')
text(12,-0.25,['tau = ' num2str(tau)])
text(12,-0.35,['Y0 = ' num2str(Y0)])
function Z = F(tauY,x,y)
tau = tauY(1); Y0 = tauY(2);
Plateau = -1;
X0 = 2;
yvals = zeros(1,numel(x));
for i = 1:numel(x)
t = x(i) - X0;
yvals(i) = Y0*(t<=0)+(Plateau+(Y0-Plateau)*exp(-t/tau)).*(t>0);
end
Z = norm(yvals-y);
end
Hello, I tried to implement the solution above, however I did not manage to have single function that provides all relevant parameters such as R squared, and Tau (or K).
For my analysis, Y0, X0 and Plateau are not relevant as main outputs.
Community help is greatly appreciated.

Sign in to comment.

Francesco
Francesco on 27 Feb 2024
Thanks Alan for your fantastic help, this is of great help, I guess for now this is resolved :) and I will figure out if there will be need for a function to automatically find X0. Best regards.

Asked:

on 26 Feb 2024

Commented:

on 25 Apr 2024

Community Treasure Hunt

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

Start Hunting!