CS 99

Summer 2003: Lab 1                                                                        6/24

 

 

1.     Objectives

To become acquainted with the computer lab and MATLAB by playing with and mimicking various commands & copying output into a Word document. First skim, and then carefully read the entire assignment before starting any tasks.

 

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 6.2 (in radians), for instance, we would type

EDU» sin(6.2)

Task 1:
Use MATLAB to compute the expression
π*sin(13log(12))+10cos(10)

 

Solution

>> pi*sin(13*log(12))+10*cos(10)

 

The answer is -5.9537

 

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

        2
x + y + z = 10
        x +3 y - z = -21
        5x - y +2 z = 19


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

 

Solution

EDU>> A = [2 1 1;1 3 -1;5 -1 2]
EDU>> b=[10;-21;19]

EDU>> x = A\b

 

The answer is:

x =

 

   -2.8462

   -0.6154

   16.3077

 

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.     Letters in Your Name

We saw today how to create a variable that is a character array.  This type of variable is also known as a string.

 

Task 4:

Create a string variable called myName and store your name in it.  Then use the Matlab help and lookfor functions to find a function in Matlab that will return the number of letters in myName.  Use this function to get the number of letters in myName.  Copy all the relevant lines in the command window and paste them into your word document.  Label it Task 4.

 

Solution

EDU>> myName = ‘Jane Doe’

EDU>> help length

EDU>> length(myName)

 

7.     Just for fun
A friend once showed me that typing why at the command prompt actually works.  Try 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.

If you haven’t set up your Net-Print account, go to http://www.cit.cornell.edu/net-print/getstarted.html to set up your account.  You will need a Net-Print account to print from the Cornell computer labs.