MATH.2720 Introduction to Programming with MATLAB, Exercises of Algebra

A. Vectors. A vector is a quantity that has both magnitude and direction, like velocity. The location of a vector is irrelevant; all that matters are ...

Typology: Exercises

2022/2023

Uploaded on 03/01/2023

deville
deville 🇺🇸

4.7

(23)

390 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
MATH.2720 Introduction to Programming with MATLAB
Vector and Matrix Algebra
A. Vectors
Avector is a quantity that has both magnitude and direction, like velocity. The location of a vector
is irrelevant; all that matters are magnitude and direction. You can visualize a vector as an arrow,
with the length of the arrow representing the magnitude of the vector and the direction of the
arrow representing the direction of the vector.
A vector is usually denoted by a bold-face lower-case letter (e.g. v) or by a lower-case letter with
an arrow above it (e.g. ~v). The magnitude (or norm) of a vector ~v is usually denoted either k~vkor
|~v|.
If you think of a vector as an arrow with its tail at the origin of a coordinate system, you can
describe the vector analytically by specifying the location of the head of the vector. For example,
~v =<1,2>is the vector in the xy plane that starts at the origin and ends at the point (1,2). A
vector can have 2, 3, or more components. The magnitude of a vector is the distance from the tail
to the head of the vector. For example, k<1,2>k=12+ 22=5 by the distance formula.
MATLAB syntax: >> norm([1 2])
Vector Operations
1. Scalar Multiplication.
If kis a real number (a scalar), then k~v is the vector with magnitude |k| k~vkand direction
the same direction as ~v if k > 0 and the opposite direction of ~v if k < 0.
Analytical definition: k < v1, v2, v3>=< kv1, kv2, kv3>.
For example, 2<1,2,3>=<2,4,6>
MATLAB syntax: >> -2*[1 2 3]
2. Vector Addition.
Geometric definition of ~v +~w: Place the tail of ~w at the head of ~v. The vector from the tail
of ~v to the head of ~w is ~v +~w. See the figure below.
v
w
v+w
Analytical definition of vector addition:
< v1, v2, v3>+< w1, w2, w3>=< v1+w1, v2+w2, v3+w3>.
For example, <1,2,3>+<4,5,6>=<5,7,9>
MATLAB syntax: >> [1 2 3] + [4 5 6]
pf3
pf4
pf5

Partial preview of the text

Download MATH.2720 Introduction to Programming with MATLAB and more Exercises Algebra in PDF only on Docsity!

MATH.2720 Introduction to Programming with MATLAB Vector and Matrix Algebra

A. Vectors

A vector is a quantity that has both magnitude and direction, like velocity. The location of a vector is irrelevant; all that matters are magnitude and direction. You can visualize a vector as an arrow, with the length of the arrow representing the magnitude of the vector and the direction of the arrow representing the direction of the vector. A vector is usually denoted by a bold-face lower-case letter (e.g. v) or by a lower-case letter with an arrow above it (e.g. ~v). The magnitude (or norm) of a vector ~v is usually denoted either ‖~v‖ or |~v|. If you think of a vector as an arrow with its tail at the origin of a coordinate system, you can describe the vector analytically by specifying the location of the head of the vector. For example, ~v =< 1 , 2 > is the vector in the xy plane that starts at the origin and ends at the point (1, 2). A vector can have 2, 3, or more components. The magnitude of a vector is the distance from the tail to the head of the vector. For example, ‖< 1 , 2 >‖ =

12 + 2^2 =

5 by the distance formula. MATLAB syntax: >> norm([1 2])

Vector Operations

  1. Scalar Multiplication. If k is a real number (a scalar), then k~v is the vector with magnitude |k| ‖~v‖ and direction the same direction as ~v if k > 0 and the opposite direction of ~v if k < 0. Analytical definition: k < v 1 , v 2 , v 3 >=< kv 1 , kv 2 , kv 3 >. For example, − 2 < 1 , 2 , 3 >=< − 2 , − 4 , − 6 > MATLAB syntax: >> -2*[1 2 3]
  2. Vector Addition. Geometric definition of ~v + w~: Place the tail of w~ at the head of ~v. The vector from the tail of ~v to the head of w~ is ~v + w~. See the figure below.

v w

v+w

