Piecewise Least Square Fit Polynomial

4 views (last 30 days)
David Fariyike
David Fariyike on 23 Mar 2022
Commented: David Fariyike on 23 Mar 2022
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
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.
David Fariyike
David Fariyike on 23 Mar 2022
Thank you for the reply. I think that you got way too far ahead of yourself given the information or perhaps I did a bad explanation. It is for an assignment so I think it has to be possible at least in some capacity. A large portion of your reply is how "bad" the piecewise polynomial will be and that is not important to me. I do not need for the polynomials to be continous but everytime there is a discontinuity I need to make note of it. Additionally, the change in local slope can be a "bad" estimation of when to switch order of polynomial and window size but I am also okay with that and just need to make note of it. There will be noise added later but as of right now no noise.
I will try to explain the assignment so that you get a better feel for what I am trying to accomplish.
First Part: Global Fit of Decaying Sine Wave (already produces a "bad" fit) Complete.
Second Part: Piecewise Fit of that same wave (divided wave into eight equal length parts much better fit but discontinuities) Complete.
As you can see I already have "bad" fit and discontinuity so that is not an issue for the function I am trying to make.
Third Part: Function that switches windowsize and order for a change a chosen change in local slope. Basically trying to do the second part with much less code and the function can switch between 2 orders and window sizes as much as needed. In Progress.
Repeat all 3 parts with noise. Not Started.
I think if I can divide the tt and A matrices for everytime there is a switch in the 2 orders and window sizes I can complete the code from there.

Sign in to comment.

Answers (0)

Categories

Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!