MATLAB Workshop 3 - Vectors in MATLAB



Similar documents
Algebra 2 Chapter 1 Vocabulary. identity - A statement that equates two equivalent expressions.

(!' ) "' # "*# "!(!' +,

A vector is a directed line segment used to represent a vector quantity.

Trigonometric Functions and Triangles

2 Session Two - Complex Numbers and Vectors

Lecture L3 - Vectors, Matrices and Coordinate Transformations

Section V.3: Dot Product

Mathematics Pre-Test Sample Questions A. { 11, 7} B. { 7,0,7} C. { 7, 7} D. { 11, 11}

Lecture 2 Matrix Operations

Algebra. Exponents. Absolute Value. Simplify each of the following as much as possible. 2x y x + y y. xxx 3. x x x xx x. 1. Evaluate 5 and 123

FX 115 MS Training guide. FX 115 MS Calculator. Applicable activities. Quick Reference Guide (inside the calculator cover)

Evaluating trigonometric functions

Basic Use of the TI-84 Plus

2+2 Just type and press enter and the answer comes up ans = 4

MatLab Basics. Now, press return to see what Matlab has stored as your variable x. You should see:

Figure 1.1 Vector A and Vector F

Trigonometry Review with the Unit Circle: All the trig. you ll ever need to know in Calculus

SYSTEMS OF EQUATIONS AND MATRICES WITH THE TI-89. by Joseph Collison

MATLAB Basics MATLAB numbers and numeric formats

AP Physics - Vector Algrebra Tutorial

Linear algebra and the geometry of quadratic equations. Similarity transformations and orthogonal matrices

Beginner s Matlab Tutorial

MATH 60 NOTEBOOK CERTIFICATIONS

Vector Notation: AB represents the vector from point A to point B on a graph. The vector can be computed by B A.

Matrices 2. Solving Square Systems of Linear Equations; Inverse Matrices

Quick Tour of Mathcad and Examples

Linear Algebra Notes for Marsden and Tromba Vector Calculus

Below is a very brief tutorial on the basic capabilities of Excel. Refer to the Excel help files for more information.

Solutions to old Exam 1 problems

AMATH 352 Lecture 3 MATLAB Tutorial Starting MATLAB Entering Variables

One advantage of this algebraic approach is that we can write down

To Evaluate an Algebraic Expression

arrays C Programming Language - Arrays

Tom wants to find two real numbers, a and b, that have a sum of 10 and have a product of 10. He makes this table.

PYTHAGOREAN TRIPLES KEITH CONRAD

Introduction to Matrices for Engineers

Typical Linear Equation Set and Corresponding Matrices

The Force Table Introduction: Theory:

Mechanics 1: Conservation of Energy and Momentum

9.4. The Scalar Product. Introduction. Prerequisites. Learning Style. Learning Outcomes

Using Casio Graphics Calculators

Graphing Calculator Workshops

Vector has a magnitude and a direction. Scalar has a magnitude

Creating Basic Excel Formulas

Rational Exponents. Squaring both sides of the equation yields. and to be consistent, we must have

Week 13 Trigonometric Form of Complex Numbers

A few useful MATLAB functions

Unified Lecture # 4 Vectors

Experiment 9. The Pendulum

Homework 2 Solutions

Student Performance Q&A:

Vector Math Computer Graphics Scott D. Anderson

Differentiation of vectors

Definition: A vector is a directed line segment that has and. Each vector has an initial point and a terminal point.

The University of the State of New York REGENTS HIGH SCHOOL EXAMINATION MATHEMATICS B. Thursday, January 29, :15 a.m. to 12:15 p.m.

This unit will lay the groundwork for later units where the students will extend this knowledge to quadratic and exponential functions.

Microsoft Excel Tutorial

2.2 Magic with complex exponentials

Vector Algebra II: Scalar and Vector Products

Section 10.4 Vectors

SAT Subject Math Level 2 Facts & Formulas

ab = c a If the coefficients a,b and c are real then either α and β are real or α and β are complex conjugates

5: Magnitude 6: Convert to Polar 7: Convert to Rectangular

MATLAB Workshop 14 - Plotting Data in MATLAB

Display Format To change the exponential display format, press the [MODE] key 3 times.

13 MATH FACTS a = The elements of a vector have a graphical interpretation, which is particularly easy to see in two or three dimensions.

EL-9650/9600c/9450/9400 Handbook Vol. 1

Algebra Cheat Sheets

Negative Integral Exponents. If x is nonzero, the reciprocal of x is written as 1 x. For example, the reciprocal of 23 is written as 2

MATH Fundamental Mathematics IV

The Center for Teaching, Learning, & Technology

Additional Topics in Math

Chapter 17. Orthogonal Matrices and Symmetries of Space

5.1 Radical Notation and Rational Exponents

Math Placement Test Practice Problems

Recall the basic property of the transpose (for any A): v A t Aw = v w, v, w R n.

A linear combination is a sum of scalars times quantities. Such expressions arise quite frequently and have the form

In order to describe motion you need to describe the following properties.

Examples of Functions

ALGEBRA 2/TRIGONOMETRY

THE COMPLEX EXPONENTIAL FUNCTION

Summary of important mathematical operations and formulas (from first tutorial):

Georgia Department of Education Kathy Cox, State Superintendent of Schools 7/19/2005 All Rights Reserved 1

SCIENTIFIC CALCULATOR OPERATION GUIDE. <Write View>

Simulation Tools. Python for MATLAB Users I. Claus Führer. Automn Claus Führer Simulation Tools Automn / 65

Introduction to Matrix Algebra

MTN Learn. Mathematics. Grade 10. radio support notes

Lab 2: Vector Analysis

Example SECTION X-AXIS - the horizontal number line. Y-AXIS - the vertical number line ORIGIN - the point where the x-axis and y-axis cross

Vectors VECTOR PRODUCT. Graham S McDonald. A Tutorial Module for learning about the vector product of two vectors. Table of contents Begin Tutorial

Content. Chapter 4 Functions Basic concepts on real functions 62. Credits 11

MATH2210 Notebook 1 Fall Semester 2016/ MATH2210 Notebook Solving Systems of Linear Equations... 3

IV. ALGEBRAIC CONCEPTS

Algebra I Vocabulary Cards

4. How many integers between 2004 and 4002 are perfect squares?

Continued Fractions and the Euclidean Algorithm

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS. + + x 2. x n. a 11 a 12 a 1n b 1 a 21 a 22 a 2n b 2 a 31 a 32 a 3n b 3. a m1 a m2 a mn b m

Section 9.1 Vectors in Two Dimensions

MAT188H1S Lec0101 Burbulla

Introduction to Matrices

Transcription:

MATLAB: Workshop - Vectors in MATLAB page 1 MATLAB Workshop - Vectors in MATLAB Objectives: Learn about vector properties in MATLAB, methods to create row and column vectors, mathematical functions with vectors, and element-by-element vector operations. MATLAB Features: vector/matrix variable properties Property Comment user chooses names name should represent meaning of variable ex: use radius rather than x or s for radius var_name letters, digits (0-9), underscore ( _ ) allowed in name must start with a letter case sensitive ( Radius is different than radius ) can be any length, but first 1 characters must be unique all numerical values are double precision ~ 16 significant figures (not decimal places) and imaginary value has both real and imaginary parts a ± bi (where i = ( 1 )) both a and b are double precision if b = 0, MATLAB only displays real part, a memory location MATLAB assigns - you do not need to worry about this order of precedence rules for vector/matrix arithmetic operators Order of Symbol Meaning Precedence 1 ( ) group together ' transpose (exchange rows and columns) 2.^ raise each element to indicated power ^ multiply matrix by itself indicated number of times.* element-by-element multiplication./ element-by-element right division.\ element-by-element left division * multiply two matrices / matrix right division \ matrix left division + addition 4 - subtraction colon (:) operator - a special vector/matrix operator Notation Action Result a:c creates vector with elements 1 apart [a a+1 a+2... c] a:b:c creates vector with elements b apart [a a+b a+2b... ( c)]

MATLAB: Workshop - Vectors in MATLAB page 2 vector functions create vector create vector with N elements from a to b number of elements maximum value maximum value & index minimum value minimum value & index average of all values median of all values standard deviation of all values sum of all values vec = a:b:c vec = linspace(a:b:n) length(x) max(x) [greatest, index] = max(x) min(x) [least, index] = min(x) mean(x) median(x) std(x) sum(x)

MATLAB: Workshop - Vectors in MATLAB page Working with vectors in MATLAB MATLAB has two types of vectors (row and column) and offers several methods for creating vectors for use. We will use a naming convention that vector variable names contain only lower case letters. (1) Creating row vectors in MATLAB. (a) row_vec1 = [, 9, -4] row_vec1 = 9-4 A row vector has been created. row_vec1(1) ans = The value of the first element is displayed row_vec1() ans = -4 The value of the third element is displayed (b) row_vec2 = [7-2 12] row_vec2 = 7-2 12 A second row vector has been created. Note: elements separated by spaces rather than commas. Notes on vectors and vector elements The vector name, vec_name, refers collectively to all elements in the vector. Each element has an associated index that uniquely identifies the element. Index counting starts at 1 vec_name(1) refers to the first element, vec_name(2) the second, etc. When creating vectors use square braces [ ]; when accessing elements use parentheses ( ). Elements in a row vector can be separated by either commas or blank spaces. (c) (d) row_vec = 1:4 row_vec = 1 2 4 A row vector has been created using the colon operator, :. row_vec4 = [1:4] row_vec4 = 1 2 4

MATLAB: Workshop - Vectors in MATLAB page 4 Another row vector has been created using the colon operator. Square braces, [ ], are optional. (e) (f) row_vec5 = 1:2:5 row_vec5 = 1 5 The colon operator has been used to create a row vector with elements spaced 2 apart. row_vec6 = [1:-:-6] row_vec6 = 1-2 -5 The colon operator has been used to create a row vector with elements spaced - apart. Notes on colon notation start_value:increment:end_value Colon notation can be used to create row vectors starting at start_value and ending at end_value. Use of square braces is optional when using colon notation. If increment is not specified, increment equals one The increment cannot add a value to the vector beyond end_value the last element in the vector is less than or equal to end_value The increment can be negative if end_value < start_value (g) (h) start = 12; stop = 24; numel = 5; row_vec7 = linspace(start,stop,numel) row_vec7 = 12 15 18 21 24 linspace has been used to create a row vector with ascending values. start = 12; stop = 0; numel = 5; row_vec8 = linspace(start,stop,numel) row_vec8 = 12 9 6 0 linspace has been used to create a row vector with descending values. Notes on colon notation and linspace Variable names can be used rather than numbers to specify start, stop, and increment. Remember, a variable name is a command to go get the value assigned to the variable. Colon notation is used to create vectors with evenly spaced elements (increment) between start and stop. linspace is used to create a vector with a specified number of elements (numel) starting with start and ending with end

MATLAB: Workshop - Vectors in MATLAB page 5 (2) Creating column vectors in MATLAB. (a) col_vec1 = [; 9; -4] col_vec1 = 9-4 A column vector has been created. Rows are separated by a semi-colon. col_vec1(1) ans = The value of the first element is displayed. col_vec1() ans = -4 The value of the third element is displayed. Notes on column vectors The vector name, vec_name, refers collectively to all elements in the vector. Each element has an associated index that uniquely identifies the element. Index counting starts at 1 vec_name(1) refers to the first element, vec_name(2) the second, etc. When creating vectors use square braces [ ]; when accessing elements use parentheses ( ). Elements in a column vector are separated by semi-colons. () Transposing vectors in MATLAB. vec1 = [, 9, -4] vec1 = 9-4 A row vector is created. (use single quote key) vec1tr = vec1' vec1tr = 9-4 The row vector is transposed to a column vector. (use single quote key) vec1trtr = vec1tr' vec1trtr = 9-4

MATLAB: Workshop - Vectors in MATLAB page 6 The column vector is transposed to a row vector. We can change from row to column or column to row vectors at will by using the transpose operator, (the single quote key), ' (4) Extracting elements from vectors in MATLAB. xvec = 1:6 xvec = 1 2 4 5 6 xvec_subset = xvec(2:4) xvec_subset = 2 4 The colon operator can be used to extract a specified range of elements from a vector. (5) Determining the number of elements in a vector. length(xvec) ans = 6 numel_rv5 = length(row_vec5) numel_rv5 = numel_cv1 = length(col_vec1) numel_cv1 = The length function returns the number of elements (length) in a row or column vector. (6) Mathematical functions using vectors in MATLAB. xvec = linspace(0,pi/2,5) xvec = 0 0.927 0.7854 1.1781 1.5708 sin_xvec = sin(xvec) sin_xvec = 0 0.827 0.7071 0.929 1.0000 exp_xvec = exp(xvec) exp_xvec = 1.0000 1.4810 2.19.2482 4.8105

MATLAB: Workshop - Vectors in MATLAB page 7 MATLAB processes a vector in mathematical functions such as sin and cos element-by-element to produce a vector of the same length where each element in the resulting vector results from performing the specified function on the corresponding element of the argument vector. (7) Some basic vector operations in MATLAB. MATLAB is a convenient engineering problem solving tool because it has many canned rountines or functions that find frequent use in solving problems. For example, think of a vector consisting of grades on an exam. Questions that students frequently ask about the exam are: (1) what was the average? (2) what is the median? () what was the maximum score (students rarely ask about the minimum score) (4) what was the standard deviation? (5) will the grades be curved? (MATLAB is not much use to answer this.) A variety of vector functions that can answer these and other questions are provided at the start of this workshop. grades = [97 67 78 88 92 94 84 79 62 95 81 7 91 85 84]; average = mean(grades) average = 8. mid = median(grades) mid = 84 [high, stnum] = max(grades) high = 97 stnum = 1 stdev = std(grades) stdev = 10.2725 Notes on MATLAB functions Many MATLAB functions are available to perform different jobs. Functions may return more than one value (parameter) max returns two values the first is the maximum value in the vector the second is the location (index) of the maximum value in the vector DO NOT give variables the same name as a MATLAB function!!!!! ex: sum = 1+2+ sum = 6 total = sum(grades)??? Index exceeds matrix dimensions. This cryptic error message results because sum has been redefined and now refers to the variable rather than the function!!!

MATLAB: Workshop - Vectors in MATLAB page 8 Arithmetic operations with vectors in MATLAB MATLAB has arithmetic operations that process vectors either element-by-element (term-byterm) or by matrix algebra rules. For example, consider the function 2 at x = + v0t + x0 2 which describes the displacement of an object under constant acceleration, a, starting at position x 0 with initial velocity v 0. We might like to display a graph of x vs t. In order to do this, we would need a vector of t-points (the independent variable) and a corresponding vector of x-points (the dependent variable). Generally, we will create an evenly spaced set of independent variable points and then calculate the corresponding dependent variable points. (8) Creating a set of (x,y) data points in MATLAB. We will generically say (x,y) data points for (independent, dependent) data points. For the displacement problem, the (x,y) data set is really a (time,displacement) data set. Why? diary wkshp_ac7 % Solution to Workshop, Activity 7 % Your Name(s) % Class (e.g. Engr 0012, TH 10:00, Instructor Name) % Today s Date % Your e-mail address % set problem parameters accel = 1.0; % acceleration, m/s/s initvel = 0.0; % initial velocity, m/s initpos = 0.0; % initial position, m time = 0:5 % time range, s time = 0 1 2 4 5 % compute corresponding displacement, m displacement = accel*time.^2/2.0 +... initvel*time + initpos displacement = 0 0.5000 2.0000 4.5000 8.0000 12.5000 Note that the element-by-element exponentiation operator,.^, was used to square the time. This told MATLAB to process the vector element-by-element so that a displacement element was created for each time element. Also note that, if a line is extending off the screen, it can be broken and continued on the next line by using... Using this feature makes your command window easier to read and avoids needing to use the scroll bars to see everything that you have written. See what happens if you forget to use the element-by-element exponentiation operator,.^, and use the matrix exponentiation operator, ^, instead. (The difference is a period or dot before the hat). Use the up arrow to move up to the displacement = command,

MATLAB: Workshop - Vectors in MATLAB page 9 change the operator by deleting the period, and press enter. Then use the up arrow to recall the rest of the equation and press enter. displacement = accel*time^2/2.0 +... initvel*time + initpos??? Error using ==> ^ Matrix must be square. diary off MATLAB tried to use the rules of matrix algebra to multiply time by itself. However, a vector cannot be multiplied by itself under those rules, so an error message was issued. (9) Creating another set of (x,y) data points in MATLAB. Let s repeat the previous activity for the equation ( α / 2π ) d ( α ) = 0.5 + 0.4e sin( α ) which describes the displacement of an irregular cam in cm about its center as a function of angular rotation, α, in radians. Cam shafts are used in engines to control the stroke of the piston. We will look at the displacement through one revolution. diary wkshp_ac8 % Solution to Workshop, Activity 8 % Your Name(s) % Class (e.g. Engr 0012, TH 10:00, Instructor Name) % Today s Date % Your e-mail address % set problem parameters alpha = linspace(0,2*pi,20) % alpha range, radians alpha = Columns 1 through 7 0 0.07 0.6614 0.9921 1.228 1.655 1.9842 Columns 8 through 14 2.149 2.6456 2.9762.069.676.968 4.2990 Columns 15 through 20 4.6297 4.9604 5.2911 5.6218 5.9525 6.282 % compute corresponding displacement, cm displacement = 0.5 + 0.4*exp(-alpha/2/pi).*sin(alpha) displacement = Columns 1 through 7 0.5000 0.622 0.7211 0.7860 0.8141 0.8064 0.7671 Columns 8 through 14 0.706 0.6250 0.5410 0.4611 0.9 0.45 0.152 Columns 15 through 20 0.092 0.29 0.557 0.996 0.4496 0.5000 Note how MATLAB reports the contents of a (row) vector if it contains more elements than can be displayed on a single line. Also note the use of linspace rather than the colon

MATLAB: Workshop - Vectors in MATLAB page 10 operator to create values for the angle vector. Why was linspace used? Finally, note that multiplication by scalars does not require the element-by-element operator while multiplication of the two vectors created by the exp and sin functions does. A function with a vector as an argument creates a new vector with values obtained from element-byelement processing of the function. What happens if you forget to use the element-by-element exponentiation operator,.^, and use the matrix exponentiation operator, ^, instead? displacement = 0.5 + 0.4*exp(-alpha/2/pi)*sin(alpha)??? Error using ==> * Inner matrix dimensions must agree. diary off MATLAB tried to use the rules of matrix algebra to multiply the two vectors. However, two vectors cannot be multiplied under those rules, so an (different) error message was issued. Exercises: Perform the following operations using array arithmetic where appropriate. 1. Equation of a line. The equation of a line is given by y = mx + b where m is the slope (a scalar) and b is the intercept (also a scalar). a. Compute the corresponding y-coordinates for the following x-coordinates if m = and b = -1 x = [1, 2,, 5, 8, 1, 21] b. Compute the corresponding x-coordinates for the following y-coordinates if m = 5 and b = 2. y = [0, 1, 1, 2,, 5, 8] c. Compute the corresponding y-coordinates for the following x-coordinates if m = π/2 and b = 2/π x = linspace(0,0,8) d. Given the equation d = sin( t) + t / 2, compute the corresponding d-coordinates for t = 0:10 2. Vector multiplication, division, exponentiation. Create a vector, g, with 10 evenly spaced elements starting at 1 and ending at 10. Compute the following with vector operations: a. h = g cos(g) b. c. g 1 z = h + 1 2 cos( g ) s = (try this by squaring g and then by multiplying g times g) hg

MATLAB: Workshop - Vectors in MATLAB page 11 t / 2 d. Given the equation q = 5sin( π t / 2.5) e, compute the corresponding q-coordinates for t = 0:100. Parametric equation for a circle. The parametric equation for a circle is x = r cos(θ ) and y = r sin(θ ) where r is the radius and θ is the angle of rotation counter-clockwise from the positive 2 2 2 x-axis. Defined this way, x and y satisfy the equation x + y = r. Show this using MATLAB. Use linspace to create an angle vector, theta, with values (0, π/, 2π/, π, 4π/, 5π/, 2π). Compute the corresponding x- and y-vectors for r = 5. Show that the x- and y-vectors satisfy the equation of a circle. Recap: You should have learned How to declare row and column vectors in MATLAB How to declare a vector with evenly spaced elements (two methods) How to transpose a vector How to extract elements from a vector Arithmetic operations between a scalar and a vector Arithmetic operations between two vectors Simple function operations with a vector Arithmetic operations between functions of vectors