Octave for Chapter 9

Examples and Templates in this section provide sample Octave code for least squares, and -factorization. You can access our code through the link at the bottom of each template. Feel free to modify the code and experiment to learn more!

You can write your own code using Octave software or online Octave cells. To access Octave cells online, go to the Sage Math Cell Webpage, select OCTAVE as the language, enter your code, and press EVALUATE.

To ”save” or share your online code, click on the Share button, select Permalink, then copy the address directly from the browser window. You can store this link to access your work later or share this link with others. You will need to get a new Permalink every time you modify the code.

Octave Tutorial

Least Squares

The following theorem establishes one way to implement least squares.

We can implement this process as follows.

To do least squares in Octave, we use the backslash operator (). Depending on the type of matrix, backslash uses different techniques to compute the least squares solution more efficiently. For more information about inv and the backslash operator () see Reference.

The following two examples illustrate the two implementations of least squares side-by-side.

The backslash operator () can be used to solve systems of equations when a solution exists. Here is an example.

-factorization

Recall the definition of -factorization.

Recall the following algorithm for using -factorization to approximate eigenvalues from -Factorization.

Compare the following example to Example QR-algortihm-2x2-025425.

Octave Exercises

Attempt to solve each of the following systems of equations using the backslash () operator, and the rref function. Interpret your results.
(a)
\begin{equation} \begin{array}{ccccccccc} 3x &- &y&+&4z&= &2 \\ &&y&+&z&=&1\\ -2x&&&+&3z&=&1 \end{array} \end{equation}

What is true about this system?

The system is consistent. Using the backslash operator produces the same result as using the rref function. The system is inconsistent. The backslash operator gives the least squares approximation. The system is consistent but has infinitely many solutions. The backslash operator gives one particular solution.
(b)
\begin{equation} \begin{array}{ccccccccc} 3x &- &y&+&4z&= &2 \\ x&-&y&+&2z&=&4\\ -2x&+&y&-&3z&=&-3 \end{array} \end{equation}

What is true about this system?

The system is consistent. Using the backslash operator produces the same result as using the rref function. The system is inconsistent. The backslash operator gives the least squares approximation. The system is consistent but has infinitely many solutions. The backslash operator gives one particular solution.
(c)
\begin{equation} \begin{array}{ccccccccc} 3x &- &y&+&4z&= &2 \\ x&-&y&+&2z&=&1\\ -2x&+&y&-&3z&=&1 \end{array} \end{equation}

What is true about this system?

The system is consistent. Using the backslash operator produces the same result as using the rref function. The system is inconsistent. The backslash operator gives the least squares approximation. The system is consistent but has infinitely many solutions. The backslash operator gives one particular solution.
Use least squares to find a line of best fit for the points shown in the GeoGebra interactive below. Enter the equation of the line into the GeoGebra interactive to view your result.

(a)
Find the residual, , for your line of best fit.
(b)
The original line, initially shown in the interactive, visually appeared to be a pretty good fit. Find the residual for the line and compare it to the residual you got for your line. Is your line a better fit?
Explain how to interpret the product to determine whether matrix has orthonormal columns. Prove your claims.
Modify the code in Example ex:qrEig to approximate the eigenvalues of . Compare your answers to the answers you got for Problem prob:3x3fromKuttler1.
Modify the code in Example ex:qrEig for the matrix . What are you observing? Are we getting close to finding the eigenvalues of ? Explain what is happening and why.
2024-10-01 02:16:33