1

我有两组数据。一是名义形式。另一种是实际形式。问题是当我想单独计算表单错误时。当两组数据不是“相互重叠”时,这是一个大问题。这给出了还包括位置误差的错误。

两条曲线都是从一系列数据中读取的。标称形状(黑色)由许多彼此相切的不同尺寸半径组成。它是翼型轮廓的前缘。

我已经尝试了各种“最佳拟合”方法,我在这里和谷歌带我去的地方都找到了。但问题是它们都平滑了我的“实际”数据。所以它被修改并且没有保持它的实际形式。

scipy 或任何其他 python 库中是否有任何“简单地”可以在不改变实际形状的情况下将我的两条曲线拟合在一起的函数?我希望带有红点的绿色曲线尽可能地位于黑色之上。

在此处输入图像描述

是否可以计算两条曲线的重心,然后根据与中心点的值差在 x 和 y 中移动实际曲线?它可能不是最终的解决方案,但它会更接近?

4

1 回答 1

2

这是一个解决方案,假设名义形式可以描述为圆锥曲线,ia 是方程 a x^2 + b y^2 + c x y + d x + e y = 1 的解。然后,最小二乘拟合可用于查找系数 (a, b, c, d, e)。

import numpy as np
import matplotlib.pylab as plt

# Generate example data
t = np.linspace(-2, 2.5, 25)
e, theta = 0.5, 0.3  # ratio minor axis/major & orientation angle major axis
c, s = np.cos(theta), np.sin(theta)
x = c*np.cos(t) - s*e*np.sin(t)
y = s*np.cos(t) + c*e*np.sin(t)

# add noise:
xy = 4*np.vstack((x, y))
xy += .08 *np.random.randn(*xy.shape) + np.random.randn(2, 1)


# Least square fit by a generic conic equation
# a*x^2 + b*y^2 + c*x*y + d*x + e*y = 1
x, y = xy
x = x - x.mean()
y = y - y.mean()

M = np.vstack([x**2, y**2, x*y, x, y]).T
b = np.ones_like(x)
# solve M*w = b
w, res, rank, s = np.linalg.lstsq(M, b, rcond=None)
a, b, c, d, e = w

# Get x, y coordinates for the fitted ellipse:
# using polar coordinates
# x = r*cos(theta), y = r*sin(theta)
# for a given theta, the radius is obtained with the 2nd order eq.:
# (a*ct^2 + b*st^2 + c*cs*st)*r^2 + (d*ct + e*st)*r - 1 = 0
# with ct = cos(theta) and st = sin(theta)

theta = np.linspace(-np.pi, np.pi, 97)
ct, st = np.cos(theta), np.sin(theta)

A = a*ct**2 + b*st**2 + c*ct*st
B = d*ct + e*st
D = B**2 + 4*A
radius = (-B + np.sqrt(D))/2/A

# Graph
plt.plot(radius*ct, radius*st, '-k', label='fitted ellipse');
plt.plot(x, y, 'or', label='measured points');
plt.axis('equal'); plt.legend();
plt.xlabel('x'); plt.ylabel('y');

椭圆的最小二乘拟合

于 2019-07-05T08:54:34.487 回答