Lab for Least Squares Lines and Polynomials

Module for Least Squares Lines

Module for Least Squares Polynomials

   Check out the new Numerical Analysis Projects page.

 

First load Mathematica's graphics package "Colors".

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr1.gif]
 
 
 

Report to be handed in.

Computer Exercises
 

 

Exercise 1. Find the two "Least Squares Lines" y = ax + b and x = cy + d for the data points
(-4.5, 0.7), (-3.2, 2.3), (-1.4, 3.8), (0.8, 5.0), (2.5, 5.5), (4.1, 6.6).

(a) Use the computer to find the least squares lines y = ax + b and x = cy + d.

(b) What point is common to both lines.

(c) Are the two lines the same ? Why?

First, enter the data points.

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr3.gif]

Use Mathematica to find the "Least Square Lines" y = ax + b.

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr4.gif]
[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr5.gif]

Plot the "Least Square Lines" y = ax + b.

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr6.gif]

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr7.gif]

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr8.gif]

Reverse the order of the coordinates of the points.

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr9.gif]
[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr10.gif]

Use Mathematica to find the "Least Square Lines" x = cy + d.

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr11.gif]
[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr12.gif]

Plot the "Least Square Lines" x = cy + d.

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr13.gif]

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr14.gif]

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr15.gif]

Compare the two "Least Squares Lines" y = ax + b and x = cy + d.

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr16.gif]

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr17.gif]

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr18.gif]
[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr19.gif]

Find the point of intersection.
Be careful, to prevent a fatal error you must use == .

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr20.gif]
[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr21.gif]

Compare this with the following average.

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr22.gif]
[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr23.gif]

What do you conjecture ! Can you prove it ?

 

Exercise 2. Derive the "normal equations" for "Least Squares Lines" y = ax + b and x = cy + d .

First, derive the "normal equations" for "Least Squares Line" y = ax + b.

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr24.gif]

The "normal equations" for "Least Squares Line" y = ax + b are

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr25.gif]

The coefficients a and b are computed wit the formulas:

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr26.gif]

Second, derive the "normal equations" for "Least Squares Line" x = cy + d.

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr27.gif]

The "normal equations" for "Least Squares Line" x = cy + d are:

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr28.gif]

The coefficients c and d are computed wit the formulas:

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr29.gif]
 

Exercise 3. Explain geometrically what is minimized to find each of the "Least Squares Lines."

[Graphics:lp.txtgr30.gif]

 

Polynomial curve fitting.

 

Exercise 4. Find the polynomial fits of degree n = 2 for the following points.
Use Mathematica to find the "Least Square Quadratic".

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr31.gif]
[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr32.gif]
[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr33.gif]

Plot the "Least Square Quadratic".

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr34.gif]

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr35.gif]

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr36.gif]
 
 

Exercise 5. Find the polynomial fits of degree n = 3 for the data points in the previous example.
Use Mathematica to find the "Least Square Cubic".

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr37.gif]
[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr38.gif]

Plot the "Least Square Cubic".

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr39.gif]

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr40.gif]

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr41.gif]
 
 

Caution for polynomial curve fitting.
Something goes wrong if the data is radically "NOT polynomial."
This phenomenon is called "polynomial wiggle."

 

Exercise 6. Find the least squares polynomial fits of degree n = 2, 3, 4, 5 for the points
(0.25, 23.1), (1.0 , 1.68), (1.5 , 1.0), (2.0 , 0.84), (2.4 , 0.826), (5.0 , 1.2576)
REMARK. The points were obtained from [Graphics:lp.txtgr42.gif].

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr43.gif]
[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr44.gif]
[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr45.gif]

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr46.gif]

Use Mathematica to find the least squares polynomial of degree n = 2.

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr47.gif]

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr48.gif]

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr49.gif]

Use Mathematica to find the least squares polynomial of degree n = 3.

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr50.gif]

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr51.gif]

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr52.gif]

Use Mathematica to find the least squares polynomial of degree n = 4.

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr53.gif]

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr54.gif]

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr55.gif]

Use Mathematica to find the least squares polynomial of degree n = 5.

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr56.gif]

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr57.gif]

[Graphics:lp.txtgr2.gif][Graphics:lp.txtgr58.gif]
 

What do you see happening with the polynomials of degree n = 2, 3, 4, 5 ?

 

 

 

(c) John H. Mathews, 1998