MATLAB: How do you get the equations of shape-preserving fitting method automatically

curve fittinginterpolation

This is one of the fitting methods provided by matlab (Curve fitting tool). it is under Interpolant. The difficulty in this method is that it gives its fitting results as piece-wise function. I want it in the form of equation, How to get it automatically?

Best Answer

The problem that Matt is getting at is, the equation of a set of points as fit by any spline (including shape preserving interpolants) is not any simple expression that you will want to write down or use. In fact, it is a complicated set of many functions. All is fine, as long as you just want to use it, because then you use one of the supplied tools to evaluate it, or work with it in some way.
Butm like many new users of spline tools, you think there is an equation you can see and write down. There is not anything simple.
Even for the simple case of the triangle curve shown by Matt, there are multiple questions that I would see. First, are you looking for a piecewise LINEAR function? However, that is not what a shape preserving interpolant would produce. It would produce a set of cubic polynomials.
For example, consider the triangle function.
x = -2:2;
y = [0 0 1 0 0];
pspl = pchip(x,y)
pspl =
struct with fields:
form: 'pp'
breaks: [-2 -1 0 1 2]
coefs: [4×4 double]
pieces: 4
order: 4
dim: 1
pchip is the tool I assume you want to use.
fplot(@(t) ppval(pspl,t),[-2,2])
hold on
plot(x,y,'or')
So we have a reasonably smooth curve that interpolates the points, and has the same general shape as they have. It has no extreme in it that did not exist in the original set of points. (That is a way I like to describe a shape preserving interpolant.)
Now, we can extract the actual polynomials in that curve, using part of my SLM toolbox. (IT is on the file exchange.)
funs = slmpar(pspl,'symabs');
>> funs{1,:}
ans =
-2 -1
ans =
-1 0
ans =
0 1
ans =
1 2
>> funs{2,:}
ans =
0.0
ans =
- 2.0*x^3 - 3.0*x^2 + 1.0
ans =
2.0*x^3 - 3.0*x^2 + 1.0
ans =
0.0
There are 4 intervals between points on that curve. We can interpret the output of slmpar simply. Over the intervals...
[-2 , -1] f(x) = 0
[-1 , 0] f(x) = -2.0*x^3 - 3.0*x^2 + 1.0
[ 0 , 1] f(x) = 2.0*x^3 - 3.0*x^2 + 1.0
[ 1 , 2] f(x) = 0
For more complex sets of points though, the cubic polynomials will get increasingly nasty.
You can find the SLM toolbox on the file exchange: