Stuart’s MATLAB Videos

Piecewise linear curve fitting 11

Posted by Doug Hull,

This week, I am getting back to my blogging roots and I want to highlight (and thank) John D’errico for a great file Exchange submission. That file is Shape Language Modeling.

Recently I had some very noisy data that I wanted to fit a curve to. The problem is there were different regions that were somewhat linear, but the overall data was far from linear. A spline or a polynomial did not capture the behavior in a global sense. What I wanted was the ability to fit a line to each region. SLM does that right out of the box.

I think it is very instructive to look at how this code was packaged up. There is a clearly labeled tutorial that was easy to scan looking for an example like what I wanted to do. All of the files have lots of comments. sometimes more comments that code I think. The files are named coherently so they make a nice package. These are all great things to make a package that other people can use.

11 CommentsOldest to Newest


Nice! I love it when I can learn about MATLAB functions that I did not know exist! It is a very large product, impossible to know it all!


Nice pick Doug – did you have to specify how many linear segments there are in your data? If so, do you have to specify the location of the knots?


Yeah, code would have been nice – how can I learn this without an example to display some metacognitiion? Not trying to complain, but kind of lame.

Hi Doug, great code. I was wondering if there was a way to specify a range for the interior knots. For example, if I have x-axis data ranging in value from 0 to 10, and I want 4 knots, the first at 0, the last at 10, and the middle 2 at or between 4 and 8. Is there a way to make this happen using your slm toolbox? The ‘interior knots’ just puts the knots at any number great than 0 or less than 10, I believe. Thanks!

Add A Comment

What is 5 + 8?

Preview: hide

These postings are the author's and don't necessarily represent the opinions of MathWorks.