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

39 views (last 30 days)
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?
  8 Comments
John D'Errico
John D'Errico on 28 Sep 2019
I fail to see that it is computationally intensive in context of what you want to do, or why you think it is.
Yes, if you use the code I wrote to compute symbolic polynomials, then it will take some amount of time to run, simply because you will have a long list of polynomials to create. If there are hundreds or thousands of points in the curve, then there will be as many polynomial segments. Your choice to make, and made necessary by your decision to do the computation.
The simpel fact is, if you create a spline from MANY points, then you will have MANY polynomial functions to create. There is no single simple function you can write down.
If you are willing to work with the polynomials as simply a list of coefficients, then you can essentially go directly to that form, with no effort made at all. It is already stored in the struct as lists of coefficients. But that requires you now need to understand what a spline is, how it is stored, etc. So you can trade off computational complexity with knowledge. But you NEED that knowledge, otherwise you are stuck with computation.
As I've said several times, it seems to make little sense to compute these polynomials explicitly. But that is what you want, what you asked to do.
Zakarya Motea
Zakarya Motea on 30 Sep 2019
thanks again john.
It seems that, power fitting method (provided in curve fitting tool) also provide good shaped fit with less equations, which is i think a good alternative for pchip in the case of having data with an interval for each point. do you think so?

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 27 Sep 2019
Edited: John D'Errico on 27 Sep 2019
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:

More Answers (1)

Thiago Henrique Gomes Lobato
From the Curve fitting tool you can select fit -> save to workspace to get a structure of your fit. From that structure you can access the coefficients and breaks as follows (substitute fittedmodel for the name you saved your structure):
Coeffs = fittedmodel.p;
Breaks = fittedmodel.p.breaks;
Here you will have a set of 4 coefficients for each interval x1 given in the breaks that describe a equation as shown in the pchip matlab page https://de.mathworks.com/help/matlab/ref/pchip.html :
which means that for every interval you will have a different equation (make sure to check if matlab does any normalization on x, you can do this by just outputing the model alone as "fittedmodel" then enter in the command prompt). I'm not sure if this is what you wanted, but it is the closest you will get for the analytical representation of your fit function. If the goal is to have one general equation for your data I would advise another fit functions as, for example, a polynomial one.

Community Treasure Hunt

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

Start Hunting!