Curveffiting¶
Class to get the best fit of a curve to a set of (x, y) points.

class
pymeeus.CurveFitting.
CurveFitting
(*args)[source]¶ Class CurveFitting deals with finding the function (linear, cuadratic, etc) that best fit a given set of points.
The constructor takes pairs of (x, y) values from the table of interest. These pairs of values can be given as a sequence of int/floats, tuples, lists or Angles. It is also possible to provide a CurveFitting object to the constructor in order to get a copy.
Note
When using Angles, be careful with the 360to0 discontinuity.
If a sequence of int, floats or Angles is given, the values in the odd positions are considered to belong to the ‘x’ set, while the values in the even positions belong to the ‘y’ set. If only one tuple or list is provided, it is assumed that it is the ‘y’ set, and the ‘x’ set is build from 0 onwards with steps of length 1.
Please keep in mind that a minimum of two data pairs are needed in order to carry out any fitting. If only one value pair is provided, a ValueError exception will be raised.

__init__
(*args)[source]¶ CurveFitting constructor.
This takes pairs of (x, y) values from the table of interest. These pairs of values can be given as a sequence of int/floats, tuples, lists or Angles. It is also possible to provide a CurveFitting object to the constructor in order to get a copy.
Note
When using Angles, be careful with the 360to0 discontinuity
If a sequence of int, floats or Angles is given, the values in the odd positions are considered to belong to the ‘x’ set, while the values in the even positions belong to the ‘y’ set. If only one tuple or list is provided, it is assumed that it is the ‘y’ set, and the ‘x’ set is build from 0 onwards with steps of length 1.
Please keep in mind that a minimum of two data pairs are needed in order to carry out any interpolation. If only one value pair is provided, a ValueError exception will be raised.
Parameters: args (int, float, list, tuple, Angle
,CurveFitting
) – Input tabular values, or another CurveFitting object.Returns: CurveFitting object. Return type: CurveFitting
Raises: ValueError if not enough input data pairs are provided. Raises: TypeError if input values are of wrong type. >>> i = CurveFitting([5, 3, 6, 1, 2, 4, 9], [10, 6, 12, 2, 4, 8]) >>> print(i._x) [5, 3, 6, 1, 2, 4] >>> print(i._y) [10, 6, 12, 2, 4, 8] >>> j = CurveFitting([3, 8, 1, 12, 2, 5, 8]) >>> print(j._x) [0, 1, 2, 3, 4, 5, 6] >>> print(j._y) [3, 8, 1, 12, 2, 5, 8] >>> k = CurveFitting(3, 8, 1, 12, 2, 5, 8) >>> print(k._x) [3, 1, 2] >>> print(k._y) [8, 12, 5] >>> m = CurveFitting(k) >>> print(m._x) [3, 1, 2] >>> print(m._y) [8, 12, 5]

__len__
()[source]¶ This method returns the number of value pairs internally stored in this object.
Returns: Number of value pairs internally stored Return type: int >>> i = CurveFitting([5, 3, 6, 1, 2, 4, 9], [10, 6, 12, 2, 4, 8]) >>> len(i) 6

__repr__
()[source]¶ Method providing the ‘official’ string representation of the object. It provides a valid expression that could be used to recreate the object.
Returns: As string with a valid expression to recreate the object Return type: string >>> i = CurveFitting([5, 3, 6, 1, 2, 4, 9], [10, 6, 12, 2, 4, 8]) >>> repr(i) 'CurveFitting([5, 3, 6, 1, 2, 4], [10, 6, 12, 2, 4, 8])'

__str__
()[source]¶ Method used when trying to print the object.
Returns: Internal tabular values as strings. Return type: string >>> i = CurveFitting([5, 3, 6, 1, 2, 4, 9], [10, 6, 12, 2, 4, 8]) >>> print(i) X: [5, 3, 6, 1, 2, 4] Y: [10, 6, 12, 2, 4, 8]

__weakref__
¶ list of weak references to the object (if defined)

correlation_coeff
()[source]¶ This method returns the coefficient of correlation, as a float.
Returns: Coefficient of correlation. Return type: float >>> cf = CurveFitting([73.0, 38.0, 35.0, 42.0, 78.0, 68.0, 74.0, 42.0, ... 52.0, 54.0, 39.0, 61.0, 42.0, 49.0, 50.0, 62.0, ... 44.0, 39.0, 43.0, 54.0, 44.0, 37.0], ... [90.4, 125.3, 161.8, 143.4, 52.5, 50.8, 71.5, ... 152.8, 131.3, 98.5, 144.8, 78.1, 89.5, 63.9, ... 112.1, 82.0, 119.8, 161.2, 208.4, 111.6, 167.1, ... 162.1]) >>> r = cf.correlation_coeff() >>> print(round(r, 3)) 0.767

