GOALS:
1. As with Lab 2, follow the instructions for accessing meteorology computers to log into your Meteorology account. If you are sharing a computer, the second student should open a second window, as per the instructions, and then login as her/himself, entering in the second window,
ssh LOGNAME@0
where the student's login name should be substituted for LOGNAME. Then take turns completing the rest of the lab.
2. Start matlab:
matlab
If you are logged into Project Vincent, but not on the meteorology computers,
then you have to take some extra steps to start Matlab. Enter
add matlab
add math
matlab
Do these "adds" ONLY if you are logged into Project Vincent using
non-meteorology computers!
3. A window with a fancy 3-d graph may appear and then disappear. Then you may see a symbol like an upside-down "L" appear at your mouse pointer on the screen. Click your mouse button, and a window with 3 panels will open.
You may need to move the window to center it. This can be done by holding down the mouse button when the mouse pointer is on the upper bar of the window and dragging the window to where you want it.
The main Matlab panel is on the right side of the window. When you
see the Matlab prompt
>>
in it, Matlab is ready for your commands. First make sure that Matlab
is working in your home directory (where
presumably all your Matlab files are located):
>>cd ~
You can make the window bigger by moving the mouse pointer into a corner. When the pointer changes into an arrow pointing into a corner, hold down the left button and drag the window bigger.
The other two panels provide access to some tools (upper panel) and a record of your previous commands (lower panel), including commands from previous Matlab sessions, which you can re-enter by dragging them into the work panel on the right.
4.Matlab gives lots of help. Try
>>help
Note the 'help' instruction in the last line of the list that appears.
One choice
on the list is " - Operators and special characters." Let's look at
these. Enter
>>help ops
Note that addition and subtraction are performed using their usual
symbol. Multiplcation and division are somewhat more complicated, because
Matlab can work with arrays (vectors) and matrices.
Matlab can also compute trigonometric and exponential functions:
>>help elfun
5.First do some simple computations. Enter the following. In
each case, you should get an answer when you hit return.
>>1+1
>>5.25*4
>>17/11
>>8472.35-2281.9675
6.You can assign values to variables. Thus,
>>a=3
>>b=4
>>c=sqrt(a^2+b^2)
If you did not notice it on the "ops" help, the carat (^) is used to signify an exponent (i.e., raise a number to some power).
7.Matlab is a very powerful tool. Part of its power comes from
being able to write your own functions to compute. Matlab language
constructs contribute to this power. Entering
>>help lang
will give you information on them.
We will write a function of our own that we will save in a file. We will save the file in such a way that Matlab can invoke the function.
Matlab function files are ascii files, so you can edit them using emacs. They must be in the same directory from which you start Matlab. They must also end with the suffix ".m", e.g. test.m Because of this naming convention, Matlab function files are called M files.
8.Let's create a function file called pressure.m
Open an emacs window. Do this by either opening a new Vincent window
or by entering
>>quit
in Matlab. In either case, once you have a Vincent prompt, enter
emacs pressure.m
9.In lecture 4, we derived from the hydrostatic relation an equation for the change in atmospheric pressure as a function of a change in the geopotential height Z for an isothermal atmosphere. Let's assume that we are looking at changes from the level Z=0, where p = p(0) = 1000 mb. Our equation is then
p(Z) = p(0)exp{-Z/H} (3.9.1)Since geopotential height Z is approximately the geometric height (distance from sea-level) in the troposphere, then Z = z, and (3.9.1) tells us how p changes with height.
We want to use this formula in our function file. In the emacs window
for pressure.m, enter these lines
function [p]=pressure(Z,T) % define some constants Rd=287; g0=9.8; p0=1000; % compute scale height H=Rd*T/g0; % compute pressure at geopotential height Z p=p0*exp(-Z/H);Note that "%" identifies comment lines. These are not executed. Note especially that there is a semi-colon at the end of each executed line. This tells Matlab NOT to echo these lines to the screen when it is executing the function.
The first line of the file tells Matlab that we are defining a function called pressure that has two arguments, Z & T, which are used to compute the value returned in the variable p.
Be careful! Matlab variables and functions are case-sensitive. Make sure you are consistent in how you use upper and lower case letters.
10.Exit emacs
C-x C-c
and go back into Matlab. If you quit it in step 8, then
start it again, like you did in step 3.
11. Back in Matlab, we need to make sure it looks in the
correct directory for the file you created. At the top of your
Matlab window is a line that says, "Current Directory" with a box to
its right. That box needs to have the name of the directory with your
file pressure.m . Unless you have done more than I asked with your
directories, the file should be in /mthome/USERNAME, where USERNAME is
your login name. To change to the correct directory, enter
>>cd ~
Note that there is a space between "cd" and "~".
12.Enter
>>p=pressure(0,273)
>>p=pressure(0,300)
The second number of course is the temperature we assign to the
atmosphere in pressure.m. In each case, the answer should come back p
= 1000. Why? (You will have to give me your written answer - see
below.)
13.Now assign an array (vector) of values to Z:
>>Z = [0,2000,4000,6000,8000,10000,12000]
Because our formula is in MKS units, Z is in meters. Thus we have
assigned a vector of Z values ranging from 0 to 12 km.
Then enter
>>p200=pressure(Z,200)
After entering this command, you should see
p200 = 1.0e+03 * 1.0000 0.7107 0.5051 0.3590 0.2552 0.1814 0.1289which gives pressure as a function of increasing Z. Note that all values should be multiplied by 1.0e+03 (that is, 1000), as indicated by the second line of the three shown here. Each pressure value corresponds to one of the input Z values. Thus, the pressure at 8 km (8000 meters) is 255.2 mb (255.2 hPa in MKS units).
14. Changing the temperature of the atmosphere changes the pressure
vs. height relationship.
(a) Use different input temperatures in your pressure function, e.g.,
>>p250=pressure(Z,250)
>>p300=pressure(Z,300)
Find the range of temperatures for which pressure at 8 km falls in the range
300-350 mb. What is this range? Does higher or lower temperature
give higher or lower pressure?
(b)Find the approximate temperature for which the 8-km pressure is at
the middle of this range, 325 mb. Is it closer to the upper or lower
end of the temperature range you found in (a)?
15. Make a plot of one of your (p,Z) relations:
>>plot(p200,Z)
Now plot two curves, one solid, the other dashed, where "p:
>>plot(p200,Z,'y-',p300,Z,'c--')
What you have done here is give the plot software two pairs of vectors
that associate a "p" with a "Z". The items in single quote, such as
'y-', assign a color and line pattern. For more on plotting,
enter
>>help plot
Looking at your plots and thinking about what you had to do to answer
questions in 14(a), why does pressure at a given level change the way
it does when temperature changes? Think about the hydrostatic
relation and what it implies for how rapidly pressure changes with
height. In particular, use a version of the hydrostatic relationship
that uses the ideal gas law to eliminate density. This version will
give you a relationship between dp/dz and temperature.
Why is the "middle-pressure"
temperature in 14(b) closer to one end of the range in 14(a) than the
other? (This is a subtle one.)
EXTRA CREDIT: Using "help" and/or experimenting with the menus in your plot window, learn how to put on your plot (1) axis labels, (2) a title, and (3) text labels for each curve. Then add any or all of these to the plot, using Matlab commands.
Finally, whether or not you do the extra credit, save the
current plot by "printing" it to a file called
pressure.ps:
>>print -dps pressure.ps
This will create a file called pressure.ps in the directory you are
working in. (The file is in so-called postscript format.)
Then exit Matlab using the exit command in the pull-down "file" menu in the Matlab window.
16. You will need to answer the questions under steps 12., 14. and 15. above. You will also need to print and hand in copies of your M file and your plot. Following the instructions here will cause the files to print at Durham and then appear in a mailbox (bin) there. I also give an alternative method in case the primary one does not work for you.
Primary method To do this, first determine a bin number where
you want the file to go.
Durham bin numbers are between 200 - 475. You choose a number, but it
is best to avoid commonly used simple numbers like 200, 300, 400,
222,333,444. To print your files, enter (after exiting Matlab):
lpr -B ### pressure.m
lpr -B ### pressure.ps
where ### is the bin number you choose and the option "-B" uses an uppercase letter. The printouts will go that bin in the large computer lab on the first floor of Durham.
Alternative method This entails copying the file to your
Vincent storage space, then logging in to a Vincent machine and
printing from there:
cp pressure.m /afshome/LOGNAME/
cp pressure.ps /afshome/LOGNAME/
telnet isua.iastate.edu
lpr -B ### pressure.m
lpr -B ### pressure.ps
logout
In these lines, LOGNAME = your Vincent login name. Note also that in
both "cp" (copy) commands, there is a space before the "/afshome..."
Acknowledgement. Thanks go to Dr. John Iselin (ISU Ph.D. 1999) for providing much of the introductory Matlab material used here.
Return to Meteorology 301 Lab List
Return to Meteorology 301 Homepage