How would I go about getting a nonlinear least-squares fit of a segmented curve? In this case, I have a short, linear, lag period followed by a logistic growth phase (typical of bacterial growth in culture).
Thus, for x < T0, y = Y0; for x >= T0, y = Y0 + (Plateau-Y0)*(1 - exp(-K*(X-X0)).
I need least squares estimates for each of the parameters: T0, Y0, Plateau, and K
I've attempted to use a custom function in the curve fitting toolbox, but cannot figure out how to allow for the two curves.
Thanks!

 채택된 답변

Teja Muppirala
Teja Muppirala 2012년 12월 5일

1 개 추천

It is no problem to fit piecewise curves in MATLAB using the Curve Fitting Toolbox. You can deal with piecewise functions by multiplying each piece by its respective domain. For example:
rng(0); %Just fixing the random number generator for initial conditions
X = (0:0.01:10)';
% True Values
Y0_true = 3;
PLATEAU_true = 5;
K_true = 1;
X0_true = 4;
Y = [Y0_true] * (X <= X0_true) + [Y0_true + (PLATEAU_true-Y0_true)*(1 - exp(-K_true*(X-X0_true)))].* (X > X0_true);
Y = Y + 0.1*randn(size(Y));
plot(X,Y);
ftobj = fittype('[Y0] * (x <= X0) + [Y0 + (PLATEAU-Y0)*(1 - exp(-K*(x-X0)))].* (x > X0)');
cfobj = fit(X,Y,ftobj,'startpoint',rand(4,1))
hold on;
plot(X,cfobj(X),'r','linewidth',2);

댓글 수: 5

The only situation where this approach might fail is if any of the piecewise functions can blow up to infinity, since 0*inf = nan, and that will leave you with a nan in your function. For example, if you were trying to do something like this:
y = 1/(x-3) for 0 <= x <= 2
y = x for 2 < x <= 4
In this case, I think you'll just have to write the piecewise function using if/else/end inside a separate file.
function y = f(x)
if x <= 2
y = 1/(x-3);
else
y = x;
end
It's slightly more work the the answer above, but you can still use such a user-defined file as the equation for a curve fitting operation.
Eric
Eric 2012년 12월 5일
Thanks, Teja! You've made my day!
Mr M.
Mr M. 2015년 9월 17일
I think this method is not working for me! The problem is that noncontinous parameters are not fitted, they remains unchanged, such as a in the following example:
X = [-1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0]; Y = [0.1 -0.15 0.05 0.75 0.05 10.2 9.9 9.5 8.2 10.2 10.0 10.5 9.8 9.9 10.5 10.1];
equation = '(1+sign(x-a))*b';
hint = [0.5 15];
f = fit(X',Y',equation,'Start',hint);
plot(f,X,Y)
user001
user001 2019년 9월 26일
편집: user001 2019년 9월 26일
This does not work for me in R2017b. The fit is effectively the line y=4 (K=4.2, plateau=4, X0 = -1.94, Y0 = -1.98). The fit works only if noise is not added to the simulated data.
Jonathan Gößwein
Jonathan Gößwein 2022년 10월 14일
The problem are the startpoints, rand(4,1) does not work indeed, but with an appropriate selection the method works (e.g. the true values).

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

John Petersen
John Petersen 2012년 12월 4일

1 개 추천

Not sure how you would do that, but you could try using a sigmoid function which will get you close, relatively speaking. Something like, for example,
y2 = Y0 + (Plateau-Y0)./(1 + exp(-K*(X-X0)));

댓글 수: 1

Eric
Eric 2012년 12월 5일
Thanks much for your rapid response, John! However, in order for this to be acceptable for publication (and more fundamentally, for comparison with other work), I need to be able to fit it to the originally specified equation. The values of those parameters are used to describe the response in a way that's meaningful for the relevant research community. The frustrating part is that this is an amazingly simple operation with other software (GraphPad Prism) with a built-in equation. I just can't figure out how to specify a segmented equation in a way that the Curve Fitting Toolbox will understand. Perhaps there's something analogous with how a discontinuous function would be entered?

댓글을 달려면 로그인하십시오.

laoya
laoya 2013년 5월 14일

0 개 추천

Hi Teja Muppirala,
I am also interested in this topic. Now my problem is: if the express of curves are not expressed explicitly, but should be calculated by functions, how to use this function?
Thanks, Tang Laoya

카테고리

도움말 센터File Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

질문:

2012년 12월 4일

댓글:

2022년 10월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by