Z依赖于X和Y的向量的情节表面

发布于 2025-01-30 18:26:19 字数 1137 浏览 0 评论 0原文

我目前正在研究一个小型Python脚本,该脚本可用于使用径向基函数方法插值点。因此,我想绘制一个Z值由Z值计算的表面,该向量取决于X和Y。

我需要实现的公式看起来像这样:

我当前的方法是:

import numpy as np
import matplotlib.pyplot as plt 

def phi(x):
    return np.exp(- np.power(x, 2))

fig = plt.figure(figsize=(8,7))
ax = fig.add_subplot(projection='3d')

x = np.arange(0, 6, 0.25)
y = np.arange(0, 6, 0.25)
X, Y = np.meshgrid(x, y)

Z = 0.49 * phi(np.linalg.norm(np.array([[1,1], [X,Y]]))) + \
    0.79 * phi(np.linalg.norm(np.array([[2,3], [X,Y]]))) + \
    0.39 * phi(np.linalg.norm(np.array([[4,2], [X,Y]])))

surf = ax.plot_surface(X, Y, Z, cmap='viridis')

plt.xlim(0, 6)
plt.ylim(0, 6)
plt.show()

我传递给phi()的参数似乎有问题 - 但我不确定它是什么。 那么如何正确计算Z值呢?

注意:我发现了一个类似的问题,但是答案对我没有帮助。

I'm currently working on a small python script which can be used to interpolate points with a radial basis function approach. Therefore I would like to plot a surface where the Z value is calculated by a vector which depends on X and Y.

The formula I need to implement looks like this:
enter image description here

My current approach is the following:

import numpy as np
import matplotlib.pyplot as plt 

def phi(x):
    return np.exp(- np.power(x, 2))

fig = plt.figure(figsize=(8,7))
ax = fig.add_subplot(projection='3d')

x = np.arange(0, 6, 0.25)
y = np.arange(0, 6, 0.25)
X, Y = np.meshgrid(x, y)

Z = 0.49 * phi(np.linalg.norm(np.array([[1,1], [X,Y]]))) + \
    0.79 * phi(np.linalg.norm(np.array([[2,3], [X,Y]]))) + \
    0.39 * phi(np.linalg.norm(np.array([[4,2], [X,Y]])))

surf = ax.plot_surface(X, Y, Z, cmap='viridis')

plt.xlim(0, 6)
plt.ylim(0, 6)
plt.show()

Something seems to be wrong with the parameter that I pass to the phi()-function but I'm not sure what it is.
So how can I calculate the Z value correctly?

Note: I found a similar question, but the answer did not help me in my case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明明#如月 2025-02-06 18:26:19

正如评论中提到的@mateo瓶,最简单的方法是用毕达哥拉斯公式计算规范。工作代码看起来像这样:

import numpy as np
import matplotlib.pyplot as plt 

def phi(x):
    return np.exp(- np.power(x, 2))

fig = plt.figure(figsize=(8,7))
ax = fig.add_subplot(projection='3d')

x = np.arange(0, 6, 0.25)
y = np.arange(0, 6, 0.25)
X, Y = np.meshgrid(x, y)

Z = 0.49 * phi(np.sqrt((1-X)**2+(1-Y)**2)) + \
    0.79 * phi(np.sqrt((2-X)**2+(3-Y)**2)) + \
    0.39 * phi(np.sqrt((4-X)**2+(2-Y)**2))

surf = ax.plot_surface(X, Y, Z, cmap='viridis')

plt.xlim(0, 6)
plt.ylim(0, 6)
plt.show()

现在结果是具有径向基函数的良好插值。

As @Mateo Vial mentioned in the comments, the simplest approach is to calculate the norm with the pythagorean formula. The working code looks like this:

import numpy as np
import matplotlib.pyplot as plt 

def phi(x):
    return np.exp(- np.power(x, 2))

fig = plt.figure(figsize=(8,7))
ax = fig.add_subplot(projection='3d')

x = np.arange(0, 6, 0.25)
y = np.arange(0, 6, 0.25)
X, Y = np.meshgrid(x, y)

Z = 0.49 * phi(np.sqrt((1-X)**2+(1-Y)**2)) + \
    0.79 * phi(np.sqrt((2-X)**2+(3-Y)**2)) + \
    0.39 * phi(np.sqrt((4-X)**2+(2-Y)**2))

surf = ax.plot_surface(X, Y, Z, cmap='viridis')

plt.xlim(0, 6)
plt.ylim(0, 6)
plt.show()

And now the result is a good interpolation with a radial basis function.
enter image description here

偏闹i 2025-02-06 18:26:19

这是重构您拥有的一种方法:

import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as plt 

def phi(x):
    return np.exp(-np.power(x, 2))

fig, ax = plt.subplots(figsize=(8, 7))

x = y = np.arange(0, 6, 0.25)
X, Y = np.meshgrid(x, y)
xy = np.stack([X.flat, Y.flat]).T

Z = (0.49 * phi(la.norm(np.array([[1, 1]]) - xy, axis=1)) +
     0.79 * phi(la.norm(np.array([[2, 3]]) - xy, axis=1)) +
     0.39 * phi(la.norm(np.array([[4, 2]]) - xy, axis=1))
    )

surf = ax.imshow(Z.reshape(X.shape))
plt.show()

这会产生:

”输出网格作为映射”

Here's one way to refactor what you have:

import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as plt 

def phi(x):
    return np.exp(-np.power(x, 2))

fig, ax = plt.subplots(figsize=(8, 7))

x = y = np.arange(0, 6, 0.25)
X, Y = np.meshgrid(x, y)
xy = np.stack([X.flat, Y.flat]).T

Z = (0.49 * phi(la.norm(np.array([[1, 1]]) - xy, axis=1)) +
     0.79 * phi(la.norm(np.array([[2, 3]]) - xy, axis=1)) +
     0.39 * phi(la.norm(np.array([[4, 2]]) - xy, axis=1))
    )

surf = ax.imshow(Z.reshape(X.shape))
plt.show()

This produces:

The output grid as a map

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文