3D interpolating polynomial

13 次查看(过去 30 天)
Davide Bassano
Davide Bassano 2018-6-15
I have a very large set of 3D data (x,y,z) and I would like to find the interpolating polynomial. I know polyfit, but it works only for 2D data. Is there a MatLab function or Tool which could help me? I'd like to find something like P(x,y) = c1*x+c2*y+c3*x^2+...+cn*x^n*y^n, something like this or similar to this.
Thanks, Cheers,
Davide Bassano
  3 个评论
Davide Bassano
Davide Bassano 2018-6-18
Thanks for your answer. I'm aware that it is a "really bad idea", but I need the analytical definition of my volume (defined by a set of x,y,z points), anyway. I could use griddata or scatteredInterpolant, but they give me back only interpolated points, not the polynomial I need. What's the function people use to build polynomials with 2 variables?
Thanks, Davide
Davide Bassano
Davide Bassano 2018-6-18
I'm using your polyfitn function but it keeps plotting the evaluated polynomial as flat (blue points in attached figure) instead of a 3D cloud of points (red points). Red points are the real points I'm trying to find interpolating polynomial. How can I solve it?
Thanks,
Davide

请先登录,再进行评论。

回答(2 个)

John D'Errico
John D'Errico 2018-6-18
编辑:John D'Errico 2018-6-18
Now that you have shown the problem you are trying to solve...
I'm pretty sure I remember telling you that you should not use the function I posted. :) Sadly, it looks like you managed to find polyfitn, but did not believe me. While I admit that the reason I suggested you would not want to use polyfitn was because I was guessing you would try to fit too high order of a polynomial to some cloud of points, you still cannot use polyfitn here.
You have shown what is apparently a set of points on a sphere.
So, suppose that I gave you a simple equation of a sphere? It might look like this:
x^2 + y^2 + z^2 = 2
Although a classic form that would allow a non-zero center and some more general radius would look like this:
(x-x0)^2 + (y-y0)^2 + (z-z0)^2 = R^2
In either case, are these things a polynomial function of x? Are they a polynomial at all? Actually, no. They are not so, in either case. They look kind of polynomial-ish. But in fact, they are equations, describing what is technically called an implicit function .
Now, you might want to write them in the form
z(x,y) = +/- sqrt(2 - x^2 - y^2)
but even that is not a function. That little +/- in the beginning should be the clue that a problem exists, that this is not a single valued animal, so you cannot think of it as z(x,y).
A single valued function would have the form z(x,y). For any value of x and y that you would put in, you get ONE value of the function out. The polyfitn tool is designed to work with (estimate) polynomials. It applies to single-valued functions of one or more independent variables.
So, what do you have? You have a more difficult thing that is not in fact a single valued function at all. You have what might be also correctly described as a multivalued function. Really, it is the surface of what looks to be a sphere. I might guess this is a test case, that your real problem is not quite so simple, and you really have some more nasty looking 2-manifold embedded in R^3 that you want to fit as a polynomial. Or it may be really that simple, and you just want to find the equation of a sphere from some data points.
But you cannot use polyfitn to fit an implicit/multi-valued function. polyfitn assumes that you have data that represents a single valued function, here z(x,y). Pass in (x,y), get one z out. You don't have that.
You cannot use polyfitn. You cannot use the curve fitting toolbox. You cannot use regress. You cannot use scatteredInterpolant. You cannot use griddata. You cannot use my other tools, in case you chance upon gridfit.
What not? Again, you don't have data in the form of a single valued function. The surface of a sphere is not a function. It is defined by an implicit functional form, thus one of those I showed above.
What happens when you try to use a tool like polyfitn to data that lives on the surface of a sphere? You might get some simple plane, that passes through the center of the sphere, or you might get something more nasty looking that still fails for many reasons.
Now, maybe what you really want to do is just find the equation of a sphere from some data in three dimensions? You could look on the File Exchange, which is what I should probably tell you to do. There is a lot of crap on the FEX for things like this though. So I would need to find something on the FEX that does work, or I could just be lazy and attach circlefit.m to this answer. I did the latter. It finds the center and radius for a circle (or sphere, or hypersphere), fit to n-dimensional data. I wrote it many years go when playing around with some ideas for how I might robustly solve the problem, but it should still work.
To be honest, I'm not positive that in the end your real problem really is as simple as fitting a sphere to some data. It never is. :) In fact, I worry that your problem is something more general, because you talk about interpolating the surface. People show a sphere, but they will really want to fit the surface of some amorphous non-convex blob when you see their real data. So if it just fitting a sphere, then circlefit.m will do it simply enough. If it is the amorphous blob thingy, then you need to say so. I never got around to writing an amorphousblobfit.m though.
  2 个评论
