MATH 349-080
Elementary Linear Algebra
Prof. D. A. Edwards
Copyright 2024

Dot Product, Linear Systems (10/1 Version)

We begin with our standard commands:

Quit[]

$PrePrint=If[MatrixQ[#] || VectorQ[#],MatrixForm[#],#]&;

Section 1.2: The Dot Product

We define the same vectors as in the previous notebook:

xvec={-2,-1,-3}
yvec = {4,3,2}
zvec={2,5}

To take the dot product of two vectors, just insert a period (.) between them.  Again, if the dimensions don’t match, then Mathematica will produce an error.

xvec.yvec
xvec.zvec

Note that we use a period, NOT an asterisk (*) like we do for scalar multiplication.

There are two ways to calculate the length of a vector.  First, recall that the dot product of any vector with itself is its length squared:

xvec.xvec
Sqrt[%]

Alternatively, we may use the command Norm:

Norm[xvec]

Similarly, there are two ways to normalize a vector.  First, we may divide by its length:

xvec/Norm[xvec]

Recall that we may use the % operator to refer to the line before.

Alternatively, we may use the command Normalize:

Normalize[xvec]

To calculate the angle between two vectors, we use the VectorAngle command, which calculates the angle between two vectors directly.  The syntax is VectorAngle[vector1,vector2].

theta = VectorAngle[xvec,yvec]

This isn’t very helpful without an explicit number.  Therefore, we use the N command, which always returns a floating-point number for its argument:

N[theta]

This answer is in radians.

To get the vector projection, Mathematica has a command Projection.  So to get the vector projection of v onto w, (Subscript[proj, w])v, the syntax is Projection[v,w].

vproj=Projection[xvec,yvec]

Note that this result is a multiple of yvec, as expected.

However, for the scalar projection, you MUST use the formula in class.  For instance, here is the scalar projection of x onto y:

(xvec.yvec)/Norm[yvec]

This is because the scalar projection is SIGNED length, so just taking the length of the vector projection won’t work:

Norm[vproj]

Section 2.1: Solving Linear Systems

There are two basic ways to solve systems of linear equations in Mathematica.  Today, we just learn the first.  We can input the equations one by one:

eq1=2*x+3*y-2*z==0
eq2=3*x-5*y+z==4
eq3=-x+3*y+2*z==1

Once we have entered the equations, we may use the Solve command as described before.  The difference is that we are solving a set of equations for a set of variables, so both the equations and the variables must be enclosed in braces:

Solve[{eq1,eq2,eq3},{x,y,z}]

Note that the variables may not be returned to you in the same order that you put them in, so you have to be careful to get the order correct when using your answer.

Text Formatting

In order to make your Mathematica notebooks more appealing, it may be necessary to format the text.  The most common formatting options may be accessed from the menu bar, or the Writing Assistant, which can be viewed by selecting Palettes...Writing Assistant.  There you will find a B button (for bold), an I button (for italics), and a U button (for underlined).  (The keyboard shortcuts for these commands are the same as commonly used for other applications.)  These styles may also be combined.

There are also buttons for aligning a paragraph on the left,

centering it,

or aligning it right.  (Note that it is easiest to have different paragraph in different cells for this type of formatting.)

You may also change the font or size of the text from the Writing Assistant.

Practice Exercises

1.  Find a unit vector in the direction of s, the angle between r and s, and the projection of r orthogonal to s, where r=[1,-2,3] and s=[0,2,-4].

Solution

r={1,-2,3}
s={0,2,-4}

Normalize[s]

VectorAngle[r,s]
N[%]

Here is the vector projection of r onto s:

Projection[r,s]

Then the projection of r orthogonal to s is r minus the above.

r-%

2. Solve the following system :

3 x+2 y-4 w=7
4 x-5 y+3 w=-11
x+6 y-3 w=2

Solution

eq6=3*x+2*y-4*w==7
eq7=4*x-5*y+3*w==-11
eq8=x+6*y-3*w==2

Solve[{eq6,eq7,eq8},{x,y,w}]