Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scipy ODR python

I am trying to fit with odr a cloud of 9 points to a conic using the aspheric lens formula :

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

where R is the curvature radius, K the conic constant and r = sqrt(x²+y²). K is kept constant (known value), R is really what I'm looking for. I have started from http://wiki.scipy.org/Cookbook/Least_Squares_Circle to write it in python. The implicit form I use for the conic is r² - 2.R.Z + (1+K).Z²

This is what I wrote :

# -*- 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()

Points describe a conic with a curvature radius of 4.5 and a conic constant of -0.8. My code doesn't work: via ODR the code returns R = 8 (initial point), not 4.5. Any idea what's wrong with my code ?

Thanks for your help

like image 356
user3699439 Avatar asked Sep 30 '22 16:09

user3699439


1 Answers

You are ignoring the data for Z that you provide. Instead, you are calculating Z to always satisfy the implicit equation that you have defined, no matter what parameters you pass it.

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)

The result of this run gives R = 4.34911251 +- 0.30341252, which seems to match your expectations.

like image 183
Robert Kern Avatar answered Oct 04 '22 20:10

Robert Kern