In Strang, section 4.2 we're doing projections. Example A (p. 213) asks:

Project the vector

`b = (3,4,4)`

onto the line through `a = (2,2,1)`

, and then onto the plane that also contains `a* = (1,0,0)`

.We're supposed to find p, the projection onto a, which is equal to x̂ (x with a little hat on it) times a. It is some fraction of a. The "error" is the part of b that is perpendicular to the projection:

The basic equations are:

This comes from

Furthermore

For the first part, with a = (2,2,1), we just have

We can check that

Part 2

Now we also consider

`a* = (1,0,0)`

to give a plane formed from the 2 vectors.We construct the matrix:

The fundamental equation is:

We could solve for

`x̂`

, then for `p = x̂ a`

, then for `P b = p`

.Instead of doing this, Strang uses the equations:

I don't want to do the arithmetic, so we'll use numpy:

We confirm that e* (e2 in the code) is perpendicular to both a and a*.

Also, for reasons that I don't understand yet,

`P`^{2} = P

.[UPDATE: The reason is simple. Suppose we do

`P b`

to get the projection of b in the plane. What happens if we do it again? Answer: nothing, we're already in the plane! So `P b`

must equal `P P b`

.