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: Martin Helm
Subject: Re: fixed points piecewise-linear fitting
Date: Tue, 20 Mar 2012 01:42:18 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:11.0) Gecko/20120312 Thunderbird/11.0

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-")



reply via email to

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