Piecewise Least Square Fit Polynomial
    4 views (last 30 days)
  
       Show older comments
    
I am trying to determine a piecewise least square fit  polynomial for a set of data. I have already done least square fit polynomial for a global fit and piecewise fit. However, I would like to make a function that for large variations in local slope uses a larger order of polynomial (lets say 5) and small timesteps. Conversely, I would like the function for small variations in local slope to use a smaller order polynomial (lets say 2) and large time steps. Here is what I have so far:
clear;clc;
tt=linspace(0,1,1000);
A=sin(tt); %for simplicity
for i=1:length(A)
    slope=gradient(A)/.001;
    delta=gradient(slope);
    d=abs(delta);
    Ao=A(1:i);
if d(i)>.1                %large variation small  window
    windowsize=10;
    No=5;
elseif d(i)<.1             %small variation large window
    windowsize=100;
    No=2;
   end
   end
How can I divide the tt & A matrices for these conditions? Any help would be much appreciated.
2 Comments
  John D'Errico
      
      
 on 23 Mar 2022
				Sorry, but I think ypur goals here are not well conceived and thought out.
First, most people who want to fit piecewise polynomials somehow expect the functions will also be continuous, and even differentiable across the breaks between intervals. There is nothing in twhat you are asking to do that handles that. A big problem here is the ends of your data in any segment are exactly where the derivatives of a polynomial become most uncertain in such a fit.
Next, enforcing even continuity across the boundaries of moderately high order polynomials (say degree 4 or 5) will involve solving systems of nonlinear polynomial equations, or better yet, working with polynomial basis functions of some sort. So you would then be implicitly doing a spline fit of some sort. Why not just use a spline of some sort, where someone who understands splines will have done the heavy lifting for you already?
Next, it seems you want this scheme to work automatically, knowing where to use a high order polynomial, and where to use a low order one. But is that easily inferred, just from the gradient?
For example, I can show you data with a high gradient, that will absolutely be best fit with a strainght line. At the same time, I can give you a curve with low gradient that would clearly require a moderately high order polynomial.
As well, your code does not seem to recognize that the gradient function produces a local estimate of the derivative (assuming the time step between points was 1, as you are using the gradient function) but you want to think about not the slope of the curve at a single point, but the shape over an entire interval.
Does your data have any noise in it at all? Here, you need to recognize that the local gradient of a noise containing process wiill be high. In fact, there you are trying to solve exactly the wrong problem. Where the data has high noise compared to the local signal, then you want to use a low order fit, because a high order polynomial will just be chasing the noise. Conversely, it is where the noise is small compared to the signal, that you can afford to use a (moderately) higher order fit. So any noise in your data will produce exactly the wrong behavior from your proposed scheme.
Anyway, automatically choosing where to place the breakpoints in any sort of piecewise fit is a problem that will give you heartache, and many headaches.
Answers (0)
See Also
Categories
				Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
