E5 MATLAB Assignment 1

This assignment is expected to be relatively brief; subsequent assignments will be longer.  After you finish this assignment you should:

The actual assignment to be turned in is described at the end of this document.  This exercise just scratches the surface of what is available in MATLAB - feel free to explore more on your own or ask Professor Cheever or one of the Wizards.

Reading from text

 You may want to refer to chapter 1 of your text.

Start the MATLAB program

(§1.1 in your text - relevant textbook sections will be bold purple text) Log into a computer and start MATLAB (likewise, you can download it onto your personal machine from the data-software server, go to the ITS web page for instructions).  On a Windows computer you should be able to find the program on the start menu (MATLAB) or on the desktop.  It can take a while to start, but eventually you should get a screen something like the one shown below.

Basic Mathematical Operations: Using MATLAB as a calculator

(§1.3)  Put the cursor at the MATLAB prompt (>>) in the center pane of the window.  Type "3+5" (without quotes) and hit the enter key; you should get the answer 8.  This is shown below (in all examples in this document MATLAB commands that you enter will be shown in green and with a Courier font, output from MATLAB will have a gray background).

>> 3+5
ans =
    8

You can use MATLAB just like a calculator, though it also does much more (as you will see throughout the semester). Some useful operations in calculator mode are

Operation Matlab Example Result
Addition 5+3 8
Subtraction 10-3 7
Multiplication 2*3 6
Division 10/2 5
Power 2^3 8

There are also some useful built-in functions (Note the difference between "sin" and "sind" one takes arguments in radians, the other in degrees.  The argument to a function is the value inside the parentheses)

Operation Matlab Example Result
Natural logarithm log(10) 2.3026
Logarithm Base 10 log10(10) 1
Remainder (modulus) mod(8,3) 2
sin (argument in radians) sin(2) 0.9093
sind (argument in degrees) sind(90) 1
cos (radians) cos(2) -0.4161
cos (degrees) cosd(45) 0.7071
tan (radians) tan(2) -2.1850
atan (arctangent) atan(1) 0.7854
exponent (ex) exp(2.3) 9.9742
square root sqrt(4) 2

and some useful built in expressions

Expression Matlab Example Result
Previous Result ans Previous MATLAB
Result
π pi 3.1416
inf Inf
i i √(-1)

With these expressions you can use MATLAB as a calculator (note in the last example below that when "ans" is used, MATLAB uses the value of the previous answer, in this case 12).  Try it out for yourself.

>> 3^2
ans =
     9
>> sin(pi/2)
ans =
     1
>> 4*3
ans =
    12
>> ans*4
ans =
    48

When evaluating mathematical expressions you must consider the order of operations.  MATLAB does any operation within parentheses first, then it does exponents, then multiplication and division (from left to right), and lastly addition and subtraction.  This is described more fully in the text (pages 8 and 9).  An example follows

>> 3*4+7
ans =
    19
>> 3*(4+7)
ans =
    33

Variables

(§1.1)  Using MATLAB as a calculator to operate on numbers doesn't begin to tap its potential.  Practically every time you use MATLAB you will make use of variables.  A variable in MATLAB is simply a way to store the value of a number (or other types of information, as you will see later).  For example, to store the number 3 in a variable "x" we use:

>> x=3
x =
     3

Now you can use this variable in other calculations (and you can also define more variables)

>> 3*x
ans =
     9
>> y=4*x
y =
    12
>> y^x
ans =
        1728

Two Important differences between MATLAB notation and algebraic notation.

1. Whenever you use a variable in MATLAB, it must appear on the left side of the equation (this is different than the use of variables in algebra).  If you put the variable on the right hand side, you get an error:

>> 3=x
??? 3=x
     |
Error: The expression to the left of the equals sign is not a valid target for an assignment.
>> x=3
x =
     3

 

2. In MATLAB multiplication must be explicit.  In other words to multiply 4 by "x" you need to write 4*x (or x*4); you cannot write 4x.

>> 4x
??? 4x
     |
Error: Unexpected MATLAB expression.
>> 4*x
ans =
    12 

Scripts

