gpt4 book ai didi

python-2.7 - 如何将2D椭圆拟合到给定点

转载 作者:行者123 更新时间:2023-12-03 23:13:36 25 4
gpt4 key购买 nike

我想通过椭圆函数拟合二维数组:(x/a)²+(y/b)²= 1 ----> (并得到a和b)

然后,可以将其重新绘制在我的图表上。
我在互联网上发现了许多示例,但没有人知道这个简单的笛卡尔方程式。我可能搜索不好!我认为该问题的基本解决方案可以帮助许多人。

这是数据的示例:

enter image description here

遗憾的是,我无法输入值...因此,让我们假设我有一个X,Y数组,定义了每个点的坐标。

最佳答案

这可以使用最小二乘法直接解决。您可以将其构架为使数量的平方和最小化(alpha * x_i ^ 2 + beta * y_i ^ 2-1-1),其中alpha为1/a ^ 2,beta为1/b ^ 2。您在X中拥有所有x_i,在Y中拥有y_i,因此您可以找到|| Ax-b || ^ 2的极小值,其中A是Nx2矩阵(即[X ^ 2,Y ^ 2]),x是列向量[alpha; beta]和b是所有列的列向量。

以下代码解决了形式为Ax ^ 2 + Bxy + Cy ^ 2 + Dx + Ey = 1的椭圆的更普遍的问题,尽管这个想法是完全相同的。打印语句给出0.0776x ^ 2 + 0.0315xy + 0.125y ^ 2 + 0.00457x + 0.00314y = 1并且生成的椭圆图像也在下面

import numpy as np
import matplotlib.pyplot as plt
alpha = 5
beta = 3
N = 500
DIM = 2

np.random.seed(2)

# Generate random points on the unit circle by sampling uniform angles
theta = np.random.uniform(0, 2*np.pi, (N,1))
eps_noise = 0.2 * np.random.normal(size=[N,1])
circle = np.hstack([np.cos(theta), np.sin(theta)])

# Stretch and rotate circle to an ellipse with random linear tranformation
B = np.random.randint(-3, 3, (DIM, DIM))
noisy_ellipse = circle.dot(B) + eps_noise

# Extract x coords and y coords of the ellipse as column vectors
X = noisy_ellipse[:,0:1]
Y = noisy_ellipse[:,1:]

# Formulate and solve the least squares problem ||Ax - b ||^2
A = np.hstack([X**2, X * Y, Y**2, X, Y])
b = np.ones_like(X)
x = np.linalg.lstsq(A, b)[0].squeeze()

# Print the equation of the ellipse in standard form
print('The ellipse is given by {0:.3}x^2 + {1:.3}xy+{2:.3}y^2+{3:.3}x+{4:.3}y = 1'.format(x[0], x[1],x[2],x[3],x[4]))

# Plot the noisy data
plt.scatter(X, Y, label='Data Points')

# Plot the original ellipse from which the data was generated
phi = np.linspace(0, 2*np.pi, 1000).reshape((1000,1))
c = np.hstack([np.cos(phi), np.sin(phi)])
ground_truth_ellipse = c.dot(B)
plt.plot(ground_truth_ellipse[:,0], ground_truth_ellipse[:,1], 'k--', label='Generating Ellipse')

# Plot the least squares ellipse
x_coord = np.linspace(-5,5,300)
y_coord = np.linspace(-5,5,300)
X_coord, Y_coord = np.meshgrid(x_coord, y_coord)
Z_coord = x[0] * X_coord ** 2 + x[1] * X_coord * Y_coord + x[2] * Y_coord**2 + x[3] * X_coord + x[4] * Y_coord
plt.contour(X_coord, Y_coord, Z_coord, levels=[1], colors=('r'), linewidths=2)

plt.legend()
plt.xlabel('X')
plt.ylabel('Y')
plt.show()

enter image description here

关于python-2.7 - 如何将2D椭圆拟合到给定点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47873759/

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