python - Scipy ODR python

标签 python numpy scipy

我正在尝试使用非球面透镜公式将 9 个点的云拟合为圆锥曲线:

z(r) = r² /(R*(1+sqrt(1-(1+K)*(r²/R²))))

其中 R 是曲率半径,K 是圆锥常数,r = sqrt(x²+y²) 。 K 保持不变(已知值),R 正是我正在寻找的。我是从http://wiki.scipy.org/Cookbook/Least_Squares_Circle开始的用Python编写它。 我用于二次曲线的隐式形式是 r² - 2.R.Z + (1+K).Z²

这是我写的:

# -*- coding: cp1252 -*-
from scipy import  odr
from numpy import *

# Coordinates of the 3D points
X   =   [   0,  1,  -1, 0,  0,  0.5,    -0.5,   0,  0   ]
Y   =   [   0,  0,  0,  1,  -1, 0,  0,  0.5,    -0.5    ]
Z   =   [   0,  0.113696489,    0.113696489,    0.113696489,    0.113696489,    0.027933838,    0.027933838,    0.027933838,    0.027933838]

#constantes
Rc = 8
K = -0.8

def calc_r(x, y):
    return (x**2 + y**2)

def calc_z(r, R):
    return r**2 /(R*(1+sqrt(1-(1+K)*(r**2/R**2))))

def f_3(beta, M):

    r = calc_r(M[0],M[1])
    Z = calc_z(r, beta[0])

    return r**2 - 2*beta[0]*Z + (1+K)*Z**2


beta0 = [Rc]

lsc_data  = odr.Data(row_stack([X, Y]), y=1)
lsc_model = odr.Model(f_3, implicit = True)
lsc_odr   = odr.ODR(lsc_data, lsc_model, beta0)
lsc_out   = lsc_odr.run()

点描述了曲率半径为 4.5、二次曲线常数为 -0.8 的二次曲线。我的代码不起作用:通过 ODR,代码返回 R = 8(初始点),而不是 4.5。知道我的代码有什么问题吗?

感谢您的帮助

最佳答案

您忽略了您提供的 Z 数据。相反,您计算 Z 总是满足您定义的隐式方程,无论您传递什么参数。

def f_3(beta, M):

    r = calc_r(M[0],M[1])
    Z = M[2]

    return r**2 - 2*beta[0]*Z + (1+K)*Z**2

...
lsc_data  = odr.Data(row_stack([X, Y, Z]), y=1)

本次运行的结果为 R = 4.34911251 +- 0.30341252,这似乎符合您的预期。

关于python - Scipy ODR python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23995768/

相关文章:

python - 添加 Django 模型约束?

python - 计算 N 个样本和聚类质心之间的平方欧氏距离的最有效方法是什么?

python - Healpy:从数据到 Healpix map

python - (Python MySQLdb) 尝试将 UTF-8 插入 MySQL 时

python - 如何扩展受 CPU 限制的 Twisted 应用程序?

python - Azure TimeSeries API 调用发出 python 请求

python - 如何在 Python 中插入二维曲线

python - matplotlib 在 NaN 之后不继续行

python - 提前比较两个或多个 csv 文件

python - 使用多个 y 值进行插值