More circle fitting options with Scipy

A comment on the first post on this topic found that the method used did not work well for short arcs of large radius circles.  An example of the problem can be seen in the screen shots below:

The function has returned an arc with a radius of only 85 m (rather than 6000), and the plots below show that the generated arc is a very poor fit to the data:

The comment pointed to the following page at the SciPy CookBook:

Least squares circle

which provides Python code for 5 alternative fitting methods:

  1. Solve linear system with linalg.solve
  2. Basic usage of optimize.leastsq
  3. Advanced usage of optimize.leastsq, with jacobian
  4. Basic usage of orthogonal distance regression (ODR) with an implicit function definition
  5. Advanced usage of ODR, with jacobian

To accommodate these alternatives in the Fit_Circ3D function I have added an optional “method” argument to the function.  Method 0 calls the original routine.  Methods 1-4 call the SciPy methods listed above (default method = 3 if the argument is omitted).  I have not been able to get method 5 to work properly from my code, so at the moment this option is not available.

The results with method 3 of the new code are shown below, showing a good fit to the scattered data, even with a short large radius arc  (click on the image for full-size view):

The code for the new methods (adapted from the original so that it can be called from Excel, via xlwings) can be found in least_squares_circle2.py.  This file, the spreadsheet IP2_py.xlsb, and the associated Python file IP2_py.py are all available for free download from:

IP2_py.zip

In addition to Excel, the following (free) software is required by the spreadsheet:

  • Python
  • Numpy and Scipy
  • xlwings

All are included in the default Anaconda Python installation.

 

This entry was posted in Coordinate Geometry, Curve fitting, Excel, Link to Python, Maths, Newton, NumPy and SciPy, UDFs, VBA, xlwings and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.