Skip to contentSkip to lesson map

Introduction to Vectors II

Reading
MMAC Ch 3.1, pp 273-276
Practice
Optional HW 16 Q2.1, Q3.4, and Q4.4.
Lesson map

Objectives

  1. Use R to define a vector.
  2. Use R to perform vector addition, subtraction, and scalar multiplication.
  3. Given a vector field and an input point, evaluate the field at that point and draw the 2-D output vector by hand, originating from the input point.
  4. Use R to plot a given vector field.

1. Defining a Vector in R

Objective. Use R to define a vector.

The command c(...) is the R function that returns a vector whose components are its inputs, in the order listed. The c stands for combine.

u = c(-1, 1)
uthe vector’s name
=the assignment that stores the vector under that name and prints nothing
c(...)the command
-1the first component
1the second component

Grouping parentheses. Plain parentheses group one calculation, as in (2 + 3) * 4. R rejects (1, 2, 3), because a list of numbers inside plain parentheses is a syntax error. The vector comes from c(1, 2, 3). The command c() is the only way to build a vector in this course.

The name c. Naming a vector c is legal but confusing, because c() still works as the command. Prefer another short name: k, u, vec1.

Example 1

Use R to store the vectors

bu‾=(−11),bv‾=(40),bw‾=(−33), \overline{\vphantom{b}u} = \begin{pmatrix*}[r] -1 \\ 1 \end{pmatrix*}, \qquad \overline{\vphantom{b}v} = \begin{pmatrix*}[r] 4 \\ 0 \end{pmatrix*}, \qquad \overline{\vphantom{b}w} = \begin{pmatrix*}[r] -3 \\ 3 \end{pmatrix*},

then print bu‾\overline{\vphantom{b}u}.

ShowHide solution

Store each vector under its name with c(), then type the name u to print it:

u = c(-1, 1)
v = c(4, 0)
w = c(-3, 3)
u

The last line prints [1] -1 1.

R’s printed output.

  • Bracketed output. R prints [1] 3 1 for the vector (31)\begin{pmatrix*}[r] 3 \\ 1 \end{pmatrix*}. The bracketed number [1] gives the position of the first value on that line and is left out of the answer.
  • Row versus column. R prints a vector across the line. In Math 141 a vector is written down a column. Write the output [1] 3 1 as (31)\begin{pmatrix*}[r] 3 \\ 1 \end{pmatrix*} on paper and on Gradescope.

Example 2

A drone climbs from its launch point to a point 30 meters east, 40 meters north, and 120 meters up. Store its displacement

bd‾=(3040120) \overline{\vphantom{b}d} = \begin{pmatrix*}[r] 30 \\ 40 \\ 120 \end{pmatrix*}

in R and print it.

ShowHide solution

List the three components in order inside c(), then type the name. Used R.

d = c(30, 40, 120)
d
[1]  30  40 120

R prints the components across the line in the order typed: east, north, and up. The first printed number after [1], 30, is the first component d1d_{1}. Written as a column, the output is bd‾\overline{\vphantom{b}d}.

Next: 2. Vector Arithmetic in RLesson map

2. Vector Arithmetic in R

Objective. Use R to perform vector addition, subtraction, and scalar multiplication.

Vector addition, subtraction, and scalar multiplication are defined in Lesson 15 section 4. Type each one in R with the ordinary arithmetic symbol.

DescriptionHandwrittenR Code
Vector additionbu‾+bv‾\overline{\vphantom{b}u} + \overline{\vphantom{b}v}u + v
Vector subtractionbu‾−bv‾\overline{\vphantom{b}u} - \overline{\vphantom{b}v}u - v
Scalar multiplication3bv‾3\overline{\vphantom{b}v}3 * v

Example 3

For

bu‾=(−11),bv‾=(40),bw‾=(−33), \overline{\vphantom{b}u} = \begin{pmatrix*}[r] -1 \\ 1 \end{pmatrix*}, \qquad \overline{\vphantom{b}v} = \begin{pmatrix*}[r] 4 \\ 0 \end{pmatrix*}, \qquad \overline{\vphantom{b}w} = \begin{pmatrix*}[r] -3 \\ 3 \end{pmatrix*},

compute bu‾+bv‾\overline{\vphantom{b}u} + \overline{\vphantom{b}v}, 2bv‾+bw‾2\overline{\vphantom{b}v} + \overline{\vphantom{b}w}, and −bw‾+3bu‾-\overline{\vphantom{b}w} + 3\overline{\vphantom{b}u} in R.

ShowHide solution

Store the three vectors, then type each combination with the arithmetic symbols. Used R.

