-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathps1.Rmd
More file actions
85 lines (49 loc) · 5.87 KB
/
Copy pathps1.Rmd
File metadata and controls
85 lines (49 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
---
title: "Problem Set 1, thinking like a computer"
output: html_document
date: "2025-12-20"
---
```{r setup, include = FALSE, echo = FALSE}
if (!require("pacman")) install.packages("pacman") #pacman allows you to use the p_load function
#the p_load function checks is a library is installed, if not it installs it, then it attaches the
#called library
p_load(fontawesome)
```
### Instructions and background
In this problem set you will complete and submit this Rmarkdown file.
Rmarkdown notebooks can be created by clicking the button `r fontawesome::fa("file-circle-plus")` and you will see "R markdown". This will give you a template for an Rmarkdown file.
You can choose a title in " " and output style, html_document, pdf_document, or word_document.
In Rmarkdown, you can write regular text. You can use \$ <<< latex >>> $ if you like, where the \$ indicates use latex code. If you need a dollar signe put a \\ in front of it.
If you want to add a block of R code it codes in you set it up like:
\```{r <<< name >>>, include = TRUE/FALSE, echo = TRUE/FALSE }
<<< code goes here >>>
\```
You give the code block a name, then choose TRUE or FALSE for include and echo. Include means = TRUE means include the code and result, FALSE means omit the code and result in the resulting file. echo = TRUE means include the code and result in the result text, echo = FALSE means include the result, but not the original code.
When you are ready to create the whole document click the `r fontawesome::fa("yarn", fill = "#337ab7")` $\bf{Knit}$ icon.
To execute a single block of code, click the `r fontawesome::fa("play", fill = "forestgreen")` button on the right side of the block.
### Problem 1
These questions are warm up question.
A. Write a function that divides $a$ by $b$, but returns $0$ zero if both $a$ and $b$ are zero. (20%)
B. Create an empty dataframe named `my_numb`, with columns $x$, $y1$, $y2$. Use a loop to make the $x$ column all integers from 1 to 100 and the $y1$ column $7x - 2$ and the $y2$ column $e^{0.01x}$. (25%)
C. Make a plot, using ggplot, of the `my_numb` with $x$ on the $x axis$ and $y1$ and $y2$ on the y-axis. Make $y1$ blue and $y2$ red. (25%)
D. Create a function that plays rock-paper-scissors and wins $1/3$ of the time. Hint: R generates a psuedo-random number between zero and one using the function $runif(1)$. (30%)
### Problem 2
The logistic growth model has two parameters $r$ and $K$, which are the growth rate and carrying capacity of the population.
$N_{t+1} = N_{t} + r N_{t}(1-\frac{N_{t}}{K})$. In this model $t$ represents a time step. We will discuss dynamics later in the term.
A. What is derivative of $N_{t+1}$ with respect to $N_{t}$? (20%).
B. Create your own function that as $N_{t}$, $r$, and $K$ as inputs and $N_{t+1}$ as an output (30%).
C. Create a block of code that has a loop that allows you to simulate the population growth over 100 time steps for $r = 1$ and for $r = 2.8$. For both simulations set $K = 100$. Let the starting populations be $N_0$ and let $N_0 = 2$. (30%)
D. Use ggplot to make a graph of the two trajectories side by side. Put time on the x-axis and population, $N_t$ on the y-axis. (20%).
### Problem 3
For this question you are working with a function that we have called “crazyfunction.” Crazyfunction is a function $𝑦=crazyfunction(𝑥)$ that you can use to calculate a $y$ for all $x\in[2,4]$, but you cannot write out the function explicitly because it is two complicated.
A. Get $crazyfunction(x)$ from my GitHub repo `https://github.com/efenichel/AMES`. You do this one of two ways. You could clone my repo. However, you really just want the function. So go to `https://github.com/efenichel/AMES/blob/master/crazyFunction.r`. Above the code you will see a bar that starts with $\bf{Code}$. As you move to right, you will see a box $\bf{Raw}$. Click that box. It will open a new window with function as simple text. Copy the url. Then add `source("url")` to your code to load the crazyfunction directly from the internet. You should now be able to type $crazyfunction(3)$ and get $52.52054$ as the output. (10%)
B. What is the slope of $crazyfunction$ on average between 3 and 4? (10%).
C. Write loop to find the lowest value of $x$, such that slope of $crazyfunction$ between $x = 3$ and that number is not changing to two-decimal precision.(40%).
D. Write a function using only loops (or applies) and $+, -, \times, \div$ operations to calculate $\sqrt{27}$ to three-decimal precision. (40%)
### Problem 4
We are going to look at how to integrate in a computer.
A. Simplify the integral $\int_{2}^{10}{3x^4 +19e^{3+2x}}dx$ and write a function for the simplified integral. (10%).
B. Simplify the integral $\int_{0}^{1}{5xe^{2x}}dx$ and write a function for the simplified integral. (10%).
C. Now create a function to numerical solve the integral in 4A using the logic of the Riemann sum. Let the integrand (expression inside the integral before the $dx$). Write a loop to show that as we divide the interval into smaller and smaller bits, the approximation gets better. Hint, first try dividing the interval evenly and sum the expression $w\times f(x)$, where $w$ is the width of the of our rectangles (e.g., if the interval is $[0,1]$ and we divide by $10$, then $w= 0.1$). Save your results in an array that $1000 \times 5$, with the slice number in the first column, the result for 4A in the second column, and the result for 4B in the 4th column. (30%).
D. The Riemann sum works, is logical, but slow. What you just did is called numerical quadrature. A more sophisticated approach is called Simpson's rule. Simpson's rule says the integral $\int_{a}^{b}{f(x)}dx$ is approximately $h \sum_j f(z_j)$, where $h = \frac{b-a}{n-1}$ and $z_j = a + (j+1)h$. Implement Simpson's rule for 4A and 4B, and save the results in the 3rd and 4th columns. (30%).
E. Make a figure to compare the the results from part C to part D. (20%).