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.
You may want to refer to chapter 1 of your text.
(§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 () or on the desktop. It can take a while to start, but eventually you should get a screen something like the one shown below.
(§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).
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
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)
|Logarithm Base 10||log10(10)||1|
|sin (argument in radians)||sin(2)||0.9093|
|sind (argument in degrees)||sind(90)||1|
and some useful built in expressions
|Previous Result||ans||Previous MATLAB
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.
ans = 9
ans = 1
ans = 12
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
ans = 19
ans = 33
(§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
Now you can use this variable in other calculations (and you can also define more variables)
ans = 9
y = 12
ans = 1728
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 | Error: The expression to the left of the equals sign is not a valid target for an assignment.
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 | Error: Unexpected MATLAB expression.
ans = 12
(§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.5000
n = 3
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, 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 () and run it (either by choosing 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.
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. 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 Note: you must also close any dialog boxes associated with Word.(or ). 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).
??? 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:
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).
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: