联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp2

您当前位置:首页 >> Algorithm 算法作业Algorithm 算法作业

日期:2022-11-27 10:26

GEOG 264: Programming for Environmental Sciences Fall 2022


1

Assignment #4 Population growth simulations with loops

Due date: Friday November 25th at 11:59PM

Assignment 4 is worth 45 marks total. Lab exercises 7 & 8 is worth 10 marks (5 marks each) of

this assignment.

Read the entire assignment before starting it as there are hints at the end!

You will be working with monthly temperature data from a weather station in Quebec City.

Temperature readings are in degrees Celsius and run from 1942 to 2017 (inclusive).

1) (10 marks) More for-loop within for-loop practice. Take the code that I gave you in Lecture

12 that subsets a matrix into a linear vector. a) Use it to convert the tabular

QuebecTempTrunc.csv data into a vector. Notes: you are only using data from 1942-1956 so

only subset those dates into your vector. Also, you are not going to need to subset the 1st

column as these are the years. b) Using the vector of temperature data consisting of

successive months, plot the monthly temperatures for 1942-1956, using type = “l”. Make

the graph as nice as you can. Put axis titles on your x and y axes, include a title. Use color

and line types to make an attractive plot. Make sure the x-axis labels are 1942 through

1956. Describe the plot. What do you see? Include your answer in what you hand in. Include

the code used to make the graph in your R script file.


2) (25 marks) In this part your job is to write a little simulator which estimates Canadian lynx

and snowshoe hare populations over a number of years. You should prompt the user for

the number of years the simulation will run and for the initial number of snowshoe hares

and lynxes at the beginning of the first year. Using the initial values and some rules about

the growth and interactions of these two species, you can estimate the populations for the

period specified by the user.

Rules for your simulator: Each year the number of baby lynxes born is 15% of the adult

population. In the same year, if it is a normal year, 2% of the current lynx population will die

due to natural causes. If it is a starvation year, i.e., there aren’t enough hares for all the

lynxes to eat, 50% of the current lynx population will die. This occurs when the ratio

between the current lynx and current hare population is greater than or equal to 0.20.

Snowshoe hares reproduce more rapidly and fewer die naturally. The number of baby hares

is 75% of the current adult hare population, but natural deaths eliminate only 1%. However,

each year a number of hares are eaten by the lynxes. This number can be calculated as the

current number of lynxes times the current number of hares times 0.025.

GEOG 264: Programming for Environmental Sciences Fall 2022


2

Using these rules, generate equations to calculate the changing populations over the years.

Hint: remember that the number of a species in a given year is the number in the previous

year plus the number born minus the number that die and get eaten (if applicable). Design

your program to print a little chart which shows all the relevant numbers for each year.

(hint: you will have to pad the cat statement with extra blanks to make the columns more or

less line up like below. You will have to fiddle here to get something that looks reasonably

nice.) See the sample run. When one simulation is finished, your program should start again

asking for a number of years and initial populations. It should be able to perform any

number of simulations and stop repeating only when the user enters 0 for the number of

years.

Because the birth and death rates are fractional values and the number of animals is an

integer, you should use the R function round() in your calculation of numbers of births and

deaths.

Play with different birth rates, death rates, initial populations and starvation ratios. Observe

the simulated behavior. With some combinations, you may end up with negative numbers

of animals. What is going on? Fix this problem by adding some extra if statements.

Initial conditions:

Numyears = 20

NumLynxes = 30

NumHares = 200


[1] "Year #Hares #Lynxes babyH babyL deadHare deadLynx #Eaten"

1 200 30 150 4 2 1 150

2 198 33 148 5 2 1 163

3 181 37 136 6 2 18 167

4 148 25 111 4 1 0 92

5 166 29 124 4 2 1 120

6 168 32 126 5 2 1 134

7 158 36 118 5 2 18 142

8 132 23 99 3 1 0 76

9 154 26 116 4 2 1 100

10 168 29 126 4 2 1 122

11 170 32 128 5 2 1 136

12 160 36 120 5 2 18 144

13 134 23 100 3 1 0 77

14 156 26 117 4 2 1 101

15 170 29 128 4 2 1 123

16 173 32 130 5 2 1 138

17 163 36 122 5 2 18 147

GEOG 264: Programming for Environmental Sciences Fall 2022


3

18 136 23 102 3 1 0 78

19 159 26 119 4 2 1 103

20 173 29 130 4 2 1 125


Always remember to use good programming style. Include your code, a run of it with

Numyears = 20, NumLynxes = 30, and NumHares = 200; another run with Numyears = 40,

NumLynxes = 20, and NumHares = 1000, plus another run.


Now this is all very intimidating and you don’t know where to start. But you do know how, if

you think about it. Remember that we discussed that you have to practice breaking up your

coding into smaller pieces that you can write, test and debug?

Plan of attack

Figure out how to get the outer while loop prompting you for whether or not you

want to simulate or not, and also asking you to input Numyears, NumLynxes and

NumHares. Just ignore the simulation and get this part working. Don’t worry that the

loops asks for data that it does not actually use yet.

Now totally ignore the while-loop. Leave it off for now. Now get the inner for loop

that does the simulation up and running correctly. You need to declare and assign

values to a number of variables: years (for loop does this one), NumBabyHares,

NumBabyLynxes, NumDeadHares, NumDeadLynxes, NumHaresEaten. If you read the

text carefully, you know the equations that define all these. For instance,

NumBabyHares <- round(NumHares*0.75). Figure out all these assignments. Once all

your variables are calculated you need to print out the current line of output like

that shown. Just dump this to screen right now with a cat(). Don’t worry that it does

not print out a nice table (you will fix this ugliness next step). Your last step here is

that you have to recalculate NumLynxes and NumHares. NumLynxes <- NumLynxes +

NumBabyLynxes – NumDeadLynxes. What is the calculation for NumHares? (hint:

remember hares also alas get eaten.)

Make that table printing out using cat() as pretty as you can, using empty spaces

inside quotations to make the columns of the table line up with the top labels as

best as you can. This step is fiddly.

Put the simulation using the for-loop inside the while-loop and get the whole thing

working.

There are many parts to this assignment and you should not forget any parts. People have

been losing points unnecessarily by this mistake. Make sure you hand in a run of your code

on test case # 1 with Numyears = 20, NumLynxes = 30, and NumHares = 200; test case # 2

with Numyears = 40, NumLynxes = 20, and NumHares = 1000, plus at least another run.


GEOG 264: Programming for Environmental Sciences Fall 2022


Extra credit: modify your code so that it also makes a graph showing the numbers of lynxes

and hares for each year of the simulation (a line graph looks good). You should produce one

graph for each simulation at the end of the simulation.


Moodle submission instructions:

Submit 1 or 3 files to Moodle in r script (.R) format. You should submit r scripts for this

assignment as well as Labs 7 & 8. Make sure that all answers are included in the script (in

comments).

Remember to include all questions and all parts of each question.


Save the file as : a4_FirstName_LastName_StudentID


相关文章

版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp