Link to this document's Jupyter Notebook

In order to successfully complete this assignment you need to participate both individually and in groups during class. If you attend class in-person then have one of the instructors check your notebook and sign you out before leaving class. If you are attending asynchronously, turn in your assignment using D2L no later than 11:59pm on the day of class. See links at the end of this document for access to the class timeline for your section.


In-Class Assignment: Least Squares Fit (LSF)

Todays in-class assignment includes multiple Least Squares Fit models. The goal is to see the types of models that can be solved using least squares fit. Even though this is a Linear Algebra Method the models do not need to be linear.

As soon as you get to class, download and start working on this notebook: the instructor will go over solutions but make sure you try to understand and solve them on your own.

Agenda for today's class (80 minutes)

  1. LSF Example: Tracking the Planets
  2. LSF Example: Predator Pray Model
  3. LSF Example: Estimating the best Ellipses

1. LSF Example: Tracking the Planets

composit image of the planets (and sun) in our solar system

The following table lists the average distance from the sun to each of the first seven planets, using Earth's distance as a unit of measure (AUs).

Mercury Venus Earth Mars Jupiter Satern Uranus
0.39 0.72 1.00 1.52 5.20 9.54 19.2

The following is a plot of the data:

Note that the above plot does not look like a line, and so finding the line of best fit is not fruitful. It does, however look like an exponential curve (maybe a polynomial?). The following step transforms the distances using the numpy log function and generates a plot that looks much more linear.

For this question we are going to find the coefficients ($c$) for the best fit line of the form $c_1 + c_2i= \log{d}$, where $i$ is the index of the planet and $d$ is the distance.

The following code constructs this problem in the form $Ax=b$ and define the $A$ matrix and the $b$ matrix as numpy matrices

DO THIS: Solve for the best fit of $Ax=b$ and define a new variable $c$ which consists of the of the two coefficients used to define the line $(\log{d} = c_1 + c_2i)$

DO THIS: Modify the following code (as needed) to plot your best estimates of $c_1$ and $c_2$ against the provided data.

We can determine the quality of this line fit by calculating the root mean squared error between the estimate and the actual data:

Finally, we can also make the plot on the original axis using the inverse of the log (i.e. the exp function):

The asteroid belt between Mars and Jupiter is what is left of a planet that broke apart. Let's the above calculation again but renumber so that the index of Jupyter is 6, Saturn is 7 and Uranus is 8 as follows:

DO THIS: Repeat the calculations from above with the updated model. Plot the results and compare the RMSE.

This model of planet location was used to help discover Neptune and prompted people to look for the "missing planet" in position 5 which resulted in the discovery of the asteroid belt. Based on the above model, what is the estimated distance of the asteroid belt and Neptune (index 9) from the sun in AUs? (Hint: you can check your answer by searching for the answer on-line).


2. LSF Example: Predator Pray Model

Cartoon image of a cat chasing a wind up mouse

The following example example data comes from https://mathematica.stackexchange.com/questions/34761/find-parameters-of-odes-to-fit-solution-data and represents some experimental data time, x and y.

$$dx = ax + bxy$$$$dy = cy + dxy$$

The following code plots the data

DO THIS Use Numerical Differentiation to calculate $dx$ and $dy$ from $x$ and $y$. See if you can plot $x,dx$ nad $y,dy$ on a couple of plots. Use the plots to try and check to make sure your results make senes.

DO THIS Formulate two linear systems ($Ax=b$) and solve them using LSF as we did in the pre-class. Use one to solve the first ODE and the second to solve the second ODE. Remember, we are trying to estimate values for $a,b,c,d$

Assuming everything worked the following should plot the result.


3. LSF Example: Estimating the best Ellipses

Animated image of planets revolving around the sun

Now consider the following sets of points. Think of these as observations of planet moving in an elliptical orbit.

In this problem we want to try to fit an ellipse to the above data. First lets look at a general equation for an ellipse:

$$ \frac{(u+x)^2}{a^2} + \frac{(v+y)^2}{b^2} = 1 \qquad \qquad \text{ (1)}$$

Where $u$ and $v$ are the $x$ and $y$ coordinates for the center of the ellipse and $a$ and $b$ are the lengths of the axes sizes of the ellipse. A quick search on how to plot an ellipse in python comes up with the following example:

Notice this example uses equations of the form:

$$t = [0, \dots, 2\pi]$$$$x = u+a\cos(t)$$$$y = v+b\sin(t)$$

Turns out that this form of the equation is easier to plot and the variables $u,v,a,b$ are the same as our original equation.

Now lets expand the original equation (equation 1 from above) and we get the following:

$$x^2−2ux-u^2+y^2−2vy+v^2=r^2 \qquad \qquad \text{ (2)}$$

QUESTION: Why can't we convert equation 2 into the form $Ax=b$ and solve using Least Squares Fit? Discuss with your group and be prepared to share your thought with the class.

Put your answer to the above question here.

If we look at our data more closely we can simplify equations 1 and 2 by assuming the the centroid (u,v) is at the origin. This assumption results in the following equation:

$$ \frac{x^2}{a^2} + \frac{y^2}{b^2} = 1 $$

Notice we can rewrite this into a standard linear set of equations by defining $c_o = \frac{1}{a^2}$ and $c_1 = \frac{1}{b^2}$ and rewriting the equation as follows:

$$ c_0x^2 + c_1y^2 = 1 $$

DO THIS Given that we know the $x$ and $y$ values of our point observations, equation 4 is now linear and can be solved using Least Squares Fit. Using the observation points from above construct A and b as numpy matrixes for the overdefined system $Ax=b$

Put your answer to the above question here.

DO THIS: Solve the above over defined system of linear equations for $c_0$ and $c_1$ using LSF.

Assuming we have $c$ in the correct format, we can now calculate $a$ and $b$ from the solution for $c_o$ and $c_1$ calculated in the previous step and plot using our plotting code:


Congratulations, we're done!

If you attend class in-person then have one of the instructors check your notebook and sign you out before leaving class. If you are attending asynchronously, turn in your assignment using D2L.

Course Resources:

Written by Dr. Dirk Colbry, Michigan State University Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.