-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL3.Rmd
96 lines (70 loc) · 1.34 KB
/
L3.Rmd
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
---
title: "L3"
author: "Xiaoting Chen"
date: "2023-08-09"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(purrr)
library(dplyr)
library(ggplot2)
library(tidyr)
```
## Transform data for plotting
purrr::map* function
```{r}
x <- 1:10
y <- x |>
map(\(x) runif(n=100, min=x,max=15)) |>
map_dbl(mean)
# "\(x)" equals to creating a new function taking x as input
## name(x) <- function(x) { }
y
```
```{r}
rates <- seq(0.05, 0.2, length = 10)
p <- 100
time <- seq(1, 50, length = 50)
Pe <- function(A, r, t) A*exp(r * t)
d <- time |>
map(\(x) Pe(A=p, r = rates, t = x))
#str(d)
```
practice
```{r}
data("Arbuthnot")
# head(Arbuthnot)
dat_p <- Arbuthnot[, 1:3] %>%
pivot_longer(cols = c(Males, Females),names_to = "gender", values_to = "christening")
ggplot(dat_p, aes(x = Year, y = christening, color = gender)) +
geom_line()
```
## integration
### Riemann Sum
```{r}
riemann <- function(f, lower, upper, step_size) {
step <- seq(lower, upper, by = step_size)
s <- 0 # initialize the sum
for (i in 2:length(step)) {
# multiply base by the height of the rectangle
area <- step_size * f(step[i])
s <- s + area
}
return(s)
}
```
evaluate the method
```{r}
f <- function(x) x * sin(x^2)
riemann(f, 1, 3, 0.01)
integrate(f, 1, 3)
```
```{r}
```
```{r}
```
```{r}
```
```{r}
```