general_fitting
(f0, f1=<function <lambda>>, f2=<function <lambda>>)[source]¶ This method returns a tuple with the ‘a’, ‘b’, ‘c’ coefficients of the general equation ‘y = a*f0(x) + b*f1(x) + c*f2(x)’ that best fits the table data, using the least squares approach.
Parameters: f1, f2 (f0,) – Functions used to build the general equation. Returns: ‘a’, ‘b’, ‘c’ coefficients of best general equation fit. Return type: tuple Raises: ZeroDivisionError if input functions are null or input data leads to a division by zero >>> cf4 = CurveFitting([3, 20, 34, 50, 75, 88, 111, 129, 143, 160, 183, ... 200, 218, 230, 248, 269, 290, 303, 320, 344], ... [0.0433, 0.2532, 0.3386, 0.3560, 0.4983, 0.7577, ... 1.4585, 1.8628, 1.8264, 1.2431, 0.2043, ... 1.2431, 1.8422, 1.8726, 1.4889, 0.8372, ... 0.4377, 0.3640, 0.3508, 0.2126]) >>> def sin1(x): return sin(radians(x)) >>> def sin2(x): return sin(radians(2.0*x)) >>> def sin3(x): return sin(radians(3.0*x)) >>> a, b, c = cf4.general_fitting(sin1, sin2, sin3) >>> print("a = {}; b = {}; c = {}".format(round(a, 2), round(b, 2), ... round(c, 2))) a = 1.2; b = 0.77; c = 0.39
>>> cf5 = CurveFitting([0, 1.2, 1.4, 1.7, 2.1, 2.2]) >>> a, b, c = cf5.general_fitting(sqrt) >>> print("a = {}; b = {}; c = {}".format(round(a, 3), round(b, 3), ... round(c, 3))) a = 1.016; b = 0.0; c = 0.0

linear_fitting
()[source]¶ This method returns a tuple with the ‘a’, ‘b’ coefficients of the linear equation ‘y = a*x + b’ that best fits the table data, using the least squares approach.
Returns: ‘a’, ‘b’ coefficients of best linear equation fit. Return type: tuple Raises: ZeroDivisionError if input data leads to a division by zero >>> cf = CurveFitting([73.0, 38.0, 35.0, 42.0, 78.0, 68.0, 74.0, 42.0, ... 52.0, 54.0, 39.0, 61.0, 42.0, 49.0, 50.0, 62.0, ... 44.0, 39.0, 43.0, 54.0, 44.0, 37.0], ... [90.4, 125.3, 161.8, 143.4, 52.5, 50.8, 71.5, ... 152.8, 131.3, 98.5, 144.8, 78.1, 89.5, 63.9, ... 112.1, 82.0, 119.8, 161.2, 208.4, 111.6, 167.1, ... 162.1]) >>> a, b = cf.linear_fitting() >>> print("a = {} b = {}".format(round(a, 2), round(b, 2))) a = 2.49 b = 244.18

quadratic_fitting
()[source]¶ This method returns a tuple with the ‘a’, ‘b’, ‘c’ coefficients of the quadratic equation ‘y = a*x*x + b*x + c’ that best fits the table data, using the least squares approach.
Returns: ‘a’, ‘b’, ‘c’ coefficients of best quadratic equation fit. Return type: tuple Raises: ZeroDivisionError if input data leads to a division by zero >>> cf2 = CurveFitting([2.0, 1.5, 1.0, 0.5, 0.0, 0.5, 1.0, 1.5, ... 2.0, 2.5,3.0], ... [9.372, 3.821, 0.291, 3.730, 5.822, 8.324, ... 9.083, 6.957, 7.006, 0.365, 1.722]) >>> a, b, c = cf2.quadratic_fitting() >>> print("a = {}; b = {}; c = {}".format(round(a, 2), round(b, 2), ... round(c, 2))) a = 2.22; b = 3.76; c = 6.64

set
(*args)[source]¶ Method used to define the value pairs of CurveFitting object.
This takes pairs of (x, y) values from the table of interest. These pairs of values can be given as a sequence of int/floats, tuples, lists, or Angles. It is also possible to provide a CurveFitting object to this method in order to get a copy.
Note
When using Angles, be careful with the 360to0 discontinuity
If a sequence of int, floats or Angles is given, the values in the odd positions are considered to belong to the ‘x’ set, while the values in the even positions belong to the ‘y’ set. If only one tuple or list is provided, it is assumed that it is the ‘y’ set, and the ‘x’ set is build from 0 onwards with steps of length 1.
Please keep in mind that a minimum of two data pairs are needed in order to carry out any interpolation. If only one value is provided, a ValueError exception will be raised.
Parameters: args (int, float, list, tuple, Angle
) – Input tabular values, or another CurveFitting object.Returns: None. Return type: None Raises: ValueError if not enough input data pairs are provided. Raises: TypeError if input values are of wrong type. >>> i = CurveFitting() >>> i.set([5, 3, 6, 1, 2, 4, 9], [10, 6, 12, 2, 4, 8]) >>> print(i._x) [5, 3, 6, 1, 2, 4] >>> print(i._y) [10, 6, 12, 2, 4, 8] >>> j = CurveFitting() >>> j.set([3, 8, 1, 12, 2, 5, 8]) >>> print(j._x) [0, 1, 2, 3, 4, 5, 6] >>> print(j._y) [3, 8, 1, 12, 2, 5, 8] >>> k = CurveFitting(3, 8, 1, 12, 2, 5, 8) >>> print(k._x) [3, 1, 2] >>> print(k._y) [8, 12, 5]
