MTH-2520 Homework 2: Using R as a Scientific Calculator

Homework 2: Using R as a Scientific Calculator


Homework 2 Instructions:

  1. This is a skeleton .rmd notebook for HW 2. Fill in the code for the other problems and write a summary statement on the answers. Some Latex code is included below (see pr. 5).

Pr. 1: Use R to compute the following

#Pr. 1a
10**2 + 3*60/8-3
[1] 119.5

\[\boxed{10^2 + \frac{3*60}{8} - 3 = 119.5}\]

#Pr. 1b
(5**3*(6-2))/(61-3+4)
[1] 8.064516

\[\boxed{\frac{5^3 * (6 - 2)}{61 -3 + 4} = 8.06}\]

#Pr. 1c
((0.44 * (1 - 0.44)) / 34)^{1/2}
[1] 0.08512966

\[\boxed{\left( \frac{0.44 * (1 - 0.44)}{34} \right) ^{1/2}}\]

#Pr. 1d
x <- c(2, 2.5, 3, 3.5, 4)
2^x
[1]  4.000000  5.656854  8.000000 11.313708 16.000000
#Pr. 1e
x <- c(10:15)
log(x)
[1] 2.302585 2.397895 2.484907 2.564949 2.639057 2.708050
#Pr. 1f
n <- c(5:10)
factorial(n)
[1]     120     720    5040   40320  362880 3628800
#Pr. 1g
cos((2 * pi) / 3)
[1] -0.5

\[\boxed{cos\frac{2\pi}{3} = -0.5}\]

#Pr. 1h
# So, based on my reading it seems R operates in radians by default for stuff like cos.
# We can fix that by converting our given degrees to radians in-line!
# r = d * pi / 180
cos((30*pi) / 180)
[1] 0.8660254

\[\boxed{cos(30^{\circ}) = 0.866}\]

#Pr. 1i
r <- c(0:5)
choose(5, r)
[1]  1  5 10 10  5  1
\[\frac{n!}{r! (n - r)!}\]
#Pr. 1j
r <- c(0:6)
choose(6, r)
[1]  1  6 15 20 15  6  1
\[\frac{n!}{r! (n - r)!}\]

Pr. 2 Use the quadratic formula to compute the roots of the quadratic equations.

2a. \(3x^2 +2x - 1\)

#Pr. 2a
a = 3
b = 2
c = -1
(-b + sqrt(b**2 - 4*a*c))/(2*a)
[1] 0.3333333
(-b - sqrt(b**2 - 4*a*c))/(2*a)
[1] -1

The roots of the quadratic equation \(3x^2+2x-1=0\) are \(\boxed{x=-1}\) and \(\boxed{x=\frac{1}{3}}\).


2b. \(x^2 - x - 6 = 0\)

#Pr. 2b
a = 1
b = -1
c = -6
(-b + sqrt(b**2 - 4*a*c))/(2*a)
[1] 3
(-b - sqrt(b**2 - 4*a*c))/(2*a)
[1] -2

The roots of the quadratic equation \(x^2-x-6=0\) are \(\boxed{x=3}\) and \(\boxed{x=-2}\).


2c. \(x^2 + x + 1 = 0\)

#Pr. 2b
a = 1
b = 1
c = 1
(-b + sqrt(as.complex(b**2 - 4*a*c)))/(2*a)
[1] -0.5+0.8660254i
(-b - sqrt(as.complex(b**2 - 4*a*c)))/(2*a)
[1] -0.5-0.8660254i

The roots of the quadratic equation \(x^2+x+1=0\) are \(\boxed{x = -0.5 + 0.866i}\) and \(\boxed{x = -0.5 - 0.866i}\).


Pr. 3

pi
[1] 3.141593
round(pi)
[1] 3
round(pi,4)
[1] 3.1416
signif(pi,5)
[1] 3.1416
signif(pi,10)
[1] 3.141593
signif(pi,50)
[1] 3.141593
#After some messing around it seems 10 significant figures is the max I'm able to get out of this.
trunc(pi)
[1] 3
ceiling(pi)
[1] 4
floor(pi)
[1] 3

Pr. 4

trunc(4.87)
[1] 4
trunc(-4.87)
[1] -4
trunc(17.973)
[1] 17
round(4.87)
[1] 5

trunc() seems to just outright turn the given value into an integer with no regard to what the decimals are valued at. That’s very different from rounding which does take that into consideration.


Pr. 5

#5a.How many times does 11 divide into 2387 evenly?
2387 / 11
[1] 217
2387 %/%11
[1] 217
#5b. Compute 2387 mod 11.
2387 %% 11
[1] 0

Pr. 5c
Express 2387 in the form 11 * q + r
Let q be quotient and let r be remainder.
\(\boxed{2387 = 11 * 217 + 0}\)

#Pr. 5d quotient
2387 %/% 42
[1] 56
#Pr. 5e 2387 mod 42
2387 %% 42
[1] 35

Pr. 5f

\(2387 = 56*42 +35\)

#Pr. 5g quotient
170166719 %/% 31079
[1] 5475
#Pr. 5h 2387 mod 42
170166719 %% 31079
[1] 9194

Pr. 5i
Express 170,166,719 in the form 31,079 * q + r
Let q be quotient and let r be remainder.
\(\boxed{170166719 = 5475*31079 + 9194}\)


Pr. 6 How can you use %% to compute the units digit of a given integer n. Use this process to have R give the units digit for \(n = 1234\) and \(n = 517\).

1234 %% 10
[1] 4
517 %% 10
[1] 7

Why does this work? To find out let’s just look at the math!
\(1234 \mod 10 = 123 R 4\)
The %% function only shows the remainder and so that’s how we get our answer.


Pr.7 How can you use %/% to return all but the units digit of a given integer n? Use this process to have R return all but the units digit for \(n = 1234\) and \(n = 517\).

1234 %/% 10
[1] 123
517 %/% 10
[1] 51

To find out why this works let’s look at the same write up of the problem again.
\(1234 \mod 10 = 123 R 4\)
The %% function only shows us the following:
\(1234 \mod 10 = 123 \boxed{R 4}\)
whereas the %/% function shows us:
\(1234 \mod 10 = \boxed{123} R 4\).


Pr. 8

#Pr. 8a
x <- c(1:10)
x
 [1]  1  2  3  4  5  6  7  8  9 10
class(x)
[1] "integer"
mode(x)
[1] "numeric"
typeof(x)
[1] "integer"

#Pr. 8b
x <- sqrt(1:10)
x
 [1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427
 [9] 3.000000 3.162278
class(x)
[1] "numeric"
mode(x)
[1] "numeric"
typeof(x)
[1] "double"

#Pr. 8c
x <- c("alpha", "bravo", "charlie", "delta")
x
[1] "alpha"   "bravo"   "charlie" "delta"  
class(x)
[1] "character"
mode(x)
[1] "character"
typeof(x)
[1] "character"
Built with Hugo
Theme Stack designed by Jimmy