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.
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- sin(0) %(then press enter)
- sin(180) %(then press enter)
- sin(3.14159) %(then press enter)
Pop quiz.- sin(pi)
- sin(Pi)
The quick explanation of what you see is:- 1:100
- sin(1:100)
- plot(sin(1:100))
x = 1:100 y = sin(x) plot(y)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:
x = 1:100 % and... x = 1:100;
xDense = 1:.1:100; % this make a list [1 1.1 1.2 1.3 1.4 ... 100] yDense = sin(xDense); plot(yDense);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:- xSparse = 1:13:100; % this make a list [1 14 27 40 ... ~100]
- ySparse = sin(xSparse);
- plot(ySparse);
clf; % this clears the figure. plot(y); % plot the original y values hold on; % tell matlab not to clear the figure plot(ySparse,'r'); % plot the ySparse values, in red.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:
help plotYou 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.- seriously, try to figure it out on your own.
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:
clf; % this clears the figure. plot(x,y); % plot the original y values hold on; % tell matlab not to clear the figure plot(xSparse,ySparse,'r'); % plot the ySparse values, in red. plot(xDense,yDense,'g'); % plot the ySparse values, in green.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.
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.