(§1.4) In addition to using variables to hold numerical values, it is often useful to put several MATLAB statements in a script.  That way it is possible to repeat the calculation without having to type in the values.  As an example, you may want to calculate the area and perimeter of an regular polygon given the length of a side (which you'll put in a variable "lside"). The perimeter and area of regular n-sided polygons are given by:

    Note: the angle 180/n is measured in degrees.

Now you can calculate the perimeter and area of an equilateral triangle with sides of length 2.5.

>> lside=2.5
lside =
     2.5000
>> n=3
n =
     3
>> per=n*lside
per =
     7.5000
>> area = lside^2*n/4/tand(180/n)
area =
    2.7063

In this code you have created two new variables; "per" is the perimeter of the triangle, and "area" is its area.

If you want to repeat the calculation with a different size or number of sides, you would have to reenter the last two equations.  A more effective way to do this is to use a script.   Open a script by using the top menu to go to File→New→Script, or by hitting the "New File" icon () on the menu bar.  An editor window will open up.  I can add text to it as shown (link to file).  Note that the script file has a ".m" extension at the end; the name of the file in the image below is e5M1Script1.m. To download this file, right-click on the link and save it to your desktop.

Note that there are a few new elements here. 

Save the script (File→Save) and run it (either by choosing Debug→Run from the editor window, or by hitting the run icon, ).  MATLAB may tell you that you need to change the folder or add the folder to the path.  Choose "Change Folder."  Note that the output will appear in the command window, so make sure that is visible after you run the program.

The output of the script comes in the main MATLAB command window

Number of sides is
     3

Length of a side is
    2.5000

Area is
    2.7063

and the perimeter is
    7.5000

Now if you want to change the number of sides or the length of the side you can simply change the corresponding line in the script and run it again.

Publishing

Note: this is not covered in your textbook.

The last aspect of MATLAB that you will learn today is called "publishing."  MATLAB can generate a Microsoft Word document (or a web page...) that formats the output of a script for us.  It is these Word documents that you will submit (electronically, to Moodle) to be graded.  You do this by creating "cells" in a MATLAB script.  A cell starts at a line beginning with two percent signs and continues to the next such line.   The lines beginning cells also have particular meaning when you publish.  This is most easily demonstrated with an example script, shown below.  Open the file and save it to your desktop.

Note that every line beginning with "%% " starts a new cell as shown by the horizontal line that  you call this the "cell heading"  and  the cell with the cursor in it has a yellow background.  Note that there is a space after the two percent signs - this space is necessary to indicate that this comment begins a new cell.

Before you can "publish" the file to a Word document, the publishing tool needs to be configured.  From the editor window choose File→Publish Configuration→Edit Publish Configurations.  Change the file type to "doc" and the directory to a convenient place to store the file (perhaps the desktop, or your "H:" drive) as shown below.  If you are on an Apple computer and the "doc" option is not available, choose "pdf" instead.

Close this window and save the file.  From the editor window go to File→Publish (or File→Save File and Publish).  The MATLAB results will now be published to Word.  This may take a minute or two the first time you publish during a MATLAB session.  Important:  If you change your MATLAB script and want to republish, you must first close the Word document  or you will get an error in MATLAB (as shown below).  Note: you must also close any dialog boxes associated with Word.

??? Error using ==> publish at 110
The output file "W:\NatSci\echeeve1\Class\e5\E5M1\e5M1Script2.doc" already exists
and is not writable.

Error in ==> mdbpublish at 55
outputPath = publish(file, options);
 
??? Error using ==> open at 68
NAME must contain a single string.

The output of the script is in the file E5M1Script2.doc.  Download the file and open it with Word.  It will be similar to the image shown below (I have altered the displayed document so that it fits on one page).  Note that the formatting will be somewhat different because I used an older version of MATLAB than is currently installed in the lab (e.g., in the current version the text is all black and there is a gray box around the code instead of displaying it in boldface).

The effect of the MATLAB code on the Word document is shown diagrammatically below, followed by explicatory text.

Starting from the top and moving to the bottom on the MATLAB document on the left, note the following:

  1. magenta:  The MATLAB code (on the left) starts with a cell heading (i.e., a line beginning with "%% "; don't forget the space after the percent signs) → this gives you the title of the Word document (on the right).
  2. yellow:  A comment immediately following a cell heading → this generates plain text in the Word document.
  3. cyan: A second cell heading →
  4. blue: MATLAB code →
  5. red: A cell  with a blank cell heading, but with comments immediately following →
  6. green: A new section with text in the cell heading immediately followed by comments →

Not shown in this document is that if there are comments that do not immediately follow a cell heading, they appear simply as MATAB code (i.e., they are bold and indented in the Word document; there is an example in the following section).

To turn in

(via moodle)

The assignment this week is brief: 

 

In the displayed document the MATLAB code that you need to write is replaced by x's, as is the final result; you will need to figure out what to put in place of the x's.  When you are done, submit the Word document and the MATLAB script on moodle (you do not need to turn in the MATLAB file).  Make sure you have included your name in the text immediately following the document title.  It may be helpful to recall that the volume of a cone is given by:

Also recall that MATLAB has the value of π predefined in the variable "pi".   If you create a new script file, don't forget to configure the publication tool  for that file because MATLAB publishes to html by default (i.e., a web page) and you need to submit a Word document.

Grading is as follows: