MATLAB: Convert data to a function using polyfit

MATLABmatlab codermatlab functionmatrix manipulation

i have used polyfit function to fit my data and to have as an output a function and i have got that plot the red one is the polyfit function , the blue is the one i have it data , as you see they are close to each other but
P(t)= =2.995134969444502*(10^3)*(t^10)+(2.454194242827056*(10^3))*(t^9)-(3.074449680687996*(10^4))*(t^8)-(1.831814183200164*(10^4))*(t^7)+(1.225116448716314*(10^5))*(t^6)+(1.225116448716314*(10^5))*(t^5)-(2.265044070414277*(10^5))*(t^4)+(6.259310423328208*(10^2))*(t^3)+(1.720638633336634*(10^5))*(t^2)+(8.587517861009773*(10^4))*t+(2.102731998692876*(10^5));
so P(t=0)= (2.102731998692876*(10^5)) different from zero
what should i do to make it close ?

Best Answer

This is almost what would normally be called a broken stick regression, except the joints between the linear segments are curves and pretty smooth.
Is a polynomial ever a good choice to try to fit a curve that has essentially linear segments? Not really. Polynomials just don't like that kind of behavior, where they are SOMETIMES linear, but reasonably curved in other regions.
Can you fit a polynomial of the desired form, such that it will be forced through 0 at time 0? Well, yes. You could use my polyfitn tool. You can even trick polyfit to do so, but that would cause other problems, that you won't know how to deal with.
Simplest is to just use backslash. This code will run in R2016b or later:
P = (time.^(10:-1:1))\u;
P(end+1) = 0; % stuff a zero constant term so polyval will work
plot(time,u,'b.')
hold on
ti = linspace(0,max(time),1000);
plot(ti,polyval(P,ti),'r-')
What I have done here is to recognize that to fit a polynomial that is FORCED to pass through the point (0,0) just requires no constant term in the model. That is, the constant term must be EXACTLY zero.
As you can see, at zero, we get the desired behavior. We get exactly zero.
polyval(P,0)
ans =
0
The curve actually dips considerably below zero, but you have no data there at all. In fact, your curve fit as produced had virtually the same behavior, but your plot was not sufficiently carefully done to show that. I'll show a blowup of the plot you would have produced near zero for your fit:
As you can see, it dives down quite a way below zero. If you are worried about missing EXACTLY zero, then why are you not worried about that behavior? The small error at zero in your model was insignificant compared to the extent it dove down below zero. However, you missed that due to the way you plotted the curve, because you never evaluated the curve between the 1st and second data points!
Could I have done better? Yes, most assuredly so, but not by using a polynomial. The absolutely simplest solution is to just use a spline interpolation. Your data is not at all noisy, so an interpolating function will be a good choice. I can get that from the functions spline and ppval, or I could just have used interp1.
plot(time,u,'.b',ti,interp1(time,u,ti,'pchip'),'r-')
I used pchip to produce a curve that will NOT dip even by the slightest amount below zero there. So I could have done it like this too:
spl = pchip(time,u);
plot(time,u,'.b',ti,ppval(spl,ti),'r-')
The only thing you cannot do is write down the actual function on paper, as you have done. But there is no need to do so. The model is entirely contained in the variable spl there.