u = c(-1, 1)
v = c(4, 0)
w = c(-3, 3)
u + v
[1] 3 1
2 * v + w
[1] 5 3
-w + 3 * u
[1] 0 0

Each of the last three R lines computes one combination, in the order asked. Written as columns, the three results are

bu‾+bv‾=(31),2bv‾+bw‾=(53),−bw‾+3bu‾=(00). \overline{\vphantom{b}u} + \overline{\vphantom{b}v} = \begin{pmatrix*}[r] 3 \\ 1 \end{pmatrix*}, \qquad 2\overline{\vphantom{b}v} + \overline{\vphantom{b}w} = \begin{pmatrix*}[r] 5 \\ 3 \end{pmatrix*}, \qquad -\overline{\vphantom{b}w} + 3\overline{\vphantom{b}u} = \begin{pmatrix*}[r] 0 \\ 0 \end{pmatrix*}.

Example 4

Two drones sit at the positions br‾1\overline{\vphantom{b}r}_{1} and br‾2\overline{\vphantom{b}r}_{2}, in meters east, north, and up from the launch point:

br‾1=(3040120),br‾2=(1060100). \overline{\vphantom{b}r}_{1} = \begin{pmatrix*}[r] 30 \\ 40 \\ 120 \end{pmatrix*}, \qquad \overline{\vphantom{b}r}_{2} = \begin{pmatrix*}[r] 10 \\ 60 \\ 100 \end{pmatrix*}.

In R, compute the displacement br‾2−br‾1\overline{\vphantom{b}r}_{2} - \overline{\vphantom{b}r}_{1} from the first drone to the second, and half of that displacement, 0.5(br‾2−br‾1)0.5(\overline{\vphantom{b}r}_{2} - \overline{\vphantom{b}r}_{1}).

ShowHide solution

Store both positions, subtract with -, and scale with *. The parentheses make R subtract before it scales. Used R.

r1 = c(30, 40, 120)
r2 = c(10, 60, 100)
r2 - r1
[1] -20  20 -20
0.5 * (r2 - r1)
[1] -10  10 -10

Written as columns, the two results are

br‾2−br‾1=(−2020−20),0.5(br‾2−br‾1)=(−1010−10). \overline{\vphantom{b}r}_{2} - \overline{\vphantom{b}r}_{1} = \begin{pmatrix*}[r] -20 \\ 20 \\ -20 \end{pmatrix*}, \qquad 0.5(\overline{\vphantom{b}r}_{2} - \overline{\vphantom{b}r}_{1}) = \begin{pmatrix*}[r] -10 \\ 10 \\ -10 \end{pmatrix*}.

The second drone sits 20 meters west, 20 meters north, and 20 meters down from the first. Half of that displacement leads from the first drone to the point halfway between the two drones: 10 meters west, 10 meters north, and 10 meters down.

Matching dimensions. Addition and subtraction require matching dimensions, but R adds vectors of different dimensions anyway. It repeats the components of the vector with fewer components until the two counts match, and returns a vector of the larger dimension. R prints a warning only when the larger dimension is not a whole multiple of the smaller one, as in Example 5, and prints none in Example 6.

  1. Count the components of both vectors before typing anything. If the counts differ, the operation is undefined, whatever R prints.
  2. On Gradescope, click the Not Applicable box for each missing component, or for every component when the operation is undefined, and leave the answer boxes blank.

Example 5

For

bu‾=(−11),bp‾=(123), \overline{\vphantom{b}u} = \begin{pmatrix*}[r] -1 \\ 1 \end{pmatrix*}, \qquad \overline{\vphantom{b}p} = \begin{pmatrix*}[r] 1 \\ 2 \\ 3 \end{pmatrix*},

find bu‾+bp‾\overline{\vphantom{b}u} + \overline{\vphantom{b}p}.

ShowHide solution

The sum is undefined, because bu‾\overline{\vphantom{b}u} has two components and bp‾\overline{\vphantom{b}p} has three. R still prints a result:

> u = c(-1, 1)
> p = c(1, 2, 3)
> u + p
[1] 0 3 2
Warning message:
In u + p : longer object length is not a multiple of shorter object length

On Gradescope, click the Not Applicable box for every component and leave the answer boxes blank.

Example 6

For

bu‾=(−11),bq‾=(1234), \overline{\vphantom{b}u} = \begin{pmatrix*}[r] -1 \\ 1 \end{pmatrix*}, \qquad \overline{\vphantom{b}q} = \begin{pmatrix*}[r] 1 \\ 2 \\ 3 \\ 4 \end{pmatrix*},

find bu‾+bq‾\overline{\vphantom{b}u} + \overline{\vphantom{b}q}.

ShowHide solution

The sum is undefined, because bu‾\overline{\vphantom{b}u} has two components and bq‾\overline{\vphantom{b}q} has four. This time R prints no warning, because four is a whole multiple of two:

u = c(-1, 1)
q = c(1, 2, 3, 4)
u + q
[1] 0 3 2 5

Next: 3. Vector Fields by HandLesson map

3. Vector Fields by Hand

Objective. Given a vector field and an input point, evaluate the field at that point and draw the 2-D output vector by hand, originating from the input point.

On a weather map, the wind at every point has a speed and a direction. Both are drawn as one arrow at that point.

Definition 1 (Vector field).

A vector field is a function whose input is a point and whose output is a vector of the same dimension as the input.

The wind on a weather map is a vector field: its input is a point (x,y)(x, y) on the map, and its output is the 2-D wind vector at that point.

The function f(x,y)=x+yf(x,y) = x + y is not a vector field, because its output is a single number, and the definition requires a vector output with two components for a 2-D input.

The two output components of a 2-D vector field bF‾\overline{\vphantom{b}F} are functions of xx and yy, the component functions gg and hh of bF‾\overline{\vphantom{b}F}:

bF‾(x,y)=(g(x,y)h(x,y)). \overline{\vphantom{b}F}(x,y) = \begin{pmatrix*}[r] g(x,y) \\ h(x,y) \end{pmatrix*}.
  1. Substitute the input point into both component functions.
  2. Compute both components. The pair is the output vector.
  3. Mark the input point on the axes.
  4. Draw the output vector with its tail at that mark: from the input point, count the first component across and the second component up, as on the Lesson 15 sheet, section 2.
Figure

Each arrow is an output vector of bF‾(x,y)=(x+yx−y)\overline{\vphantom{b}F}(x,y) = \begin{pmatrix*}[r] x+y \\ x-y \end{pmatrix*} based at one of the input points (1,0)(1,0), (2,0)(2,0), (1,1)(1,1), (0,2)(0,2), and (−1,−2)(-1,-2). A dot marks each input point. Every output vector is drawn at true length. The green arrow is Example 7 and the orange arrow is Example 8.

Example 7

Evaluate the vector field

bF‾(x,y)=(x+yx−y) \overline{\vphantom{b}F}(x,y) = \begin{pmatrix*}[r] x+y \\ x-y \end{pmatrix*}

at the input point (1,0)(1,0) and draw the output vector.

ShowHide solution

By the definition of a vector field, bF‾\overline{\vphantom{b}F} takes the point (1,0)(1,0) as its input and returns a 2-D vector. Substitute x=1x = 1 and y=0y = 0 into both component functions of bF‾\overline{\vphantom{b}F}:

bF‾(1,0)=(1+01−0)=(11). \overline{\vphantom{b}F}(1,0) = \begin{pmatrix*}[r] 1+0 \\ 1-0 \end{pmatrix*} = \begin{pmatrix*}[r] 1 \\ 1 \end{pmatrix*}.

Draw the output vector from the input point (1,0)(1,0) to (2,1)(2,1), one unit across and one unit up. It is the green arrow in the figure above.

Example 8

Evaluate the vector field

bF‾(x,y)=(x+yx−y) \overline{\vphantom{b}F}(x,y) = \begin{pmatrix*}[r] x+y \\ x-y \end{pmatrix*}

at the input point (−1,−2)(-1,-2) and draw the output vector.

ShowHide solution

Substitute x=−1x = -1 and y=−2y = -2 into both component functions, as in Example 7:

bF‾(−1,−2)=(−1+(−2)−1−(−2))=(−31). \overline{\vphantom{b}F}(-1,-2) = \begin{pmatrix*}[r] -1+(-2) \\ -1-(-2) \end{pmatrix*} = \begin{pmatrix*}[r] -3 \\ 1 \end{pmatrix*}.

Draw the output vector from (−1,−2)(-1,-2) to (−4,−1)(-4,-1), three units left and one unit up. It is the orange arrow in the figure above.

Example 9

A map gives positions (x,y)(x, y) in miles. The wind velocity in knots at the point (x,y)(x, y) is

bW‾(x,y)=(−yx). \overline{\vphantom{b}W}(x,y) = \begin{pmatrix*}[r] -y \\ x \end{pmatrix*}.

Find the wind velocity at the point (2,1)(2,1) and draw it on the map.

ShowHide solution

