【问题标题】:Numerical calculation of curvature曲率的数值计算
【发布时间】:2018-05-30 12:07:17
【问题描述】:

我想计算局部曲率,即在每个点。我有一组在 x 中等距分布的数据点。下面是生成曲率的代码。

data=np.loadtxt('newsorted.txt') #data with uniform spacing
x=data[:,0]
y=data[:,1]

dx = np.gradient(data[:,0]) # first derivatives
dy = np.gradient(data[:,1])

d2x = np.gradient(dx) #second derivatives
d2y = np.gradient(dy)

cur = np.abs(d2y)/(1 + dy**2))**1.5 #curvature

下面是曲率(洋红色)的图像及其与解析(方程:-0.02*(x-500)**2 + 250)的比较(纯绿色)

为什么两者之间会有这么大的偏差?如何获得分析的精确值。

帮助表示赞赏。

【问题讨论】:

  • x 中的点等距是什么意思?您拥有的方程式是用于描述为x(t), y(t) 的平面的曲率。如果x 是您的自变量,那么dx/dx == 1。你的意思是在t 或任何你的自变量中等距?
  • 您的数据是什么样的? plot(x,y) 会有所帮助
  • @newstudent 没有看到数据就不可能知道发生了什么(我猜你看到通过两次推导放大了噪音)。分析的东西从何而来?
  • @Jonas 已更新。

标签: python numpy derivative


【解决方案1】:

我一直在使用您的值,发现它们不够平滑,无法计算曲率。事实上,即使是一阶导数也是有缺陷的。 原因如下:

您可以在蓝色中看到您的数据看起来像一条抛物线,它的导数应该看起来像一条直线,但事实并非如此。当你取二阶导数时,情况会变得更糟。红色是用 10000 个点计算的平滑抛物线(用 100 个点尝试,它的工作原理相同:完美的线条和曲率)。 我制作了一个小脚本来“丰富”您的数据,人为地增加点数,但它只会变得更糟,如果您想尝试,这是我的脚本。

import numpy as np
import matplotlib.pyplot as plt

def enrich(x, y):
    x2 = []
    y2 = []
    for i in range(len(x)-1):
        x2 += [x[i], (x[i] + x[i+1]) / 2]
        y2 += [y[i], (y[i] + y[i + 1]) / 2]
    x2 += [x[-1]]
    y2 += [y[-1]]
    assert len(x2) == len(y2)
    return x2, y2

data = np.loadtxt('newsorted.txt')
x = data[:, 0]
y = data[:, 1]

for _ in range(0):
    x, y = enrich(x, y)

dx = np.gradient(x, x)  # first derivatives
dy = np.gradient(y, x)

d2x = np.gradient(dx, x)  # second derivatives
d2y = np.gradient(dy, x)

cur = np.abs(d2y) / (np.sqrt(1 + dy ** 2)) ** 1.5  # curvature


# My interpolation with a lot of points made quickly
x2 = np.linspace(400, 600, num=100)
y2 = -0.0225*(x2 - 500)**2 + 250

dy2 = np.gradient(y2, x2)

d2y2 = np.gradient(dy2, x2)

cur2 = np.abs(d2y2) / (np.sqrt(1 + dy2 ** 2)) ** 1.5  # curvature

plt.figure(1)

plt.subplot(221)
plt.plot(x, y, 'b', x2, y2, 'r')
plt.legend(['new sorted values', 'My interpolation values'])
plt.title('y=f(x)')
plt.subplot(222)
plt.plot(x, cur, 'b', x2, cur2, 'r')
plt.legend(['new sorted values', 'My interpolation values'])
plt.title('curvature')
plt.subplot(223)
plt.plot(x, dy, 'b', x2, dy2, 'r')
plt.legend(['new sorted values', 'My interpolation values'])
plt.title('dy/dx')
plt.subplot(224)
plt.plot(x, d2y, 'b', x2, d2y2, 'r')
plt.legend(['new sorted values', 'My interpolation values'])
plt.title('d2y/dx2')

plt.show()

我的建议是使用抛物线对数据进行插值,并在此插值上计算尽可能多的点。

【讨论】:

  • 非常感谢您的回答。我对 dy/dx 也有同样的看法。其实你能告诉我“不光滑”是什么意思吗?也..如果我运行你的代码,我会得到所有内容的完全差异图。
  • 如果你没有得到相同的情节那很奇怪,它们有多大不同?关于“平滑度”,它指的是您的功能的可微性。在你的情况下,当我们绘制微分时,你的函数看起来不像是可微的。这就是为什么我建议你用真正的抛物线插值你的函数。
  • 您的 Y 轴在 3 个图中不同,您是否将我的代码粘贴到新的 python 文件中?或者可能是 python / matplotlib 版本不同?
  • 这可能是原因,我正在使用 python 3.6、matplotlib 2.2.2 和 numpy 1.14.3。首先尝试使用 python 3,如果还不够,请考虑升级 numpy 和 matplotlib。它应该可以解决问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多