



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 5
This page cannot be seen from the preview
Don't miss anything!




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 >‖ =
5 by the distance formula. MATLAB syntax: >> norm([1 2])
Vector Operations
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]
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:
[ 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 2 3 4 5 6
] ⇒ 2 A =
[ 2 4 6 8 10 12
]
MATLAB syntax: >> 2*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 |~u|
) ~u. Find the unit vector in the direction of ~u =< − 8 , − 14 , 25 > using one MATLAB command.
(a) a =
[ 1 2
] (b) b =
[ 1 22
]
(c) c = [1 2 3 4 5] (d) d = [1 1 1 1 1]
x + 2y − 3 z = − 5 2 x − y − z = 0 −x − y + z = 1
Partial Answers to Practice Problems