help-octave
[Top][All Lists]
Advanced

[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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]