Beyond the Mouse LAB 2: Fundamentals, Variables
September 5, 7
Instructor: Jeff Freymueller
x7286 Elvey 413B jfreymueller@alaska.eduTA: Shanshan Li
Last Updated: August 27, 2017
Due: Tuesday Sep 12, before class
Lab slides
Introduction
Today we want you to play with variables, relational and logical operators in Matlab. This is a basis
to understand flow control later on. We give you a list of examples to play with and want you
to explain the results Matlab gives you. I don't care which way you'll play around, but I expect the
result to be a Matlab script file (ends in .m
) similar to
my template. Your resulting script file will contain
one line or a block of lines I give below and then one or many disp
statements that
explain the code and the response Matlab gives. After the last disp
call you will
place a pause
statement. This serves two functions: 1) It's easy for me to look through,
2) you have documentation in form of a script (you may want to call it relop_doc.m
).
You can get a more complete example script by clicking here. Note that this example still has some things for you to fill in, but it should help you understand how to structure your script for turning things in. For some parts of this lab you will want to type some commands into the command window, and then copy and paste them into your final script once you understand what they do. You can put your text answers into the script as comments, or you can make up a separate document that lists the commands run and your explanations. Once everything is done, run your script to make sure the whole thing works befoe you turn it in.
Exercise 0: ans
Open Matlab, type help ans
or doc ans
and understand what the variable ans
is.
Exercise 1: Assignments and Comparisons
Open Matlab and type the following lines in the command window. Explain each of Matlab's responses to your input!
test_var == 1
test_var = 1
test_var == 1
test_var == 5
Now, let's try something fancy:
test_var == 'A'
test_var = 65
test_var == 'A'
test_var == 'MATLAB'
What's with that? Letters are stored as numbers using ASCII code.
Now create a numerical vector called test_var
that will answer the question:
>> test_var == 'MATLAB'
with:
ans =
1 1 1 1 1 1
(Hint: Try solving this with arithmetic rather than looking up all the values in a table.) To be clear, you are going to insert the ASCII code numerical valus for the letters that spell 'MATLAB' into the variable.
Turn in written responses to the questions in this section.
Exercise 2: Relational Operators
Open Matlab and type doc relop
or help relop
in the
command window and read up on relational operators in Matlab.
Now we have more input. i
and j
are special, predefined variables in
Matlab; the imaginary unit sqrt(-1)
. Do you think this statement is true?
0 > i^2 && sqrt(-1) > j
Now explain this - what's going on with the alternating results for the xor statement?
0 > i^2
xor(ans,1)
xor(ans,1)
xor(ans,1)
In Matlab the elementwise logical operators &
, |
, and xor
compare elements of arrays and
vectors to each other. Reproduce the following results using two arrays and one logical operator:
ans = 0 1 1 1
ans = 0 0 0 1
ans = 0 1 1 0
What are the answers to the deeper questions of life ... and why?
'2b' | ~'2b'
'MATLAB' == 'GMT'
'MATLAB' == 'GMT '
Turn in written responses to the questions in this section.
Exercise 3: Vectorized Operations
MATLAB is very efficient at vectorizing mathematical operations. What we mean by "vectorize" is that a single command will operate on all the elements of a vector (or matrix) at once, and also that MATLAB makes effective use of multiple processors or multiple cores to do many operations simultaneously, making for very fast code execution. Type the following lines of code in and execute them (no semicolons at the end so that you can see what is happening):
x = linspace(1,10,10)
for i = 1:10
x2(i) = x(i)*x(i)
end
x2fast = x.*x
This shows two ways of computing x². The first executes a loop 10 times, each time
computing the i-th element of the array x2. The second computes all 10 values in
a single statement using the array multiply operator ( help .*
).
Do the same thing but replace the 10 with 100 or 1000. Can you tell the difference now?
What happens if you forget the .
and just use *
? There
is also an array divide operator ( help ./
). Explain why there is
no need for .+
or .-
.
Now predict what will happen if you execute the same code but replace the first
line with
x = linspace(1,10,10)'
Try it. Was your prediction correct? What has changed and what has stayed the same when the input array was transposed? What does that tell you about MATLAB's defaults about array orientation (row vs. column)?
Turn in written responses to the questions in this section.
Exercise 4: Computing and Plotting Mathematical Functions
MATLAB's internal mathematical functions are all vectorized, and in general any function will operated on a single number, a vector, or even a matrix without you having to change anything in the code. See the following example:
x = linspace(-3,3,100);
gaussian = exp(-x.*x)
plot(x, gaussian)
You do need to remember to use .*
when appropriate, but with a little
practice it is easy to make a single line of code compute hundreds of values of even
a complex mathematical function. Now define a variable θ with
values ranging from zero to 2π (that is a pi, in case the font is not clear), and compute the values of sin(2θ) and
cos(2θ), using the trigonometric identities for those two functions.
Look up the identities on the internet if you need to; both of these can be written
in terms of products of sin(θ) and cos(θ). Don't cheat
and use 2*theta
in your code; write the expressions in terms of
sin(theta)
and cos(theta)
. Plot each function. Then
choose three additional functions (your choice) to compute and plot for an appropriate range
of values.
Turn in your code for each of the 5 functions (as electronic files) and the plot, either on paper or in some readable electronic format. After you have created each plot, you can go to the File menu in the plot window and use "Save As ..." to save your plot in one of several formats.
Dr. Jeffrey T. Freymueller
Professor of Geophysics
Geophysical Institute
University of Alaska, Fairbanks
Fairbanks, AK 99775-7320