User Tools

Site Tools


gibson:teaching:spring-2018:math445:lecture:vectors

This is an old revision of the document!


A PCRE internal error occured. This might be caused by a faulty plugin

====== Math 445 lecture 2: Vectors ====== topics <code> [] % creating vector vectors with square brackets () % accessing/assigning vector elements with parentheses ' % the transpose operator, apostrophe : % the colon operator, making range vectors + - % vector addition, subtraction .* ./ .^ % elementwise vector operations </code> ===== Vectors in math ===== Vectors are hugely important in math, science, engineering. In math, a vector is a list of numbers. E.g. this is a 3-d vector <latex> x = \left(\begin{array}{c} 4.3 \\ 5.9 \\ 0.1 \end{array} \right) </latex> In math, the components or elements of a vector are written with subscripts. E.g. for the above vector <latex> x_1 = 4.3, \quad x_2 = 5.9, \quad x_3 = 0.1 </latex> ===== Creating vectors with explicit lists in square brackets ===== In Matlab, there's a distinction between row vector and column vectors. You can construct **row vectors** by listing elements between **square brackets**, with either **spaces** or **commas** between. <code> >> x = [4.3 5.9 0.1] % assign a row vector into variable x x = 4.3000 5.9000 0.1000 </code> To create a column vectors, put semicolons between the elements. <code> >> x = [4.3; 5.9; 0.1] % assign a col vector into variable v x = 4.3000 5.9000 0.1000 </code> ===== Accessing elements with parentheses ===== In Matlab, you access the elements of a vector using **parentheses**. E.g. given the above vector $x$, you **get** the 2nd component of $x$ this way <code> >> x(2) ans = 5.9000 </code> Note what happened there. We asked Matlab for the 2nd element of x. It **returned** the value 5.9 and assigned it to the default return variable ''ans''. Alternatively, the following will **assign** or **set** the value of the second component of $x$ to another number. <code> >> x(2) = 2.7 x = 4.3000 2.7000 0.1000 </code> ===== The transpose operator ' (apostrophe) ===== Note that last method. In Matlab, the apostrophe '' ' '' stands for the **transpose**. The transpose operator turns a row vector into a column vector, and vice versa. <code> >> x = [4.3 5.9 0.1] x = 4.3000 5.9000 0.1000 >> x' ans = 4.3000 5.9000 0.1000 >> y = x' y = 4.3000 5.9000 0.1000 >> z = y' z = 4.3000 5.9000 0.1000 </code> ===== Constructing vectors with the colon operator : ===== The Matlab **colon** operator : is used in a number of ways. Here we'll see how it can be used to create vectors. The most straightforward of these is the syntax ''m:n'', which creates a row vector of the integers from ''m'' to ''n''. E.g. <code> >> 1:10 ans = 1 2 3 4 5 6 7 8 9 10 >> x = 4:7 x = 4 5 6 7 </code> You can also specify a **step** or **increment** between the elements using the syntax ''m:step:n''. For example, ''5:2:17'' will produce a vector of odd integers between 5 and 17. <code> >> x = 5:2:17 x = 5 7 9 11 13 15 17 </code> Lastly, the colon operator works with nonintegers, too. E.g. <code> >> x = 0:0.1:0.7 x = 0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 </code> ===== Constructing vectors with linspace ===== In Matlab, vectors are often used to represent *discretized functions*, that is, the values of a function at a discrete set of gridpoints. The starting place for this is often the **linspace** command: <code matlab> x = linspace(0, 2*pi, 100); % construct a vector x of 100 gridpoints evenly spaced between 0 and 2pi f = sin(x); % construct a vector f of sin evaluated at the gridpoints x whos x f Name Size Bytes Class Attributes f 1x100 800 double x 1x100 800 double </code> Note the use of the semicolon to supress output. Who wants to read all 100 elements of ''f'' and ''x''?. Note also that ''f'' **is not actually a function**. It's just a vector of the values of $\sin x$ at the 100 points in the vector ''x''. You can see that ''f'' is a vector by the output of ''whos x f'', which shows that the variables ''x'' and ''f'' are both size ''1 x 100'', i.e. row vectors of length 100. You can see this clearly by printing the values of ''f'' and ''x'' for a smaller grid. <code matlab> >> x = linspace(0, 2*pi, 5) x = 0 1.5708 3.1416 4.7124 6.2832 f = sin(x) f = 0 1.0000 0.0000 -1.0000 -0.0000 <code> ==== Dot syntax ==== Matlab has a special **dot syntax** for elementwise operations on vectors, for operations which have conflicting meanings in linear algebra. For example, in mathematics you can't simply multiply two vectors together. But if you're using vectors to represent discretized functions, sometimes you want to get the product of the functions from a pointwise multiplication of the vectors. For example, <code> x = linspace(0, 2*pi, 100); % construct a vector x of 100 gridpoints evenly spaced between 0 and 2pi f = sin(x); % construct a vector f of sin x evaluated at the gridpoints x g = cos(x); % construct a vector g of cos x evaluated at the gridpoints x h = f .* g % construct a vector h by pointwise multiplication of vectors f and g % h will consist of the numerical values of sin x cos x % plot f,g,h versus x plot(x, f, 'b-') hold on plot(x, g, 'r-') plot(x, h, 'g-') xlabel('x') legend('sin x', 'cos x', 'sin x cos x') </code> ===== Supplemental material ===== [[https://www.youtube.com/watch?v=zXP_pr7np-o | ' (apostrophe), Frank Zappa (the song)]] [[https://www.youtube.com/watch?v=kQDp2425WQ0&list=PL8WvZFiJpAr29HFJkRcE4NvWCeYy3ytte | ' (apostrophe), Frank Zappa (full album) ]]

gibson/teaching/spring-2018/math445/lecture/vectors.1516899824.txt.gz · Last modified: 2018/01/25 09:03 by gibson