All pastes #908221 Raw Edit

curve fitting theory

public text v1 · immutable
#908221 ·published 2008-02-18 06:19 UTC
rendered paste body
from Numeric import *
from numarray.linear_algebra import *

#playing with Numpy and linear_algebra.py
#d=array(((0,0),(.5,0),(0,.5)))
#print d
#e=array(d[1:4])
#print e
#e1=e[0,0]
#e2=e[0,1]
#e3=e[1,0]
#e4=e[1,1]
#g=array(((e1,e2),(e3,e4)))
#print g
#h= determinant(g)
#note: numarray.linear_algebra is needed to get a determinant 
#print h
#from http://home.att.net/~srschmitt/script_circle_solver.html

#Circle solver
#From analytic geometry, we know that there is a unique circle that passes through three points if, and only #if, they are not on the same line. Given three points,
# {x1, y1}, {x2, y2}, {x3, y3}

#points=array(((0,0),(.5,0),(0,.5)))
x1=0;y1=.5;x2=0;y2=0;x3=.5;y3=0

#

#  ^2 = squared
#|x^2 + y^2 	x 	y 	1|
#|x1^2 + y1^2	x1	y1	1| = 0
#|x2^2 + y2^2	x2	y2	1|
#|x3^2 + y3^2	x3	y3	1|
# - remember, we are solving for x and y - they are in the top row
# Evaluating the cofactors for the first row of the determinant can give us a solution. The determinant #equation can be written as an equation of these cofactors:
#
#    (x^2 + y^2)·M11 - x·M12 + y·M13 - M14 = 0

# here is a side note on how to determine Minor determinants:  #####################
#from: http://en.wikipedia.org/wiki/Minor_%28linear_algebra%29
#In linear algebra, a minor of a matrix A is the determinant of some smaller square matrix, cut down from A.
# For example, given the matrix
# | 1  4  7 |
# | 3  0  5 |
# |-1  9  11|
# The minor M23 is the determinant of the above matrix with row 2 and column 3 removed
# | 1  4    |          | 1  4 |
# |         |  yields  |-1  9 |  = (9-(-4)) = 13
# |-1  9    |
#
####################################################################################
# so M11 for the example earlier is the determinant of this matrix:
# | x1  y1  1|
# | x2  y2  1|
# | x3  y3  1|
# for points=array(((0,0),(.5,0),(0,.5)))
# let's use some variables so that we don't have to type everything, every time:
#  points= array(((x1,y1),(x2,y2),(x3,y3)))
M11_array = array(((x1,y1,1),(x2,y2,1),(x3,y3,1)))
# to get the determinant:
M11 = determinant(M11_array)
#
#    M12 is the determinant of this matrix:
# |x1^2 + y1^2	y1	1| 
# |x2^2 + y2^2	y2	1|
# |x3^2 + y3^2	y3	1|
M12_array=array((((x1*x1)+(y1*y1),y1,1),((x2*x2)+(y2*y2),y2,1),((x3*x3)+(y3*y3),y3,1)))
M12 = determinant(M12_array)
#
#    M13 is the determinant of this matrix:
# |x1^2 + y1^2	x1	1| 
# |x2^2 + y2^2	x2	1|
# |x3^2 + y3^2	x3	1|
M13_array=array((((x1*x1)+(y1*y1),x1,1),((x2*x2)+(y2*y2),x2,1),((x3*x3)+(y3*y3),x3,1)))
M13 = determinant(M13_array)
#   M14 is the determinant of this matrix:
# |x1^2 + y1^2	x1	y1| 
# |x2^2 + y2^2	x2	y2|
# |x3^2 + y3^2	x3  y3|
M14_array=array((((x1*x1)+(y1*y1),x1,y1),((x2*x2)+(y2*y2),x2,y2),((x3*x3)+(y3*y3),x3,y3)))
M14 = determinant(M14_array)
#
#
#This can be converted to the canonical form of the equation of a circle:
#
#    x^2 + y^2 - (M12/M11)·x + (M13/M11)·y - M14/M11 = 0
#
#Completing the squares in x and y gives:
#
#    x0 =  0.5·M12/M11 
#    y0 = -0.5·M13/M11 
#    r0^2 = x0^2 + y0^2 + M14/M11 
#
#Note that there is no solution when M11 is equal to zero. In this case, the points are not on a circle. 
# so for python:
x_center = .5 * (M12/M11 )
y_center = -.5 * (M13/M11 )
radius  = ((	x_center **2) + (y_center **2) + (M14/M11))**.5