You are on page 1of 4

1st home work

By Sophie Wegscheider

####### Exercise 1.1


> x <- c(1:50)
> x
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2
4 25 26 27 28 29 30 31
[32] 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
> y <- 2^x
> y

[1] 2.000000e+00 4.000000e+00 8.000000e+00 1.600000e+01 3.200000e+01 6.400


000e+01 1.280000e+02
[8] 2.560000e+02 5.120000e+02 1.024000e+03 2.048000e+03 4.096000e+03 8.192
000e+03 1.638400e+04
[15] 3.276800e+04 6.553600e+04 1.310720e+05 2.621440e+05 5.242880e+05 1.048
576e+06 2.097152e+06
[22] 4.194304e+06 8.388608e+06 1.677722e+07 3.355443e+07 6.710886e+07 1.342
177e+08 2.684355e+08
[29] 5.368709e+08 1.073742e+09 2.147484e+09 4.294967e+09 8.589935e+09 1.717
987e+10 3.435974e+10
[36] 6.871948e+10 1.374390e+11 2.748779e+11 5.497558e+11 1.099512e+12 2.199
023e+12 4.398047e+12
[43] 8.796093e+12 1.759219e+13 3.518437e+13 7.036874e+13 1.407375e+14 2.814
750e+14 5.629500e+14
[50] 1.125900e+15

> a <- x^2


> a == y

[1] FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALS
E FALSE FALSE FALSE
[16] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALS
E FALSE FALSE FALSE
[31] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALS
E FALSE FALSE FALSE
[46] FALSE FALSE FALSE FALSE FALSE

> which(a == y)

[1] 2 4

####### Exercise 1.2

> numbers <- seq(0,2*pi,0.1)


> numbers

