How to generate a 3D surface function to fit given 3D points and interpolate 3rd coordinate if I have other 3 coordinates

huangapple go评论130阅读模式
英文:

How to generate a 3D surface function to fit given 3D points and interpolate 3rd coordinate if I have other 3 coordinates

问题

我是Python的初学者。

我有一些3D点数据,如下所示,

x = [600, 450, 302, 151, 600, 450, 302, 151, 599, 450, 302, 150, 599, 449, 301, 150]
y = [0.4, 0.2, 0.1, 0.0, 2.1, 2, 2, 2, 4, 4.1, 4, 4.1, 6.1, 6.1, 6, 6]
z = [23.77, 17.74, 11.93, 5.97, 23.60, 17.60, 11.78, 5.82, 23.09, 17.24, 11.39, 5.39, 21.89, 16.18, 10.36, 4.26]

我想要拟合这些点的曲面,然后创建一个函数来表示这个曲面,这样,如果我想在这个曲面上绘制一个新点,并且我有它的两个坐标(x,y),我应该能够找到它的z坐标。

请问有人能帮助我,我该如何解决这个问题?

我尝试使用scipy.linalg.lstsq函数来拟合二次曲线,但我没有得到任何输出。

我只是期望得到一个图表和一个能够给我提供Z值的曲面方程,如果我有X和Y的话。

英文:

I am a beginer in Python.

I have some 3D point data as below,

x= [600, 450, 302, 151, 600, 450, 302, 151, 599, 450, 302, 150, 599, 449, 301, 150]
y= [0.4, 0.2, 0.1, 0.0, 2.1, 2, 2, 2, 4, 4.1, 4, 4.1, 6.1, 6.1, 6, 6]
z =[23.77, 17.74, 11.93,5.97, 23.60, 17.60, 11.78, 5.82, 23.09, 17.24, 11.39, 5.39, 21.89, 16.18, 10.36, 4.26]

I want to fit a surface to these points and then create a function of this surface such that if I want to plot a new point on this surface and I have two coordinates (x, y) of it then I should be able to find the z coordinate of the it.

Could someone please assist me, how can I proceed with this problem?

I tried using scipy.linalg.lstsq function to fit quadratic curve, but I am not getting any output.

I am simply expecting a plot and a surface equation which can give me the value of Z if i have X, and Y.

答案1

得分: 0

如果您想定义一个最佳拟合平面,而不必从零开始实现数学计算,您可以使用scikit-spatial库,如下所示(如示例中所解释的)。

安装该库(例如使用pip,但您也可以使用conda):

pip install scikit-spatial

导入库:

from skspatial.objects import Plane

定义您的点:

points = [[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]]

计算最佳拟合平面:

plane = Plane.best_fit(points)

您还可以使用plot_3d方法绘制平面。

此外,您可以使用cartesian方法获取平面的系数:

coefficients = plane.cartesian()

系数包括abcd,平面方程为ax + by + cz + d = 0。因此,给定xy坐标,您可以如下计算z坐标:

x = 3
y = 4
z = -(a*x + b*y + d) / c
print(z)

最后,如果您需要拟合二次曲面,您可以参考我的这个答案(不幸的是,它既未被接受也未被投票支持,因此我无法建议它作为重复问题)。

英文:

If you want to define a best-fit plane without implementing the math from zero, you can use the scikit-spatial library in the following way (as explained in the example).

Install the library (for instance using pip, but you can also use conda)

pip install scikit-spatial

Import the library:

from skspatial.objects import Plane

Define your points:

points = [[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]]

Compute the best-fit plane:

plane = Plane.best_fit(points)

You can also plot the plane using the plot_3d method.

Moreover, you can get the plane's coefficients using the cartesian method:

coefficients = plane.cartesian()

The coefficients are a, b, c and d and the plane equation is ax + by + cz + d = 0. Thus, given the x and y coordinates, then you can compute the z coordinate as follows:

x = 3
y = 4
z = -(a*x + b*y + d) / c
print(z)

Finally, if you need to fit a quadratic surface, you can take a look at this answer of mine (unfortunately, it's neither accepted nor upvoted, so I cannot suggest it as a duplicate).

答案2

得分: -1

线性回归与平面方程:

线性回归与二次曲面方程:

RLMSE:根最小均方误差。

注意:根据上述结果,因为A和C都很小,可以预期仅使用四个参数(而不是六个)的二次曲面方程 z = B y^2 + D x + E y + F 也能获得几乎同样好的结果。

计算细节:

英文:

If you have no clue for an equation model try several equations until you obtain an acceptable result.

Linear regression with the equation of plane :

How to generate a 3D surface function to fit given 3D points and interpolate 3rd coordinate if I have other 3 coordinates

Linear regression with equation of quadratic surface :

How to generate a 3D surface function to fit given 3D points and interpolate 3rd coordinate if I have other 3 coordinates

RLMSE : Root Least Mean Square Error.

Note : From the above result and because A and C are very small one can expect an almost as good result with the only four parameters (instead of six) equation of quadratique surface : z = B y^2 + D x + E y + F .

How to generate a 3D surface function to fit given 3D points and interpolate 3rd coordinate if I have other 3 coordinates

Details of the calculus :

How to generate a 3D surface function to fit given 3D points and interpolate 3rd coordinate if I have other 3 coordinates

huangapple
  • 本文由 发表于 2023年3月9日 17:55:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682937.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定