Davide Bassano
Davide Bassano 2018-6-18
Unfortunately, I have to fit the volume (described by a cloud of scattered data) you can see in attached figure. From what you wrote, I understood that I have no possibilities to find what I'm looking for, that is the explicit function which describes my volume. Thank you very much for your answers and for your time.
Davide
John D'Errico
John D'Errico 2018-6-18
编辑:John D'Errico 2018-6-18
I thought that what you posted as an example was too good (since too simple) to be true. So now we are faced with the amorphous blob thingy. (The technical term.)
Finding an explicit "function" for something like this is impossible for the reasons I described. There is no explicit functional relationship to work with.
At best, it is a messy implicit function. And lacking a model for that relationship, you need to understand there are infinitely many possible surfaces that would pass through any set of data.
I hope you do not want to model the flat top and bottom surfaces of that blob too, as then it turns into a piecewise problem.
Can you turn this into a problem where you CAN visualize it as a single valued surface? Well, possibly. For example, convert to cylindrical coordinates. That is, instead of (x,y,z), convert your data into the surface
r(theta,z)
This will work reasonably well as long as the origin (x,y)=(0,0) lives inside that volume at any z. In that case, then you can convert (x,y) into (r,theta). Just use cart2pol. You can visualize the surface as r(theta,z), but remember that any surface modeling needs to have the surface be continuous across the 0/2*pi boundary. So tools like scatteredInterpolant or my own gridfit would fail there. A polynomial model would still suck royally. Don't try polyfitn and ask why it does not work well. I'd just say what I did before, that I told you not to bother. Polynomials don't handle periodic boundary conditions.
As I said, simple application of cart2pol will fail when the surface does not surround the origin, because it will no longer represent a single valued relationship. There are tricks you could apply, such as a spline transformation, where you model the centroid of this tube as a function of z, thus the origin would now become translated at any point along the z axis by the parametric path (xc(z),yc(z)).
So, based on what I've seen so far, I would convert to the r(theta,z) form. To build a model, I would try a model for r that was a simple (low order) Fourier series in theta, where the coefficients of that series were a function of z. This is something you can fit using not much more than a call to backslash, once the right matrices were built.
Now, I recall that you said you had a VERY large set of data. But the picture I see drawn was clearly a highly faceted thing. So I have a funny feeling I am still faced with some information that is lacking. :)

请先登录,再进行评论。


Davide Bassano
Davide Bassano 2018-6-19
My dataset has more than 2200 points, so I don't know if "very big" was the correct way to describe it.
I want to thank you for the idea of using cylindrical coordinates and my data are all around origin, so it could work. About finding my model with Fourier series, I thought about this series:
r(theta,z) = a0(z) + sum[ai(z)*cos(i*theta) + bi(z)*sin(i*theta)]
is it correct? Then I'll write this series with matrix and use \ to find "ai" and "bi".
Thanks
Davide
  1 个评论
John D'Errico
John D'Errico 2018-6-19
编辑:John D'Errico 2018-6-19
2200 points? lol. 2.2 million points is big. But big is always just a relative measure.
As far as the model you describe, that is what I was suggesting. This will form a 2-dimensional model, that because it is essentially a truncated Fourier series, will be nicely periodic in theta. I'd probably be lazy and use a simple piecewise linear form for the functions of z, thus a0(z), etc. It allows you to control the accuracy you want, by adding more Fourier terms, as well as more points along the z axis, all as needed. And while you could use higher order splines for the functions of z, that would take more effort for something that probably does not justify the time you would invest. (Also depends on your familiarity and skill using splines.)
And in the end, the entire fit will be accomplished by one large call to backslash, estimating all of the a's and b's in one call. No iterative estimation will be needed.
For example, if you set 20 nodes along the z axis, and 10 sin and cos terms each, that will give you 20*21=420 coefficients to estimate. And you have 2200 data points. That is entirely sufficient to do some significant smoothing, or you could easily allow more coefficients. The call to backslash will take you well under 1 second to compute. For example, on my rather slow, oldish CPU...
tic,rand(2200,420)\rand(2200,1);toc
Elapsed time is 0.205928 seconds.
Sincewhat you are building are sets of piecewise linear splines in z, you will need to determine which bin each data point lies in as a function of z.
The tools to do this is histcounts (at least in recent MATLAB releases, it replaces histc, which will also work.)
Use of histcounts will allow you to build the entire 2200x420 array in not much more than a few vectorized lines of code.
Finally, be careful. If you use too fine a resolution in z, such that some bins have no points at all, the linear system will become singular. There are ways to resolve that, but it is probably best dealt with by just not going too far in resolution along the z axis.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Polynomials 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by