forked from justingrimmer/MathCamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mc3_17.R
58 lines (43 loc) · 1.31 KB
/
mc3_17.R
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
##putting together a function that approximates, via a taylor series.
approx_e<- function(a, x, order){
out<- 0
for(z in 0:order){
out<- out + (exp(a)/factorial(z))*(x - a)^z
}
return(out)
}
x<- seq(-2, 5, len = 1000)
plot(exp(x)~x, type='l', lwd = 3)
lines(approx_e(1, x, 0)~x, col='red')
lines(approx_e(1, x, 1)~x, col='red')
lines(approx_e(1, x, 2)~x, col='red')
lines(approx_e(1, x, 3)~x, col= 'red')
lines(approx_e(1, x, 4)~x, col='red')
lines(approx_e(1, x, 5)~x, col='red')
lines(approx_e(1, x, 6)~x, col='red')
lines(approx_e(1, x, 7)~x, col='red')
lines(approx_e(1, x, 8)~x, col='red')
approx_cos<- function(a, x, order){
out<- 0
int<- 0
vals<- c(cos(a), -sin(a), -cos(a), sin(a))
for(z in 0:order){
int<- int +1
out<- out + (vals[int]/factorial(z))*(x - a)^z
if(int==4){
int<- 0
}
}
return(out)
}
x<- seq(-10, 10, len = 1000)
plot(cos(x)~x, type='l', lwd = 3)
lines(approx_cos(0, x, 1)~x, col='red')
lines(approx_cos(0, x, 2)~x, col='red')
lines(approx_cos(0, x, 3)~x, col= 'red', lwd = 3)
lines(approx_cos(0, x, 4)~x, col='red', lwd = 3)
lines(approx_cos(0, x, 5)~x, col='red', lwd = 3)
lines(approx_cos(0, x, 6)~x, col='red', lwd = 3)
lines(approx_cos(0, x, 7)~x, col='red', lwd = 3)
lines(approx_cos(0, x , 20)~x, col='red', lwd=3)
lines(approx_cos(0, x, 100)~x, col='red', lwd = 3)