CS 99

Summer 2002: Lab 1                                                                             06.25

 

 

1.    Objectives

To become acquainted with the computer lab and MATLAB by playing with and mimicking various commands, copying output into a Word document, and printing to a local printer. First skim, and then carefully read the entire assignment before starting any tasks.  The starred questions are optional!

 

2.    Starting MATLAB
Listen carefully to the instructions given at the beginning of the lab session to find out where MATLAB is on the public computers in B7 and how to start it.  Remember everything, since you will need to know it all when you start the homework on your own.

Running MATLAB creates one or more windows on your monitor.  Of these, the Command window is the primary place where you interact with MATLAB. The character string EDU» is the MATLAB prompt in the Student Edition. Do not type it in the examples below.

3.    MATLAB, the calculator
MATLAB can do simple and advanced math just like a scientific calculator. Try typing various mathematical expressions in the command window to see what happens.  The symbols for multiplication, division, addition, and subtraction are *, /, +, and –.  MATLAB can handle square roots, powers, logarithms, and trigonometric operations using its own built-in functions.  The commands for sine, cosine, natural logarithm, and tangent are sin, cos, log, and tan respectively.  To find the sine of 25 (in radians), for instance, we would type

EDU» sin(25)

Task 1:
Use MATLAB to compute the expression
sin(cos(ln25))+100(55/7-1000tan(.23))

ln is the symbol for natural log. Record the answer in a word document called
LabOne.doc.  Do this by highlighting the relevant lines in the MATLAB command window with the mouse. Then click on 'Edit' on the menu bar and choose 'Copy.' Now return to the word document and click on 'Edit' and choose 'Paste'. This will paste the highlighted text from the Command window into your word document.  Label the pasted text in boldface as Task 1.

4.    Systems of equations*
As is only proper for an application called MATrix LABoratory, we can use MATLAB to solve linear algebra problems.  To solve the system of equations

 
x + 2y + 3z = 366
  4x + 5y + 6z = 804
  7x + 8y + 0z = 351

 we would type the following commands:

EDU» A = [1 2 3; 4 5 6; 7 8 0];
EDU» b = [366; 804; 351];
EDU» x = A\b


You should discover after typing the last line that the solution is
x = 25, y = 22, z = 99. 

Task 2:
Study the example to solve the system of equations

 
x + y + z = 16
  x + y - z = -8
  x - y + z = 22


Copy the resulting answer in your word document and label it Task 2.

5.     Getting help in MATLAB
You should become familiar with the help and lookfor commands; they will come in handy later.  If you type help followed by a function name in the Command Window, MATLAB will display help for that function. For instance, type

EDU» help sin

You can even type help help. Unfortunately, to use help correctly you must know the precise name of the function.  The lookfor command differs from the help command by searching for summary information instead of an exact match. 

Task 3:

Compare the results of typing help tangent and lookfor tangent in the Command window. Record the answers for each command in your word document and label it Task 3.

6.    Plotting**
Suppose you want to plot y(x) =x2 for 0 ≤ x ≤ 4.  This can be accomplished in MATLAB with the following commands:

EDU» x= linspace(0, 4, 1000);                 %create a vector x with 1000 values from 0 to 4
EDU» y = x.^2;          %create a vector y whose values are the squares of the values in x
EDU» plot(x,y);                                                                                           %plot y vs. x
EDU» xlabel(‘x’);ylabel(‘y = x^2’)%label the x-axis of the figure ‘x’ and the y-axis, ‘y = x^2’

Task 4:
Find out how to plot multiple graphs in the same figure window using help plot. Use that information, and a careful study of the commands above, to find the number of points of intersection of the graphs of y1(x) = cos(x) and y2(x) = log(x)/log(3p) in the range 0.25 ≤ x ≤ 15. In MATLAB, pi = 3.14159… Hint: what would PLOT(X,Y1,X,Y2) do? Record your answer in LabOne.doc and label it Task 4.

7.     Just for fun
One day in a pique of frustration, I typed in why at the command prompt and was surprised to find that MATLAB accepted it.  See what happens.

 

8.     Submitting Your Work
Type your name (and your partner’s name if you have one), student ID, and the date at the top of LabOne.doc.  Print the document and sign it along with your partner.  Give the signed assignment to the teaching assistant.

 

*The starred questions are optional, but worth bonus points.