Scipy ODR python

2024-06-17 13:27:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用odr将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。你知道我的代码怎么了吗?在

谢谢你的帮助


Tags: 代码fromimportreturndef半径常数calc
1条回答
网友
1楼 · 发布于 2024-06-17 13:27:59

您忽略了您提供的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,这似乎符合您的期望。

相关问题 更多 >