User Tools

Site Tools


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

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
gibson:teaching:spring-2018:math445:lecture:vectors [2018/01/25 08:43]
gibson [Constructing vectors with linspace :]
gibson:teaching:spring-2018:math445:lecture:vectors [2018/01/26 06:51]
gibson [Constructing vectors with linspace]
Line 153: Line 153:
 </​code>​ </​code>​
  
-===== Constructing vectors with linspace ​=====+===== 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: 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:
Line 169: Line 169:
 </​code>​ </​code>​
  
-Note 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  +Note the use of the semicolon to supress output. Who wants to read all 100 elements of ''​f''​ and ''​x''​?.
-the variables ''​x''​ and ''​f''​ are both size ''​1 x 100'',​ i.e. row vectors of length 100. Note also  +
-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 ===== ===== Supplemental material =====
  
gibson/teaching/spring-2018/math445/lecture/vectors.txt · Last modified: 2018/01/26 06:53 by gibson