Professional Documents
Culture Documents
ENGR 112
Homework goals:
What is a script file? What is the difference between a script file and the command
window? How do you create variables? How can you examine the values of variables?
How do you create equations, pass them variables, and print out the results? How do you
display the values of variables?
Homework expectations/How to start:
1) Homework assignments typically begin with tutorial-style questions and progress
toward open-ended questions.
a. Tutorial-style questions have specific requirements for what to turn in.
b. All open-ended questions require (at minimum) a script file and output.
c. Skim through the entire assignment, and then start at the beginning.
2) Download the StandardHWformat template from Blackboard. Youll be pasting
your results into this document and turning it in. Take a look at the standard
homework format example on Blackboard for an example script handin.
3) At the top of the script/function, write an English description of what the script or
function does as a comment (lines that start with %). Name the script by
something youll remember, like prob1trigFunc.m.
4) Write pseudo code (using comments) to outline the steps you think you need to
solve the problem
5) Underneath the pseudo code write the MATLAB statement(s) that perform that
step. If you declare a variable, make sure you declare its units in a comment.
6) When printing out the results, make sure to use fully formed sentences, the correct
formatting for numbers (eg, decimal places), and to include the units.
7) If something isnt clear, ask!
A note about labs: The tutorial-style questions are designed to introduce basic concepts
youll need later in the homework in a try it and see what happens way. When you get
stuck, ask for help, either from the TAs or from your neighbors. Each of these problems
starts with what to turn in highlighted in bold; this is to tell you what the goal of that
problem is. Following the section in bold is a set of step-by-step instructions that show
you how to do the problem.
Coding style:
1) Use variables for constants in equations (where it makes sense). Eg, in
problem 2, declare R = 2.5 as a variable, dont just write the equation with 2.5
in it.
General Grading Guidelines:
30% Comments/pseudo code: Did you declare units? Did you write out an
English description of the problem? Do you have comments outlining the steps
the script takes?
20% Output: Did you print out in the required format/correct units/correct
number of decimal places? Did you use a fully formed sentence?
50% Functionality: Does your script compute the correct value(s)? Did you
correctly convert the units where needed?
Week 1 homework
ENGR 112
1. Cut and paste each command and what MATLAB prints out under
Week 1 homework
ENGR 112
BTW, all of the window panes can be dragged out of the main window to be separate
windows, then put back in again, rearranged and re-sized.
Go to the command window (the big one in the middle) and create a variable ts by
typing: ts = linspace(0,2 * pi, 20)
o Hint: type help linspace into matlab or search for matlab linspace in your
favorite browser. What do each of the arguments (0, 2pi, 20) do? What
does linspace do/what is its output?
The variable ts will show up in the workspace window (upper right). Double click on
it to bring up a spreadsheet of the values in ts in a separate variable window. Other fun
things you can do from the variable window: Try selecting a set of values then chose
the Plots tab, and pick a plot type.
Try changing the 0, the 2*pi, and the 20. What effect do they have? What happens if
you add a semi-colon to the end of the line? ts = linspace(0, 2*pi, 20);
Try typing
o disp(ts)
o fprintf(%0.2f\n, ts)
o ts
o ts(1)
o ts(1:end)
o ts(1:3)
o ts(:)
o length(ts)
o size(ts).
What do these commands do? If you cant figure it out, ask a TA for help
Questions you should be able to answer:
o What does the linspace command do? (Google matlab linspace) What are
the three parameters? What do they mean? What effect do they have?
o What does ts(1) do?
o What does the semi-colon do? Try typing a command both with and
without the semi-colon. (The key word here is suppress)
2. Follow the instructions below, then paste the last plot into the Word
document under Plot Window Output (no need to paste anything else).
(Comments and output formatting are not required for this problem.)
Option 1: Go to the figure window. Select edit->copy figure.
Paste directly into the homework document.
Option 2: Go to the figure window. Select file->save as. Change
the type to png (middle of the screen). The file will be saved in
documents/matlab.
Option 3: Do a screen grab (eg google screen grab windows or
screen grab mac)
Week 1 homework
ENGR 112
Now create two new variables in the command window by typing: xs = cos(ts); and
ys = sin(ts);
Plot them one at a time (should make a new figure window):
o plot(ts, xs);
o plot(ts,ys);
Plot xs and ys together on the same plot:
o plot(ts, xs, r);
o hold on;
o plot(ts, ys, b);
Now plot xs versus ys:
o clf;
o plot(xs,ys);
Questions you should be able to answer:
o What does clf do? What does it stand for?
o What does hold on do?
o Whats the difference between plotting ts versus xs and xs versus ys?
3. For the following, you will create a script (called myScript.m). When
you are done, cut and paste the script under Problem 3 Script File in the
homework document. Paste the plot under Plot Window Output. Note:
From now on, use a script for all problems. (Comments and output
formatting are not required for this problem.)
Type clear. What happened to the workspace window? What happens if you type
disp(ts) now?
Type clc. What happened?
Instead of re-typing the commands, get ts, xs, and ys back by double-clicking on the
commands in the command history window (on the bottom right).
Now lets save those commands so we can run them again, and again, and Click on
new (top left plus sign on window). This brings up a new window with an editor
(the script and function editor). Start the script with the clear and clc commands.
Then cut and paste your ts, xs, and ys commands and one of the plot commands into
the editor and save the file as myScript.m.
o Note: To be clean, you should start your script with a clear and a clc
(and a clf if youre plotting).
Go to the command window. Type clear to clear the variables. Now type myScript.
You should get your variables back it ran the script.
Questions you should be able to answer:
o What does clear do?
o What does clc do?
o Whats the difference between writing MATLAB commands in the
command window versus the editor (ie, myScript.m?)
o What happens when you type myScript into the command line? What
does MATLAB do?
Week 1 homework
ENGR 112
Go to the editor and hit Run (big green triangle on top). This does the same thing as
typing myScript.
The debugger: Well go over this more later, but for now, try clicking on the next to
a numbered line in the editor, say the one for computing xs. You should get a red dot
on that line.
Now run the script. MATLB will stop at that line. In the command window the
prompt should have changed from >> to a K>>.
Click on Step, then hover your cursor over the variable xs. You should see the value
of xs. You can also print it out on the command line as usual.
Click Continue to let the script finish. Your cursor in the command window should
change back to >>
This will become useful later in the course.
Type why into the command line. Do it again, if you need a bit of comic relief
Questions you should be able to answer:
o What is the difference between typing myScript into the command
window and clicking the run button on the editor?
o Why would you want to be able to stop in the middle of a script execution
using the debugger?
Week 1 homework
ENGR 112
4. For the following, create a script and copy and paste it under Problem
4 Script File. Make sure to output 3 different values for y and z, and
paste all outputs under Problem 4 Command Window Output.
(Comments and output formatting are not required for this problem.)
Write a script to calculate the following equations using the following values of x: 1.1,
3.5, and 7.2. (The easiest way to do this with your current tools is to declare x as a
variable, write the equations for y and z, and then duplicate this code two more times
with x set to different values.) You can show the output of y and z by leaving the
equations unsuppressed (leave out the ; symbol), or by using disp or fprintf, your choice.
! =
2! !
4! + 3
! =2 !+3 +
10! + 2
3! !
Week 1 homework
ENGR 112
7. The distance to the horizon increases as you climb a mountain or hill. The expression
d = (2rh + h2)1/2
where d = distance to the horizon, r = radius of the earth, and h = height of the hill is used
to calculate the distance. If the diameter of the earth is 7926.3 miles, write a program to
find the distance d to the horizon from the top of a mountain that is 8200 feet high. Your
answer should be in miles. Format your output to one decimal place.
10. A point on a sphere can be characterized by two angles, the polar angle and the
azimuthal angle . These angles are related to the longitude and latitude as follows:
= 90 - latitude (if North)
= 90 + latitude (if South)
If the angular separation between two points 1 and 2 on the sphere is 1-2, then the
equation for the cosine of this angle is given by
cos(1-2) = cos(1) cos(2) + sin(1) sin(2)cos(2-1)
Once the angle is determined, the surface distance between the two points d is given by:
d = 1-2R
where the angle 1-2 is given in radians and R is the radius of the earth (R = 3958.9
miles).
Week 1 homework
ENGR 112
Find the surface separation distance between Corvallis Oregon and Moscow Russia. Your
answer should be in miles. Format your output to one decimal place.
Use the following coordinates:
Corvallis: 44 33' N / 123 15' W
Moscow: 55 45' N / 37 36' E