Substitute x=2x = 2 and y=1y = 1 into both component functions of bW‾\overline{\vphantom{b}W}.

bW‾(2,1)=(−(1)2)=(−12). \overline{\vphantom{b}W}(2,1) = \begin{pmatrix*}[r] -(1) \\ 2 \end{pmatrix*} = \begin{pmatrix*}[r] -1 \\ 2 \end{pmatrix*}.

The wind at (2,1)(2,1) is (−12)\left(\begin{smallmatrix} -1 \\ 2 \end{smallmatrix}\right) knots, 1 knot toward negative xx and 2 knots toward positive yy. Draw it with its tail at the input point (2,1)(2,1): one unit left and two units up reaches (1,3)(1,3).

Figure

Interactive version. At Vector Field Visualization, the output vector moves from input point to input point, and the substitution is written out at each input point.

Next: 4. Plotting a Vector Field in RLesson map

4. Plotting a Vector Field in R

Objective. Use R to plot a given vector field.

  1. Define the field: F = makeFun(c(x + y, x - y) ~ x & y).
  2. Evaluate it at a point by calling it: F(1, 0) returns [1] 1 1.
  3. Plot it: plotVectorField(F(x, y) ~ x & y).
  4. To make the picture easier to read, add one or more of the four options below. Type mathaxis.on() and grid.on() on their own lines after the plot.

    • The command mathaxis.on() draws thick xx- and yy-axes through the origin.
    • The command grid.on() adds dashed grid lines.
    • The arguments xlim = c(-5, 5) and ylim = c(-5, 5) inside plotVectorField set the window. Writing range(-5, 5) in place of c(-5, 5) sets the same window.
    • The argument col = "red" inside plotVectorField sets the arrow color.

The c() command inside makeFun. The command c() makes the output a vector. With the command, makeFun(c(x + y, x - y) ~ x & y) defines a vector field. Without the command, makeFun(x + y ~ x & y) defines an ordinary function returning one number.

The inputs after the tilde. Both forms require the part ~ x & y, because that part lists both inputs.

Drawn versus true length. The drawn arrows are not at the true lengths of the output vectors. The command plotVectorField rescales every arrow by the same factor so that neighboring arrows do not overlap. Two arrows drawn the same length have equal true lengths. An arrow drawn shorter than another is shorter in true length. An exact output vector comes from substituting the input point by hand or from calling the field at that point in R.

Example 10

Define the vector field

bG‾(x,y)=(2yx−1) \overline{\vphantom{b}G}(x,y) = \begin{pmatrix*}[r] 2y \\ x-1 \end{pmatrix*}

in R and plot it.

ShowHide solution

Define bG‾\overline{\vphantom{b}G} with c() inside makeFun, then plot it with the axes and grid lines on:

G = makeFun(c(2*y, x - 1) ~ x & y)
plotVectorField(G(x, y) ~ x & y)
mathaxis.on()
grid.on()
G(1, 1)

As a check, substituting x=1x = 1 and y=1y = 1 by hand gives

bG‾(1,1)=(2(1)1−1)=(20), \overline{\vphantom{b}G}(1,1) = \begin{pmatrix*}[r] 2(1) \\ 1-1 \end{pmatrix*} = \begin{pmatrix*}[r] 2 \\ 0 \end{pmatrix*},

and the arrow at (1,1)(1,1) in the plot points in the positive xx direction with no vertical part. The line G(1, 1) computes the same substitution in R and prints [1] 2 0.

Example 11

A map gives positions (x,y)(x, y) in miles. The wind velocity in knots at the point (x,y)(x, y) is

bW‾(x,y)=(−yx). \overline{\vphantom{b}W}(x,y) = \begin{pmatrix*}[r] -y \\ x \end{pmatrix*}.

Plot the wind field in R over the window with −3≤x≤3-3 \le x \le 3 and −3≤y≤3-3 \le y \le 3, with red arrows, and check the plotted arrow at (2,1)(2,1) against Example 9.

ShowHide solution

Define bW‾\overline{\vphantom{b}W} with c() inside makeFun, plot it with the window and color options, and evaluate it at (2,1)(2,1). Used R.

W = makeFun(c(-y, x) ~ x & y)
plotVectorField(W(x, y) ~ x & y,
xlim = c(-3, 3), ylim = c(-3, 3), col = "red")
W(2, 1)
[1] -1  2

The output [1] -1 2 is the column (−12)\left(\begin{smallmatrix} -1 \\ 2 \end{smallmatrix}\right), the wind velocity found by hand in Example 9. In the plot, the red arrow at (2,1)(2,1) points left and up, rising two units for each unit left.

Lesson map