Analytical definition of vector addition: < v 1 , v 2 , v 3 > + < w 1 , w 2 , w 3 >=< v 1 + w 1 , v 2 + w 2 , v 3 + w 3 >. For example, < 1 , 2 , 3 > + < 4 , 5 , 6 >=< 5 , 7 , 9 > MATLAB syntax: >> [1 2 3] + [4 5 6]

  1. Dot Product (or Inner Product). The dot product of two vectors of the same length is a scalar. Geometric definition: ~v · w~ = ‖~v‖ ‖ w~‖ cos (θ), where θ is the angle between ~v and w~ when the vectors have their tails at the same point. Analytical definition: ~v · w~ = v 1 w 1 + v 2 w 2 + · · · + vnwn. For example, < 1 , 2 , 3 > · < 4 , 5 , 6 >= (1)(4) + (2)(5) + (3)(6) = 32. MATLAB syntax: >> dot([1 2 3], [4 5 6])
  2. Cross Product. The cross product of two 3-component vectors ~v and w~ is a vector with magnitude ‖~v‖ ‖ w~‖ sin (θ) and direction perpendicular to both ~v and w~ per the right-hand rule. Analytical definition: < v 1 , v 2 , v 3 > × < w 1 , w 2 , w 3 >=< v 2 w 3 − w 2 v 3 , v 3 w 1 − w 3 v 1 , v 1 w 2 − w 1 v 2 >. For example, < 1 , 0 , 3 > × < 0 , 2 , − 1 >=< 0(−1) − 2(3), 3(0) − (−1)(1), 1(2) − (−1)(0) > =< − 6 , 1 , 2 >. MATLAB syntax: >> cross([1 0 3], [0 2 -1])

B. Matrices

A matrix is a rectangular array of numbers. (The plural of matrix is matrices.) An m × n matrix is a matrix with m rows and n columns. Here is an example of a 2 × 3 matrix:

A =

[ 1 2 3 4 5 6

]

If A is a matrix, then Aij denotes the element in row i and column j of matrix A. For example, if A is the matrix defined above, then A 21 = 4. MATLAB syntax: >> A = [1 2 3; 4 5 6]

Matrix Operations

  1. Scalar Multiplication. If A is an m × n matrix and k is a scalar, then kA is the m × n matrix whose entries are k times the entries of A. For example, A =

[ 1 2 3 4 5 6

] ⇒ 2 A =

[ 2 4 6 8 10 12

]

MATLAB syntax: >> 2*A

  1. Matrix Addition. If A and B are m × n matrices, then A + B is the m × n matrix with (A + B)ij = Aij + Bij. For example,

A =

[ 1 2 3 4

] and B =

[ − 2 4 − 6 8

] ⇒ A + B =

[ − 1 6 − 3 12

]

MATLAB syntax: >> A+B

C. Systems of Linear Equations

Systems of linear equations can be expressed as matrix equations. For example, the system x 1 + 2x 2 = 4, 3 x 1 + 4x 2 = 10 can be written as the matrix equation Ax = b where A =

[ 1 2 3 4

] , x =

[ x 1 x 2

] , and b =

[ 4 10

] .

You can solve this system using the following MATLAB commands.

A = [1, 2; 3, 4]; b = [4; 10]; x = A\b %Note: This is the backslash key (above the Enter key), not / If the system Ax = b is overdetermined (more equations than unknowns), then A\b is a least- squares solution of the system, i.e., a vector x that minimizes ‖Ax − b‖. If the system Ax = b is underdetermined (more unknowns than equations), then A\b produces a solution of the system, if there are any, or a least-squares solution if the system has no solution.

D. Operations on Arrays

The symbol * denotes matrix multiplication. If you want to multiply corresponding elements of arrays with the same dimensions, use .* For example, >>[1 2 3][4 5 6] produces an error message in MATLAB, but >>[1 2 3].[4 5 6] produces the array [4 10 18]. Similarly, you can perform element-by-element division or exponentiation using ./ and .^ For example, [1 2 3].^2 produces the array [1 4 9] You can apply built-in MATLAB functions to arrays, just as you can to single numbers. For example, sqrt([1 4 9]) produces the array [1 2 3]

E. MATLAB Array Functions

Here are some useful MATLAB functions for working with arrays.

MATLAB Command Description max(A) Largest element of A, if A is a vector Row vector containing largest element in each column, if A is a matrix min(A) Same as max(A) but gives minimum instead sum(A) Sum of the elements of A if A is a vector mean(A) Average of the elements of A if A is a vector

Practice Problems

  1. The unit vector ~un in the direction of vector ~u is given by

( 1 |~u|

) ~u. Find the unit vector in the direction of ~u =< − 8 , − 14 , 25 > using one MATLAB command.

  1. Define the vector v = [2, 4 , 6 , 8 , 10]. Then use v to create the following vectors:

(a) a =

[ 1 2

] (b) b =

[ 1 22

]

(c) c = [1 2 3 4 5] (d) d = [1 1 1 1 1]

  1. Define the vectors ~u =< − 2 , 6 , 5 >, ~v =< 5 , − 1 , 3 >, and w~ =< 4 , 7 , − 2 >. Use MATLAB’s built-in functions cross and dot to verify the vector identity ~u × (~v × w~) = (~u · w~) ~v − (~u · ~v) w~.
  2. Solve the following system of linear equations.   

x + 2y − 3 z = − 5 2 x − y − z = 0 −x − y + z = 1

  1. (a) Generate the row array v = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]. (Can you do this without typing in all 10 elements?) (b) Use a MATLAB function to find the average value of the entries of v. (c) Use a MATLAB function to find the sum of the entries of v.

Partial Answers to Practice Problems

  1. Both sides of the equation should equal < 124 , − 17 , 70 >
  2. x = 1, y = 0, z = 2
  3. b) 38. 5 c) 385