User Tools

Site Tools


gibson:teaching:fall-2014:math445:lecture5-diary

Differences

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

Link to this comparison view

gibson:teaching:fall-2014:math445:lecture5-diary [2014/09/16 12:49] (current)
gibson created
Line 1: Line 1:
 +Matlab diary on lecture on array operations and basic plotting
  
 +<code matlab>
 +% Topic 1: vector versus array operations
 +
 +% Matlab uses aspecial "dot syntax"​ for performing elementwise ​
 +% operations on vectors and matrices, instead of the usual 
 +% linear algebra operations. For example, lets create two
 +% 3-d row vectors x and y
 +
 +>> x = [4 5 10];
 +x =
 +     ​4 ​    ​5 ​   10
 +
 +>> y = [2 3  1];
 +y =
 +     ​2 ​    ​3 ​    1
 +
 +% You can't multiply these together x*y with the usual matrix-vector
 +% multiplication --that would require the number of columns of x to
 +% match the number of rows of y, whereas these are both 1 x 3 row vectors
 +
 +>> x*y
 +
 +Error using *
 +Inner matrix dimensions must agree.
 +
 +% So, what if we want to mutliply these componentwise (elementwise)?​
 +% Answer is matlab'​s "dot syntax"​
 +
 +>> x .* y
 +ans =
 +     ​8 ​   15    10
 +
 +
 +% Not all elementwise operations require dots. For example, vector ​
 +% addition works elementwise already. So you can add x and y like
 +% need to use '​.+'​
 +
 +>> x + y
 +ans =
 +     ​6 ​    ​8 ​   11
 +
 +
 +% Now let's make use of elementwise operations to plot functions ​
 +% Plot 3 x^2 - 5x + 2 over x in [-2, 2]
 +
 +>> x = linspace(-2,​ 2, 50); 
 +
 +>> y = 3*x.^2 - 5*x + 2;
 +
 +>> plot(x,​y,'​r.-'​)
 +
 +
 +% Let's look at the construction of that polynomial piece by piece
 +
 +% Make a vector of points x gridpoints evenly spaced between -2 and 2
 +>> x = linspace(-2,​2,​5)
 +x =
 +    -2    -1     ​0 ​    ​1 ​    2
 +% compute x^2 by elementwise exponentiation
 +>> x.^2 
 +ans =
 +     ​4 ​    ​1 ​    ​0 ​    ​1 ​    4
 +
 +% compute 3*x^2 by elementwise exponentiation and scalar mutiplication
 +>> 3*x.^2 ​                                                               ​
 +ans =
 +    12     ​3 ​    ​0 ​    ​3 ​   12
 +
 +% compute 5*x by scalar mutiplication ​                                    
 +>> 5*x
 +ans =
 +   ​-10 ​   -5     ​0 ​    ​5 ​   10
 +
 +% compute 3 x^2 - 5x by combining previous two expressions
 +>> 3*x.^2 - 5*x                                              ​
 +ans =
 +    22     ​8 ​    ​0 ​   -2     2
 +
 +% compute 3 x^2 - 5x + 2 by addign 2 to previous expression
 +% note that matlab, in summing the (3*x.^2 - 5*x) with the scalar 2
 +% automatically converts the 2 to a vector of 2's of the right size!
 +
 +>> 3*x.^2 - 5*x + 2                                                   
 +ans =
 +    24    10     ​2 ​    ​0 ​    4
 +
 +% Note also that most matlab functions can operate on vectors, e.g. sin(x)
 +
 +>> x = linspace(0,​pi,​5)
 +x =
 +         ​0 ​   0.7854 ​   1.5708 ​   2.3562 ​   3.1416
 +
 +>> sin(x)
 +ans =
 +         ​0 ​   0.7071 ​   1.0000 ​   0.7071 ​   0.0000
 +
 +% Topic 2: plotting. We can make a plot of sin(x) as follows
 +>> x = linspace(0,​pi,​100);​
 +
 +>> plot(x,​sin(x),​ '​b-'​) ​   % plot sin x versus x with a solid blue line
 +>> plot(x,​sin(x),​ '​bo-'​) ​  % blue line with circles at data points
 +>> plot(x,​sin(x),​ '​b.-'​) ​  % blue line with dots at data points
 +>> plot(x,​sin(x),​ '​g--'​) ​  % dashed green line
 +>> plot(x,​sin(x),​ '​rs-.'​) ​ % dot-dashed red line with squares
 +
 +% for more on matlab'​s plotting line styles, see 'help plot'
 +>> help plot
 +
 +  Various line types, plot symbols and colors may be obtained with
 +    PLOT(X,Y,S) where S is a character string made from one element
 +    from any or all the following 3 columns:
 + 
 +           ​b ​    ​blue ​         .     ​point ​             -     solid
 +           ​g ​    ​green ​        ​o ​    ​circle ​            : ​    ​dotted
 +           ​r ​    ​red ​          ​x ​    ​x-mark ​            ​-. ​   dashdot ​
 +           ​c ​    ​cyan ​         +     ​plus ​              ​-- ​   dashed ​  
 +           ​m ​    ​magenta ​      ​* ​    ​star ​            ​(none) ​ no line
 +           ​y ​    ​yellow ​       s     ​square
 +           ​k ​    ​black ​        ​d ​    ​diamond
 +           ​w ​    ​white ​        ​v ​    ​triangle (down)
 +                               ​^ ​    ​triangle (up)
 +                               < ​    ​triangle (left)
 +                               > ​    ​triangle (right)
 +                               ​p ​    ​pentagram
 +                               ​h ​    ​hexagram
 +
 +% You should always label the axes of a plot
 +>> xlabel('​x'​)
 +>> ylabel('​y = sin(x)'​)
 +>> title('​an example graph in matlab'​)
 +
 +% How to draw two plots at same time, two ways
 +
 +% first way: list several x,y pairs in the same '​plot'​ command
 +>> plot(x,​sin(x),​ x, x.^2 - 3*x + 4)
 +
 +% can label the two different lines using '​legend'​
 +>> legend('​sin(x)',​ '​x^2-3x+4'​)
 +>> xlabel('​x'​)
 +
 +% second way: using '​hold'​ and a sequence of '​plot'​ commands
 +
 +% clear figure and make first plot
 +>> clf(); ​          
 +>> plot(x,​sin(x),​ '​b-'​)
 +
 +% hold on to that plot, and draw another ontop
 +>> hold on 
 +>> plot(x, x.^2 - 3*x + 4, '​g-'​)
 +>> legend('​sin x', 'x^2 - 3x + 4')
 +
 +% Now adjust the axes with '​axis([xmin xmax ymin ymax])'​
 +>> axis([0 pi 0 5])
 +
 +% Turn on the background grid
 +>> grid on
 +
 +% '​subplot'​ makes many subfigures in one figure window
 +>> clf()
 +>> subplot(2,​2,​1)
 +>> plot(x, x.^2 - 3*x + 4,'​g-'​)
 +
 +>> subplot(2,​2,​2)
 +>> plot(x, sin(x),'​r-'​)
 +
 +>> subplot(2,​2,​3)
 +>> plot(x, cos(x),'​b-'​)
 +
 +
 +% Next topic: log-linear plots
 +
 +% Plotting an exponential function on a linear graph is not very revealing
 +>> clf()
 +>> x = linspace(-5,​5,​100);​
 +>> plot(x, 4.^x)
 +
 +% Logarithmic plots are better for exponential functions
 +% In matlab, you plot logarithms on the y axis using '​semilogy'​
 +>> semilogy(x, 4.^x, '​b.-'​)
 +>> grid on
 +
 +% Matlab has three forms logarithmic plots: semilogy, semilogx, and loglog
 +</​code>​
gibson/teaching/fall-2014/math445/lecture5-diary.txt ยท Last modified: 2014/09/16 12:49 by gibson