Talk:PlanetPhysics/Example of Linear Least Squares

Original TeX Content from PlanetPhysics Archive
%%% This file is part of PlanetPhysics snapshot of 2011-09-01 %%% Primary Title: example of linear least squares %%% Primary Category Code: 06.20.Dk %%% Filename: ExampleOfLinearLeastSquares.tex %%% Version: 6 %%% Owner: bloftin %%% Author(s): bloftin %%% PlanetPhysics is released under the GNU Free Documentation License. %%% You should have received a file called fdl.txt along with this file. %%% If not, please write to gnu@gnu.org. \documentclass[12pt]{article} \pagestyle{empty} \setlength{\paperwidth}{8.5in} \setlength{\paperheight}{11in}

\setlength{\topmargin}{0.00in} \setlength{\headsep}{0.00in} \setlength{\headheight}{0.00in} \setlength{\evensidemargin}{0.00in} \setlength{\oddsidemargin}{0.00in} \setlength{\textwidth}{6.5in} \setlength{\textheight}{9.00in} \setlength{\voffset}{0.00in} \setlength{\hoffset}{0.00in} \setlength{\marginparwidth}{0.00in} \setlength{\marginparsep}{0.00in} \setlength{\parindent}{0.00in} \setlength{\parskip}{0.15in}

\usepackage{html}

% this is the default PlanetMath preamble. as your knowledge % of TeX increases, you will probably want to edit this, but % it should be fine as is for beginners.

% almost certainly you want these \usepackage{amssymb} \usepackage{amsmath} \usepackage{amsfonts}

% used for TeXing text within eps files %\usepackage{psfrag} % need this for including graphics (\includegraphics) \usepackage{graphicx} % for neatly defining theorems and propositions %\usepackage{amsthm} % making logically defined graphics %\usepackage{xypic}

% there are many more packages, add them here as you need them

% define commands here

\begin{document}

The assumption of linear least \htmladdnormallink{squares}{http://planetphysics.us/encyclopedia/PiecewiseLinear.html} is that there is a linear relationship between our measurements $z$ and the variables to be estimated $x$

\begin{equation} z = Mx + b \end{equation}

For this example let us assume that our measurements are given in Table 1 and you can see them plotted in Figure 1.

\begin{center} \begin{tabular}{ccccccccccc} \hline \\ x & -3.0 & -2.5 & -2.0 & -1.5 & -1.0 & -0.5 & 0.0 & 0.5 & 1.0 & 1.5 \\ z & -1.0 & -0.25 & 0.0 & 0.25 & 0.4 & 0.7 & 1.0 & 1.1 & 1.4 & 1.8 \\ [2ex] \hline \end{tabular} \end{center} \begin{center} {\bf Table 1:} Example Data \end{center}

The linear least squares solution to fit the given data is given by the equation

\begin{equation} x_{fit} = (A^TA)^{-1}A^Tz \end{equation}

The only not so obvious step before using a tool like Matlab, is to form the $A$ \htmladdnormallink{matrix}{http://planetphysics.us/encyclopedia/Matrix.html}, which is a combination of an \htmladdnormallink{identity}{http://planetphysics.us/encyclopedia/Cod.html} \htmladdnormallink{vector}{http://planetphysics.us/encyclopedia/Vectors.html} and $x$ as column vectors, such that

$$ A = [ \,\, 1 \,\, | \,\,x \,\,] $$

This is clarified by looking at the example code in Matlab, \htmladdnormallink{LinearLeastSquares.m}{http://aux.planetphysics.us/files/objects/245/LinearLeastSquares.m}. A plot of fitting the measurement data with a line such that it minimizes the the mean square of the error is shown in Figure 1.

The equation of the line to fit this data is then

$$z = 0.543 x + 0.947$$

\begin{center} \includegraphics[scale=.6]{LinearLeastSquares2.eps}

{\bf Figure 1:} Linear Fit of Example Data (Matlab) \end{center}

\begin{center} \includegraphics[scale=.8]{least_squares.eps}

{\bf Figure 1:} Linear Fit of Example Data (rlplot) \end{center}

\end{document}