How can I perform a piecewise linear fit to data?

103 ビュー (過去 30 日間)
Ranjan Sonalkar
Ranjan Sonalkar 2015 年 5 月 19 日
コメント済み: Priyank Pathak 2021 年 6 月 23 日
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.

回答 (2 件)

Image Analyst
Image Analyst 2015 年 5 月 19 日
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 件のコメント
Priyank Pathak
Priyank Pathak 2021 年 6 月 23 日
okay @Image Analyst ......
i will try
thank you

サインインしてコメントする。


Greg Dionne
Greg Dionne 2016 年 7 月 8 日
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