# How do I perform a segmented regression for linear and exponential piecewise curve

15 views (last 30 days)
Luqman on 5 Jul 2022
Commented: Sam Chak on 5 Jul 2022
Hi, everyone! I simulated this signal where the first segment is linear (constant), second segment is linear (positive gradient) and the third segment is an exponential decay. Is there a way for me to do a piecewise regression with 3 line segments - linear, linear and exponential? Thank you very much.
p/s: I think this is my first ever post, so I might not know the rules on how to ask a question proper, so if more information is needed, please let me know what other info I should provide! Thanks!

Saksham Gupta on 5 Jul 2022
Edited: Saksham Gupta on 5 Jul 2022
As per my understanding , you wish to know how to perform segmented regression in MATLAB.
You should try Curve Fitter App of MATLAB with your data set, using 'Interpolant' or 'Smoothing Spline' as fit type might be helpful with the dataset of your kind.
Here are a few file exchanges that you might find helpful as well:
You may also checkout this answer to a previously asked similar question
Luqman on 5 Jul 2022
Thanks, I'll look into these and see if I understand it.

Sam Chak on 5 Jul 2022
Edited: Sam Chak on 5 Jul 2022
There are 3 segments defined over certain intervals of t:
a constant
a straight line
and an exponential decay
So, the piecewise model probably look something like this:
t = linspace(0, 520, 52001);
x1 = 100*((0 <= t) & (t < 80));
x2 = (((600 - 100)/(120 - 80))*t - 900).*((80 <= t) & (t < 120));
x3 = 600*exp(-0.0025*1.7918*(t - 120)).*((120 <= t) & (t < 520));
x = x1 + x2 + x3;
plot(t, x, 'linewidth', 2), grid on,
xlim([0 520]), ylim([0 700])
Luqman on 5 Jul 2022
Yes, when I simulated the signal, I used 3 piecewise functions much like in your model.
Let's say I received a dataset just like that. The only thing I know for sure, is that there are 3 segments. 2 linear and 1 exponential. How do I do a least squares fit, for example, to obtain the coefficients in the (a) contant function, (b) order 1 function and (c) an exponential decay?
Sam Chak on 5 Jul 2022
Hi @Luqman, this is probably just one way of doing it:
• Isolate the data into 3 intervals,
• Fit them separately using what you learned about LS fitting in MATLAB, and
• Combine the fitted models as a whole.

### Categories

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

R2022a

### Community Treasure Hunt

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

Start Hunting!