Doug's MATLAB Video Tutorials

March 2nd, 2012

Piecewise linear curve fitting

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 Responses to “Piecewise linear curve fitting”

  1. Gabriel replied on :

    hpfilter() ?

  2. Doug replied on :

    @Gabriel

    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!

    Thanks!
    Doug

  3. owr replied on :

    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?

    Thanks!

  4. Doug replied on :

    I did specify, but the function chooses nice defaults if I did not.

  5. Henry replied on :

    Hi Doug, can you show me the codes you used to generate the fitting?

  6. James replied on :

    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.

  7. Doug replied on :

    @Henry, James

    The link to the code is in the first paragraph of the post.

    http://www.mathworks.com/matlabcentral/fileexchange/24443-slm-shape-language-modeling

    I have confirmed it works.

    Doug

  8. Peyman replied on :

    MATLAB R2012a didn’t find “hpfilter” in help !!!

  9. doug replied on :

    @Petman,

    It is in the econometrics toolbox.

  10. Kevin replied on :

    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!

  11. Doug replied on :

    Check with the original author. I am only promoting it.

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


MathWorks

Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.

Doug's picture

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