[1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7
1.8 1.9 2.0 2.1 2.2
[24] 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.
0 4.1 4.2 4.3 4.4 4.5
[47] 4.6 4.7 4.8 4.9 5.0 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6.0 6.1 6.2
> sin(numbers)

[1] 0.00000000 0.09983342 0.19866933 0.29552021 0.38941834 0.4794255


4 0.56464247
[8] 0.64421769 0.71735609 0.78332691 0.84147098 0.89120736 0.9320390
9 0.96355819
[15] 0.98544973 0.99749499 0.99957360 0.99166481 0.97384763 0.9463000
9 0.90929743
[22] 0.86320937 0.80849640 0.74570521 0.67546318 0.59847214 0.5155013
7 0.42737988
[29] 0.33498815 0.23924933 0.14112001 0.04158066 -0.05837414 -0.1577456
9 -0.25554110
[36] -0.35078323 -0.44252044 -0.52983614 -0.61185789 -0.68776616 -0.7568025
0 -0.81827711
[43] -0.87157577 -0.91616594 -0.95160207 -0.97753012 -0.99369100 -0.9999232
6 -0.99616461
[50] -0.98245261 -0.95892427 -0.92581468 -0.88345466 -0.83226744 -0.7727644
9 -0.70554033
[57] -0.63126664 -0.55068554 -0.46460218 -0.37387666 -0.27941550 -0.1821625
0 -0.08308940

> cos(numbers)

[1] 1.00000000 0.99500417 0.98006658 0.95533649 0.92106099 0.8775825


6 0.82533561
[8] 0.76484219 0.69670671 0.62160997 0.54030231 0.45359612 0.3623577
5 0.26749883
[15] 0.16996714 0.07073720 -0.02919952 -0.12884449 -0.22720209 -0.3232895
7 -0.41614684
[22] -0.50484610 -0.58850112 -0.66627602 -0.73739372 -0.80114362 -0.8568887
5 -0.90407214
[29] -0.94222234 -0.97095817 -0.98999250 -0.99913515 -0.99829478 -0.9874797
7 -0.96679819
[36] -0.93645669 -0.89675842 -0.84810003 -0.79096771 -0.72593230 -0.6536436
2 -0.57482395
[43] -0.49026082 -0.40079917 -0.30733287 -0.21079580 -0.11215253 -0.0123886
6 0.08749898
[50] 0.18651237 0.28366219 0.37797774 0.46851667 0.55437434 0.6346928
8 0.70866977
[57] 0.77556588 0.83471278 0.88551952 0.92747843 0.96017029 0.9832684
4 0.99654210

> tangent1 <- tan(numbers)


> tangent2 <- sin(numbers)/cos(numbers)

> tangent1

[1] 0.00000000 0.10033467 0.20271004 0.30933625 0.42279322 0.5


4630249 0.68413681
[8] 0.84228838 1.02963856 1.26015822 1.55740772 1.96475966 2.5
7215162 3.60210245
[15] 5.79788372 14.10141995 -34.23253274 -7.69660214 -4.28626167 -2.9
2709751 -2.18503986
[22] -1.70984654 -1.37382306 -1.11921364 -0.91601429 -0.74702230 -0.6
0159661 -0.47272763
[29] -0.35552983 -0.24640539 -0.14254654 -0.04161665 0.05847385 0.1
5974575 0.26431690
[36] 0.37458564 0.49346673 0.62473308 0.77355609 0.94742465 1.1
5782128 1.42352648
[43] 1.77777977 2.28584788 3.09632378 4.63733205 8.86017490 80.7
1276297 -11.38487065
[50] -5.26749307 -3.38051501 -2.44938942 -1.88564188 -1.50127340 -1.2
1754082 -0.99558405
[57] -0.81394328 -0.65973057 -0.52466622 -0.40311090 -0.29100619 -0.1
8526223 -0.08337771

> tangent2

[1] 0.00000000 0.10033467 0.20271004 0.30933625 0.42279322 0.5


4630249 0.68413681
[8] 0.84228838 1.02963856 1.26015822 1.55740772 1.96475966 2.5
7215162 3.60210245
[15] 5.79788372 14.10141995 -34.23253274 -7.69660214 -4.28626167 -2.9
2709751 -2.18503986
[22] -1.70984654 -1.37382306 -1.11921364 -0.91601429 -0.74702230 -0.6
0159661 -0.47272763
[29] -0.35552983 -0.24640539 -0.14254654 -0.04161665 0.05847385 0.1
5974575 0.26431690
[36] 0.37458564 0.49346673 0.62473308 0.77355609 0.94742465 1.1
5782128 1.42352648
[43] 1.77777977 2.28584788 3.09632378 4.63733205 8.86017490 80.7
1276297 -11.38487065
[50] -5.26749307 -3.38051501 -2.44938942 -1.88564188 -1.50127340 -1.2
1754082 -0.99558405
[57] -0.81394328 -0.65973057 -0.52466622 -0.40311090 -0.29100619 -0.1
8526223 -0.08337771

> tangent1_2 <- which(tangent1 == tangent2)


> length(tangent1_2)

[1] 42

> tangent_difference <- tangent1 - tangent2


> tangent_difference

[1] 0.000000e+00 1.387779e-17 2.775558e-17 5.551115e-17 0.000000e+00


0.000000e+00
[7] -1.110223e-16 -1.110223e-16 0.000000e+00 -2.220446e-16 2.220446e-16
-2.220446e-16
[13] -4.440892e-16 -4.440892e-16 0.000000e+00 -1.776357e-15 0.000000e+00
0.000000e+00
[19] 0.000000e+00 0.000000e+00 0.000000e+00 -2.220446e-16 0.000000e+00
0.000000e+00
[25] 0.000000e+00 0.000000e+00 -1.110223e-16 0.000000e+00 0.000000e+00
0.000000e+00
[31] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
5.551115e-17
[37] 0.000000e+00 0.000000e+00 0.000000e+00 -1.110223e-16 2.220446e-16
0.000000e+00
[43] 0.000000e+00 -4.440892e-16 4.440892e-16 0.000000e+00 0.000000e+00
1.421085e-14
[49] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
0.000000e+00
[55] 0.000000e+00 0.000000e+00 -1.110223e-16 0.000000e+00 0.000000e+00
-5.551115e-17
[61] 0.000000e+00 0.000000e+00 0.000000e+00

> largest_difference <- max(tangent_difference)


> largest_difference

[1] 1.421085e-14

> maximum_difference <- which.max(tangent_difference)


> tangent_difference[maximum_difference]

[1] 1.421085e-14
####### Exercise 1.3

> ?floor

?floor
#ceiling: takes a single numeric argument x and returns a numeric
vector containing the smallest integers not less than the
corresponding elements of x.
#ceiling(x) rounds to the nearest integer that’s larger than x

#floor: takes a single numeric argument x and returns a numeric


vector containing the largest integers not greater than the
corresponding elements of x.
#floor(x) rounds to the nearest integer that’s smaller than x

#trunc: takes a single numeric argument x and returns a numeric


vector containing the integers formed by truncating the values in x
toward 0.
#trunc(x) rounds to the nearest integer in the direction of 0

#round: rounds the values in its first argument to the specified


number of decimal places (default 0).
#round numbers to the nearest possibility

> ceiling(-3.7)
[1] -3
> floor(-3.7)
[1] -4
> trunc(-3.7)
[1] -3
> round(-3.7)
[1] -4
> ceiling(3.8)
[1] 4
> floor(3.8)
[1] 3
> trunc(3.8)
[1] 3
> round(3.8)
[1] 4

You might also like