How can I perform a piecewise linear fit to data?

48 views (last 30 days)
Ranjan Sonalkar
Ranjan Sonalkar on 19 May 2015
Commented: Priyank Pathak on 23 Jun 2021
I have y-axis data that varies linearly in three regions over 0-1 on the x-axis. I would like to obtain a simple piecewise linear fit to get the 2-break points and the three linear fits. Is there a function that I could use? I have found nlhw that might do it, but I cannot figure out how to use it from the documentation. Would appreciate any help in using it or any other alternative.

Answers (2)

Image Analyst
Image Analyst on 19 May 2015
Do you have the location of the breakpoints? If so, simply use polyfit(). If not, then you should fit a line through some number of points, like 3 or 5, and scan that across your data. You can get the estimates slope at every location. Then plot the slopes and look for where it changes a lot. You can use diff() to find out where the slope changes substantially. Attach some sample data if you want people to kindly help you by writing code for you.
  3 Comments

Sign in to comment.


Greg Dionne
Greg Dionne on 8 Jul 2016
If you have the R2016a Signal Processing Toolbox, you can try FINDCHANGEPTS.

Community Treasure Hunt

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

Start Hunting!

Translated by