Mathematica Lab 10

MATH 220E Spring 2025

The Gram-Schmidt Process is a very important algorithm (i.e., procedure) that converts any basis for a subspace S into an orthogonal basis of S.

Why is an orthogonal basis so important to be able to find?

Recall from class that, in order to use the formula to compute the projection of a vector u onto a subset S, we must have an orthogonal basis for S. If not, the formula is useless.

So then, how do get an orthogonal basis for S? There are two steps.

Step 1: Simply get any basis for S. Do not worry whether or not the basis is orthogonal.

Step 2: Use the Gram-Schmidt Process, explained below, to turn your basis into an orthogonal basis.

The Gram-Schmidt Process, step-by-step

We'll begin by following Example 5 in Section 8.2 of the Holt textbook. Subspace S is given as the span of three vectors, and these three vectors form a basis for S. First we enter these basis vectors and put them together in a set. (Make sure to run the following code cell.)

s1={1,0,1,1}; s2={0,2,0,3}; s3={-3,-1,1,5};
set={s1,s2,s3}

If we run MatrixForm[set], Mathematica would print our vectors as rows of a matrix, which isn’t what we want. We would rather print each vector vertically, as in the textbook. We can achieve this by using the /@ operator (shorthand for the Map function) to apply MatrixForm to each of the vectors individually like this:

MatrixForm /@set

Remember, the goal is to create a basis in which all of the vectors are orthogonal to each other. The vectors in our original basis are s1, s2, and s3. The vectors in our new orthogonal basis, will v1, v2, and v3.

The first vector in the new orthogonal basis will be the same as the first vector in the original basis.  So, we define v1 to be the same as the original s1.

v1=s1

The second vector in the new basis, v2, will be the portion of s2 that is orthogonal to v1. Basically, we want to take vector s2 and “get rid of the portion” that is in the same direction as v1. In other words, we want to take s2 and subtract its projection onto v1.

v2=s2-(Subscript[proj, v1])s2

We can use Mathematica to compute the projection vector for us, like this:

v2=s2-Projection[s2,v1]

Now we have two vectors (v1 and v2) that are orthogonal to each other. We move on to the third vector, s3.

The third vector in the new basis, v3, will be the portion of s3 that is perpendicular to both v1 and v2. That is, we want to take s3 and subtract its projection onto the subspace spanned by v1 and v2. Since v1 and v2 are orthogonal, we can simply take s3 and subtract its projections onto v1 and v2. The following code does this:

v3=s3-Projection[s3,v1]-Projection[s3,v2]

Now we have three vectors (v1, v2, and v3) that are orthogonal to each other. If our subspace had more than three dimensions, we would keep doing this process.

The collection {v1,v2,v3} is the Gram-Schmidt orthogonal basis for S, and matches the result in the book.

GSset={v1,v2,v3}

Print these vectors nicely as a set of column vectors:

MatrixForm/@GSset

Gram-Schmidt Process, automated

The following code cell contains a module that implements the Gram-Schmidt process. Run the following code cell to store this module in memory, then test it out on our set of vectors {s1, s2, s3}.

gramSchmidt[vecSet_]:=Module[
(* local variables *)
{basisSet={vecSet[[1]]} (* set where we will store our orthogonal basis *),
newVec (* temporary vector *),i},

(* loop over our initial set, starting with the second vector *)
For[i=2,i<=Length[vecSet],i++,
newVec=vecSet[[i]];
(* subtract the projections of newVec onto the basis elements we've found already *) 
For[j=1,j<i,j++,
newVec-=Projection[vecSet[[i]],basisSet[[j]]]
];
(* store our new vector into our basis set *) 
AppendTo[basisSet,newVec]
];

(* return the orthogonal basis *) 
basisSet
]

Use the gramSchmidt function to convert our set of vectors {s1, s2, s3} to an orthogonal basis:

gsBasis=gramSchmidt[set]
MatrixForm/@gsBasis

Orthonormal Basis

An orthonormal basis is an orthogonal basis such that each basis vector has length equal to 1.

Mathematica has a built-in function, Orthogonalize, that first produces an orthogonal basis (via the Gram-Schmidt process) and then normalizes each vector to make it have length 1. Let’s try out this function:

Orthogonalize[set]

Print the resulting vectors nicely:

MatrixForm/@Orthogonalize[set]

Notice that the columns of the matrix above are exactly the normalized vectors from our Gram-Schmidt set, as we can see here:

Normalize[v1]

Normalize[v2]

Normalize[v3]

Exercises for you to do

In an new Mathematica notebook, do the following:

Use the gramSchmidt function above to do Section 8.2 Exercises 7, 11, and 13.

Use the Orthogonalize function to do Section 8.2 Exercises 23, 27, and 55.

Make sure your solutions are clearly labeled in your Mathematica notebook. Then submit your Mathematica notebook to the Mathematica Lab 10 assignment on Moodle.