[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: fixed points piecewise-linear fitting
From: |
Ben Abbott |
Subject: |
Re: fixed points piecewise-linear fitting |
Date: |
Mon, 19 Mar 2012 21:18:31 -0400 |
On Mar 19, 2012, at 8:42 PM, Martin Helm wrote:
> I want to add my poor man's solution for least squares fit of the
> piecewise polynomials
> returns the node values as first argument (makes only sense for the
> piecewise linear case) and the function values at all nodes as second
> argument.
> Simplistic but fast.
>
> function [y, yy] = ppfit2(x, xf, yf, method)
> a = zeros (length (xf), length (x));
> for ii = 1:length (x)
> p = zeros (length (x), 1);
> p(ii) = 1;
> a(:, ii) = interp1 (x(:), p, xf(:), method);
> endfor
> y = a\yf(:);
> if (nargout==2)
> yy = interp1 (x(:), y, xf(:), method);
> endif
> endfunction
>
> %!demo
> %! clf
> %! x = [0 1 2 3];
> %! xf = linspace(0, 3, 1000);
> %! yf = xf.^3 + rand(size(xf))*2.5;
> %! plot (xf, yf, "y-")
> %! hold on
> %! [y, yy] = ppfit2(x,xf,yf, "linear");
> %! plot (xf, yy, "r-")
> %! [y, yy] = ppfit2(x,xf,yf, "cubic");
> %! plot (xf, yy, "g-")
> %! [y, yy] = ppfit2(x,xf,yf, "spline");
> %! plot (xf, yy, "b-")
Nice!
I ran a comparison between your linear least squares solution and my fsolve
approach. With the default optimization values they are about the same speed.
However, I prefer linear least squares, so I'm modifying my local function.
Ben
- Re: fixed points piecewise-linear fitting, (continued)
- Re: fixed points piecewise-linear fitting, Sergei Steshenko, 2012/03/18
- Re: fixed points piecewise-linear fitting, c., 2012/03/18
- Re: fixed points piecewise-linear fitting, Ben Abbott, 2012/03/18
- Re: fixed points piecewise-linear fitting, CdeMills, 2012/03/17
- Re: fixed points piecewise-linear fitting, Sergei Steshenko, 2012/03/17
- Re: fixed points piecewise-linear fitting, Martin Helm, 2012/03/19
- Re: fixed points piecewise-linear fitting,
Ben Abbott <=
- Re: fixed points piecewise-linear fitting, Martin Helm, 2012/03/20
- Re: fixed points piecewise-linear fitting, Martin Helm, 2012/03/20
- Re: fixed points piecewise-linear fitting, Ben Abbott, 2012/03/20
- Re: fixed points piecewise-linear fitting, Martin Helm, 2012/03/20
- Re: fixed points piecewise-linear fitting, Ben Abbott, 2012/03/20
- Re: fixed points piecewise-linear fitting, Ben Abbott, 2012/03/20
- Re: fixed points piecewise-linear fitting, Martin Helm, 2012/03/20
- Re: fixed points piecewise-linear fitting, Ben Abbott, 2012/03/21
Re: fixed points piecewise-linear fitting, Macy, 2012/03/17
Re: fixed points piecewise-linear fitting, Macy, 2012/03/17