DOC PREVIEW
Duke CPS 111 - Homework 2

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CPS111- Homework 2Due on January 22, 2008Questions may continue on the back. Please write clearly. What I cannot read, I will not grade. Typedhomework is preferable. A good compromise is to type the words and write the math by hand.1. This exercise guides you through — and elaborates on — problem 2 in Section 1.2 of the textbook. Studythis section carefully first. The file population.txt, available on the class homework web page, contains thedata for this problem, so you don’t have to re-type them. If you are familiar with Matlab, you may want to skip toquestion (a) below. Matlab practicalities are set in a separate font throughout this assignment, and do not containhomework questions. So if you skip them, you won’t miss questions.Go to a computer that has Matlab installed. For instance, the Teer Engineering Library cluster has Matlabrunning on Linux. Other clusters have Matlab as well.See http://www.oit.duke.edu/comp-print/labs/locations/index.html for what is available andwhere. Download the file population.txt into a directory of your choice. Start Matlab (double-click on theicon under Windows or Mac, type matlab under Linux or Unix). It is not recommended to run Matlab remotely,especially when you are plotting graphics.See http://www.cs.duke.edu/courses/spring08/cps111/readings.htm for more information onrunning Matlab remotely. Use the user interface, or the command cd, to change Matlab’s current directory tothe directory where you put population.txt. To find out how cd works, type help cd, or start the MATLABHelp browser (through the Help menu, or by typing helpbrowser to the Matlab prompt).Read the data file by typing the following to the Matlab prompt:load population.txtand press the enter or return key. Nothing seems to happen. However, if you now type whos to the prompt,you will see a variable population listed, of size 22 x 2. This is a matrix with 22 rows and 2 columns. Tosee its contents, just type its name, population, to the prompt (as usual, press return or enter).(a) The first column of population has all the years. To make this obvious, create a new variable year thatonly contains this column as follows:year = population(:, 1);The notation population(3, 2) would denote the matrix entry in row 3, column 2, that is the entry thatcontains the number 7240000 (try typing population(3, 2), without a semicolon). The colon denotesall rows (rather than just row 3). The semicolon at the end of the command above will prevent printing outthe contents of the resulting variable year. If you want to see the contents, typeyearfollowed by enter/return.Now overwrite population with its second column:population = population(:, 2);Plot the data with the commandCPS111 — Duke — January 14, 2008plot(year, population, ’.’, ’MarkerSize’, 16)This will create a figure, and plot a dot for each of the data points (this is the meaning of the ’.’ argument).More options for the plot command can be seen by typinghelp plotor through the help browser accessible via the Help menu. Label the axes (xlabel and ylabel com-mands) and give the plot a title (title command). You can make a printable file with this plot through theprint command (try help print) or the File->Print menu entry of the figure. Hand in your plot.(b) There is a slight but noticeable kink (a small anomaly) in the plot. To emphasize that, do the following:figureplot(year(1:(end-1)), population(2:end) - population(1:(end-1)))(no need to hand in this plot).The first instruction above makes a brand new figure. This becomes figure number 2 (unless you createdmore figures than what this assignment has required so far), and becomes the current active figure, theone Matlab draws into. To go back to figure 1, you would type figure(1).The second line above contains typical Matlab syntax. The keyword end denotes the last element alonga dimension. For instance, the commanda = [11 12 13; 21 22 23]creates a 2 × 3 matrix that looks as follows:a =11 12 1321 22 23and the commands a(1, end), a(end, 2), a(end, end) return 13, 22, and 23, respectively. Trythis (no need to hand in anything here). Since both year and population are vectors, there is onlyone dimension. Typing size(year) shows that year has 22 entries, and more specifically 22 rows and1 column. So this is a column vector. Incidentally, the notation year’ would transpose the vector andproduce a row vector, that is, a matrix with one row (and 22 columns). Try this, too. So end, when usedin parentheses after year, is equivalent to 22. This is useful, because we can work with a vector withoutknowing how long it is.So year(end-1) would denote the 21-st entry of year, that is, 1990. The notation year(1:(end-1)),on the other hand, is a new vector of all the 21 entries of year from the first to the next-to-last one,inclusive. This new vector is created on the fly, by copying values from year, and then passed to plot.The contents of year are not changed.Similarly, population(2:end) is a vector of all population values from the second to the last, inclusive,andpopulation(2:end) - population(1:(end-1))is a vector difference. The result is a vector that has 21 entries, each equal to the difference between thecorresponding entries of population(2:end) and population(1:(end-1)). For instance, ifa = [1 3 4 2];thena(2:end) - a(1:(end-1))CPS111 — Duke — January 14, 2008would yield the vector[3, 4, 2] − [1, 3, 4] = [(3 − 1), (4 − 3), (2 − 4)] = [2 , 1, −2] .So the plot you just made shows the differences in population between each decade and the previous one(plotted in correspondence to the previous decade), that is, the difference between births and deaths in eachdecade. You should notice a particularly deep dip followed by a peak. Can you explain these anomalies?(c) Now read the question in the book (Section 1.2, problem 2). By eyeballing your plot, guess an appropri-ate algebraic form for the dynamical system. Specifically, let n be an index into your arrays year andpopulation you created earlier. The value n = 1 corresponds to the first entry, so year(1) = 1790and population(1) = 3,929,000. Note that n is not the year. Write an equation that expressespopulation(n+1) as a function of population(n). This equation will have one parameter. Leavethis specified as a letter for now, say, r. Also, you may want to abbreviate population(n) to pn.(d) What do you know about r even before determining a precise numerical value? Why?(e) Pretend for a moment that the


View Full Document

Duke CPS 111 - Homework 2

Download Homework 2
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Homework 2 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Homework 2 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?