8. Fitting
Here are some exercises to do together: we put these equations into sympy to do curve fitting.
We can precede this with a discussion of fitting and how that gives us a system of linear equations.
Note that you can use sympy on your own computer, in your python environment, or you can use the “SymPy Live Shell” at https://live.sympy.org/
When you use sympy live you will find that you do not need to do a few of the startup instructions becuase they do them for you. In the examples below I will
8.1. Preliminaries: degrees of freedom
1st degree |
2nd degree |
3rd degree |
4th degree |
|---|---|---|---|
\(y = mx + b\) |
\(y = ax^2 + bx + c\) |
\(y = ax^3 + bx^2 + cx + d\) |
\begin{eqnarray}
y = & a_0 + a_1 x + a_2 x^2 \\
& + a_3 x^3 + a_4 x^4
\end{eqnarray} |
2 free parameters
\(m, b\)
|
3 free parameters
\(a, b, c\)
|
4 free parameters
\(a, b, c, d\)
|
5 free parameters
\(a_0, a_1, a_2, a_3, a_4\)
|
2 points to fit
\((x_1, y_1), (x_2, y_2)\)
|
3 points to fit
\((x_1, y_1), (x_2, y_2)\)
\((x_3, y_3)\)
|
4 points to fit
\((x_1, y_1), (x_2, y_2),\)
\((x_3, y_3), (x_4, y_4)\)
|
5 points to fit
\((x_1, y_1), (x_2, y_2),\)
\((x_3, y_3), (x_4, y_4)\)
\((x_5, y_5)\)
|
\[\begin{split}m x_1 + b = y_1 \\
m x_2 + b = y_2\end{split}\]
|
\[\begin{split}a x_1^2 + b x_1 + c = y_1 \\
a x_2^2 + b x_2 + c = y_2 \\
a x_3^2 + b x_3 + c = y_3\end{split}\]
|
\[\begin{split}a x_1^3 + b x_1^2 + c x_1\\ + d = y_1 \\
a x_2^3 + b x_2^2 + c x_2\\ + d = y_2 \\
a x_3^3 + b x_3^2 + c x_3\\ + d = y_3 \\
a x_4^3 + b x_4^2 + c x_4\\ + d = y_4 \\\end{split}\]
|
\[\begin{split}a_0 + a_1 x_1 + a_2 x_1^2\\ + a_3 x_1^3 + a_4 x_1^4 = y_1 \\
a_0 + a_1 x_2 + a_2 x_2^2\\ + a_3 x_2^3 + a_4 x_2^4 = y_2 \\
a_0 + a_1 x_3 + a_2 x_3^2\\ + a_3 x_3^3 + a_4 x_3^4 = y_3 \\
a_0 + a_1 x_4 + a_2 x_4^2\\ + a_3 x_4^3 + a_4 x_4^4 = y_4 \\
a_0 + a_1 x_5 + a_2 x_5^2\\ + a_3 x_5^3 + a_4 x_5^4 = y_5\end{split}\]
|
8.2. Preliminaries: systems of equations in sympy
When you see an equation (or system of equations) written mathematically it often looks like this:
But when we prepare math expressions for a computer program we often have to canonicalize their form. Sympy will want them to have all the terms on the left hand side (LHS) of the equation, and a zero on the right hand side. So we would rewrite this as:
and then plug it in to sympy like this:
from sympy import *
init_printing(use_unicode=True)
z, w = symbols('z w')
solve([7*z + 4*w - 1, 2*z - 5*w + 3], z, w)
# HIT ENTER HERE
We should spend some time discussing how the math equation got rendered in sympy, getting used to all the details. And we note that here we used \(z, w\) as our unknowns, and we had pure numbers for all the known parameters.
8.3. Fitting a straight line
which is equivalent to:
from sympy import *
init_printing(use_unicode=True)
m, b = symbols('m b')
x1, x2, y1, y2 = symbols('x1 x2 y1 y2')
solve([m*x1 + b - y1, m*x2 + b - y2], m, b)
# HIT ENTER HERE - there will be a long result
# now specialize it to specific points
x1 = 1
y1 = 7
x2 = 2
y2 = 3
solve([m*x1 + b - y1, m*x2 + b - y2], m, b)
# HIT ENTER HERE - this will be the result:
# result: {b: 11, m: -4}
8.4. Fitting a parabola
which is equivalent to:
from sympy import *
init_printing(use_unicode=True)
a, b, c = symbols('a b c')
x1, x2, x3, y1, y2, y3 = symbols('x1 x2 x3 y1 y2 y3')
solve([a*x1**2 + b*x1 + c - y1, a*x2**2 + b*x2 + c - y2, a*x3**2 + b*x3 + c - y3], a, b, c)
# HIT ENTER HERE - there will be a long result
# now specialize it to specific points
x1 = 1
y1 = 1
x2 = 2
y2 = 2
x3 = 3
y3 = 7
solve([a*x1**2 + b*x1 + c - y1, a*x2**2 + b*x2 + c - y2, a*x3**2 + b*x3 + c - y3], a, b, c)
result: {a: 2, b: -5, c: 4}
8.5. Fitting a cubic
which is equivalent to:
from sympy import *
init_printing(use_unicode=True)
a, b, c, d = symbols('a b c d')
x1, x2, x3, x4, y1, y2, y3, y4 = symbols('x1 x2 x3 x4 y1 y2 y3 y4')
solve([a*x1**3 + b*x1**2 + c*x1 + d - y1,
a*x2**3 + b*x2**2 + c*x2 + d - y2,
a*x3**3 + b*x3**2 + c*x3 + d - y3,
a*x4**3 + b*x4**2 + c*x4 + d - y4],
a, b, c, d)
# long result
x1 = -4
y1 = -3
x2 = -2
y2 = 0
x3 = 2
y3 = 0
x4 = 4
y4 = 2
solve([a*x1**3 + b*x1**2 + c*x1 + d - y1,
a*x2**3 + b*x2**2 + c*x2 + d - y2,
a*x3**3 + b*x3**2 + c*x3 + d - y3,
a*x4**3 + b*x4**2 + c*x4 + d - y4],
a, b, c, d)