gpt4 book ai didi

python - 使用径向基函数在球体上插值函数

转载 作者:太空狗 更新时间:2023-10-29 21:39:53 25 4
gpt4 key购买 nike

首先,一些背景知识:

我使用球谐函数作为球体表面的函数示例,例如本图中的前球体:

enter image description here

我制作了其中一个球体,根据其表面各点的调和函数值着色。我首先对大量点执行此操作,因此我的函数非常准确。我将其称为我的 fine 球体。

enter image description here

现在我有了我的 fine 球体,我在球体上取了相对较少的点。这些是我希望从训练数据中进行插值的点,我称它们为 interp 点。这是我的 interp 点,根据它们的值着色,绘制在我的 fine 球体上。

enter image description here

现在,该项目的目标是使用这些 interp 点来训练 SciPy Radial Basis Function在球体上插入我的函数。我能够使用以下方法做到这一点:

# Train the interpolation using interp coordinates
rbf = Rbf(interp.phi, interp.theta, harmonic13_coarse)
# The result of the interpolation on fine coordinates
interp_values = rbf(fine.phi, fine.theta)

产生了这个插值,绘制在球体上: enter image description here

希望通过最后一张图片,您可以看到我的问题。注意到穿过插值的线了吗?这是因为插值数据有边界。边界是因为我使用球坐标([0,pi] 和 [0,2pi] 处的边界)训练径向基函数。

rbf = Rbf(interp.phi, interp.theta, harmonic13_coarse)

我的目标,也是我发布此问题的原因,是使用球体上数据的 x、y、z 笛卡尔坐标在球体表面上插值我的函数。这样,由于球体没有边界,所以我不会像在球坐标系中那样出现这种边界错误。但是,我只是不知道该怎么做。

我试过简单地为 Rbf 函数提供 x、y、z 坐标和函数值。

rbf=Rbf(interp.x, interp.y, interp.z, harmonic13_coarse)
interp_values=rbf(fine.x,fine.y,fine.z)

但是 NumPy 抛出一个奇异矩阵错误

numpy.linalg.linalg.LinAlgError: singular matrix

有什么方法可以让 Rbf 以笛卡尔坐标为我的数据站点,每个站点都有函数值,并使其表现得像球坐标一样,但没有边界?从 Rbf 文档中,有属性 norm 用于定义不同的距离范数,我是否必须使用球形距离才能使其工作?

我对此非常困惑。如果您对在没有球坐标边界的球体上插值我的函数有任何想法,请告诉我。

这是我的完整代码:

import matplotlib.pyplot as plt
from matplotlib import cm, colors
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from scipy import special
from scipy.interpolate import Rbf
from collections import namedtuple
from mayavi import mlab

# Nice aliases
pi = np.pi
cos = np.cos
sin = np.sin

# Creating a sphere in Cartesian and Sphereical
# Saves coordinates as named tuples


def coordinates(r, n):
phi, theta = np.mgrid[0:pi:n, 0:2 * pi:n]
Coor = namedtuple('Coor', 'r phi theta x y z')
r = r
x = r * sin(phi) * cos(theta)
y = r * sin(phi) * sin(theta)
z = r * cos(phi)
return Coor(r, phi, theta, x, y, z)

# Creating a sphere
# fine is coordinates on a fine grid
# interp is coordinates on coarse grid for training interpolation
fine = coordinates(1, 100j)
interp = coordinates(1, 5j)


# Defining finection to colour sphere
# Here we are using a spherical harmonic
def harmonic(m, n, theta, phi):
return special.sph_harm(m, n, theta, phi).real
norm = colors.Normalize()

# One example of the harmonic function, for testing
harmonic13_fine = harmonic(1, 3, fine.theta, fine.phi)
harmonic13_coarse = harmonic(1, 3, interp.theta, interp.phi)


# Train the interpolation using interp coordinates
rbf = Rbf(interp.phi, interp.theta, harmonic13_coarse)
# The result of the interpolation on fine coordinates
interp_values = rbf(fine.phi, fine.theta)


rbf=Rbf(interp.x, interp.y, interp.z, harmonic13_coarse)
interp_values=rbf(fine.x,fine.y,fine.z)

#Figure of harmoinc function on sphere in fine cordinates
#Points3d showing interpolation training points coloured to their value
mlab.figure()
vmax, vmin = np.max(harmonic13_fine), np.min(harmonic13_fine)
mlab.mesh(fine.x, fine.y, fine.z, scalars=harmonic13_fine, vmax=vmax, vmin=vmin)
mlab.points3d(interp.x, interp.y, interp.z, harmonic13_coarse,
scale_factor=0.1, scale_mode='none', vmax=vmax, vmin=vmin)


#Figure showing results of rbf interpolation
mlab.figure()
vmax, vmin = np.max(harmonic13_fine), np.min(harmonic13_fine)
mlab.mesh(fine.x, fine.y, fine.z, scalars=interp_values)
# mlab.points3d(interp.x, interp.y, interp.z, scalars, scale_factor=0.1, scale_mode='none',vmax=vmax, vmin=vmin)

mlab.show()

最佳答案

您看到的边界是因为您正在将封闭曲面 (S2) 映射到开放曲面 (R2)。不管怎样,你都会有界限。流形的局部属性是兼容的,因此它适用于大部分球体,但不适用于全局,你得到一条线。

解决方法是使用图集而不是单个图表。 map 集是重叠图表的集合。在重叠区域,您需要定义权重,这是一个在每个图表上从 0 到 1 的平滑函数。 (抱歉,微分几何可能不是您期望听到的)。

如果您不想一路走到这里,您会注意到原始球体有一个赤道,其中方差最小。然后你可以旋转你的精细球体并使其与线重合。它不能解决您的问题,但肯定可以减轻它。

关于python - 使用径向基函数在球体上插值函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25169236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com