http://www.cs.wustl.edu/~pless/200/matlabIntro.html
CSE 200, Day 1, Matlab Intro
Hello and welcome to CSE 200. This class, Engineering And Scientific Computing is intended to teach fundamental tools and methods for the kinds of computations and simulations that you may need in your career as an engineer. Today's lab is intended to introduce you to our computational tool for this class, MATLAB. There are several excellent tutorials on matlab available online. One of the joys of the web is that one can link to, (instead of replicating), tutorials from other places. In addition to your textbook(s), here are some of the best tutorials that I've come across in my last 10 years as a daily matlab user:
  1. http://www.cyclismo.org/tutorial/matlab/
  2. http://amath.colorado.edu/computing/Matlab/Tutorial/
  3. http://www.math.utah.edu/lab/ms/matlab/matlab.html#tutorial
  4. http://www.engin.umich.edu/group/ctm/basic/basic.html
If you have never used matlab before, it would be very valuable for you to spend the hour to go through one of these tutorials when you finish today's worksheet. I (and the TA's) would be very happy to help as you get stuck.

Today, my goal is to motivate why we use MATLAB in this class (and why, in my biased opinion, it is probably the only programing language you need to know) by showing how much you can do with just a few commands. This is a 6 part demonstration.

  1. Getting started. Run matlab. It often takes 1-2 minutes to get started. There may be a window with a graph on it that shows on the screen then disappears. When it finally starts up, you will be in the "matlab workspace" which has 3 windows, two small ones on the left, and one filling the right side. On the right side window, the command window, type 2+2, then press (enter). Don't go on to the next step until you see a 4. You can seek advice from/ help your neighbor to get to this point.
  2. First exercise. A visualization of the Nyquist-Shannon sampling theorem (wiki). This theorem says that you can't see very high frequency signals if you don't have samples that are close together. Our plan here is to define a function: y = sin(x), and then look at that function with example "x" values with different spacings. Try typing the following into the command window: Note: the "%" character is a comment (like // in java). Matlab won't pay attention to any part of a line after a %. Tthe number Pi comes up all the time in math, wouldn't it be nice if there was a way to use "Pi" without typing all the digits? try typing Pop quiz.
    • (Circle T or F) MATLAB is case sensitive. That is, does matlab care about the capitalization of what you type?
    • (Circle the correct answer) MATLAB interprets arguments to the "sin" function as degrees or radians?
  3. It is difficult to understand a function by looking at one value at a time, so we want to look at the function for many values of x. If you've done programming in the past, you might be thinking something like, "how can I do a for loop in matlab?" This is not the Tao of Matlab. Instead, let's specify the values of x we are interested in as a vector. In the command window, try each of the following: The quick explanation of what you see is: Thank goodness that we have programming languages, because writing out these instructions in english gets unwieldy.
  4. This is a useful time to introduce variables. They help make what you are doing more understandable, even if you don't strictly need them. The better way to write the above lines would be: This assigns the vector [1 2 3 ... 100] to the variable x, Then assigns sin(x) to the variable y, then plots that variable. Matlab has the pleasant feature that when you assign something to a variable, it also prints the value (or vector, or matrix) to the command window. if you add a semi-colon to the end of the line, it does not print this out. Notice the difference between:
  5. Now, suppose we want to sample our function more densely. we can use: and then try the "zoom in" tool (the magnifying glass with a "+", to zoom in to the figure and see how smooth it is. If we want to sample our function less densely. we can use: Now, the best part of matlab is that it makes it easy to plot things. To compare y, yDense, and ySparse, we can use the following commands: With luck, you see a blue sin curve, and the start of a different sin curve in red. Is that what you expected? We were trying to plot the same function, just sampled at different frequencies... the range of our function should have been the same. This is a good chance to introduce the "help" function. type: You may have to scroll up to see the beginning of this help function. Take a few minutes to see if you can figure out what happened. See if you can fix the problem... You can use "help command" for any command in matlab. So far the only two matlab commands you've seen are "sin" and "plot". The tutorials at the top of this page introduce you to a large number of useful commands.

    ok, ok, so the problem is we asked matlab to plot a list of y-values, without telling if what the x-values should be. Matlab tries to be helpful... if you only give it a list of y values, it assumes the x-values are 1,2,3,4 etc... So, to fix this, we can try the following:

    What you are seeing here is our function, y = sin(x). When sampled with a spacing of 0.1 (shown in green), it gives a relatively smooth sin curve. When sampled with a spacing of 1 (shown in blue), it gives an approximation to the sin curve. When sampled with a spacing of 13, it gives a completely different frequency. This is because sampling this function every 13 points is "below the Nyquist limit", it cannot capture the frequency of the function.
    • Pop Assignment. What is the Nyquist limit for this function? That is, try a collection of different definitions of xSparse, then compute ySparse = sin(xSparse), and plot them. At what spacing does the sparse sampling start to miss the original sin curve?



  6. So plotting curves in matlab is part of what matlab is good at. But i think more motivation is in order. You can copy the below lines into matlab chunk by chunk to see some of the power of what it can simply do.
    
    clf;                       % this clears the figure.
    
                               % grab current wash U webcam image.
    cameraURL = 'http://166.211.74.92/jpg/image.jpg'
    % cameraURL = 'http://210.236.173.198/axis-cgi/jpg/image.cgi'
    im = imread(cameraURL);  
    image(im);                 % display the image
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % so we can read and display an image.  not so exciting.  try this:
    
    for n = 1:30               % loop 30 times.
                               % grab and display image
      im = imread(cameraURL);
      image(im);               
    
      drawnow;                 % force matlab to draw the figure right now.
      pause(0.5);              % pause .5 seconds
    end                        % end of loop
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % so we can read and display a live movie.  a little exciting?  try this:
    
    
    colormap gray;                   % color is over-rated.  lets show only grays.
    imA = imread(cameraURL);  
    imA = double(rgb2gray(imA));     % make it a double so we can do math with it.
    for n = 1:300                    % loop 30 times.
                                     % grab new image
      imB = imread(cameraURL);  
      imB = double(rgb2gray(imB));   % make it a double so we can do math with it.
    
      imDiff = imB-imA;              % make a difference image
    
      subplot(1,2,1);                % Seperate the figure into 2 parts,
                                     % and get ready to plot in the first one.
      imagesc(imA);                  % show the image
    
    
      % show where things have changed.
      subplot(1,2,2);                % Seperate the figure into 2 parts,
                                     % and get ready to plot in the second one.
      imagesc(imDiff,[-80 80]);      % show the difference image 
    
      drawnow;                       % force matlab to draw the figure right now.
      pause(0.5);                    % pause .5 seconds
    
      imA = imB;                     % keep the newest image around for the next iteration.
    end                             %end the foor loop
    
    
    A little bit of commentary on this function. This was intended as a brief introduction to what matlab can do with just a few commands. However, this short program illustrates a few of the things we've talked about. First, why the semi-colons? Languages like C and Java require semi-colons. In matlab they are optional, but you almost always use them when you are programming in matlab; usually you want to assign data values to a variable, rather then printing out numbers on a screen. For instance, type the last line above (imA = imB) without the semi-colon (and press enter). "blechh!", especially when dealing with images, we really don't want to have to have all our data printed on the screen.