Derivatives

Find the derivatives with the respect to x of the following.

Question 1:

\(F(x|x \ge 0)=1-e^{\lambda*x}\)

Answer 1:

# in order to find the derivative in R, we will install the "Deriv" library.
# we need to include the "repos" so it doesnt give error for R markdowns.

install.packages('Deriv', repos="http://cran.us.r-project.org") 
## Installing package into 'C:/Users/Anil Akyildirim/Documents/R/win-library/3.6'
## (as 'lib' is unspecified)
## package 'Deriv' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\Anil Akyildirim\AppData\Local\Temp\Rtmp4Y5XdH\downloaded_packages
library('Deriv')

# creating the function in R

f1=function(x)1-(exp(-lambda*x))

# derivative of the function

der_f1=Deriv(f1)
der_f1
## function (x) 
## lambda * exp(-(lambda * x))

Question 2:

\(F(x|b > a)=(x-a)/(b-a)\)

Answer 2:

# creating the function in R

f2=function(x)(x-a)/(b-a)

# finding the derivative of the function - f2-

der_f2=Deriv(f2)
der_f2
## function (x) 
## 1/(b - a)

Question 3:

\(F(x|a < x \le c \le b)=(x-a)^2/((b-a)*(c-a))\)

Answer 3:

# creating the function in R

f3=function(x)(x-a)^2/((b-a)*(c-a))

# finding the derivative of the function -f3-

der_f3=Deriv(f3)
der_f3
## function (x) 
## 2 * ((x - a)/((b - a) * (c - a)))

Question 4:

\(F(x|a \le c < x < b)=1-(((b-x)^2)/((b-a)*(c-a)))\)

Answer 4:

# creating the function in R

f4=function(x)1-(((b-x)^2)/((b-a)*(c-a)))

# finding the derivative of the function

der_f4=Deriv(f4)
der_f4
## function (x) 
## 2 * ((b - x)/((b - a) * (c - a)))

Integrals

Solve the following definite and indefinite integrals

Question 5:

\(\int_{0}^{10}\)\(3x^3dx\)

Answer 5:

# creating the function in R

f5=function(x)3*x^3
integrate(Vectorize(f5), 0, 10) # it is a definite integral
## 7500 with absolute error < 8.3e-11

Question 6

\(\int_{a}^{b}\)\(x*\lambda*e^(-\lambda*x)dx\)

Answer 6

This is an indefinite integral. We installed mosaic package(please be aware if you try to install the package via script it does not include mosaicCalc which is neccessary to install in order to use D() or antiD() functions. We install this package via RStudio Packages section.

library('mosaicCalc')
## Loading required package: mosaicCore
## Registered S3 method overwritten by 'mosaic':
##   method                           from   
##   fortify.SpatialPolygonsDataFrame ggplot2
## 
## Attaching package: 'mosaicCalc'
## The following object is masked from 'package:stats':
## 
##     D
# creating the function in R

f6=function(x)x*lambda*exp(-lambda*x)

#using antiD function for indefinite integral

antiD(x*lambda*exp(-lambda*x)~x)
## function (x, lambda, C = 0) 
## {
##     numerical_integration(.newf, .wrt, as.list(match.call())[-1], 
##         formals(), from, ciName = intC, .tol)
## }
## <environment: 0x000000002a74f630>

Question 7

\(\int_{0}^{5}\)\(1/(b-a)dx\)

Answer 7

# in this case a and b are constant and i need to provide them a number to get a result.
# creating a function 

a=1
b=2
fx2=function(x)(1/(b-a))
integrate(Vectorize(fx2), 0, 5)
## 5 with absolute error < 5.6e-14

Question 8:

Answer 8:

# indefinite integral 
antiD(x*1/gamma(x)*x^(sigma-1)*exp(-beta*x)~x)
## function (x, sigma, beta, C = 0) 
## {
##     numerical_integration(.newf, .wrt, as.list(match.call())[-1], 
##         formals(), from, ciName = intC, .tol)
## }
## <environment: 0x000000002afeb358>

Linear Algebra

With the following matrix

Question 9

Invert it using Gaussian row reduction.

Answer 9

# creating the matrix first

A <- matrix(c(1,2,3,3,3,1,4,6,8), nrow =3, byrow=TRUE)
A
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    3    3    1
## [3,]    4    6    8
# we can use solve() function to find the inverse of a matrix
B <- solve(A)
B
##      [,1] [,2]  [,3]
## [1,] -4.5 -0.5  1.75
## [2,]  5.0  1.0 -2.00
## [3,] -1.5 -0.5  0.75

Question 10:

Find the determinant.

Answer 10:

# to find the determinant i can use the det() function in R
det(A)
## [1] -4

Question 11:

Conduct LU decomposition

Answer 11:

# we can use lu() or ln.decomposition() functions for LU decomposition of the matrix
# in order to do that, we need to install pracma v1.9.9
# I have done that from the "Packages" section instead of a running the install.packages('') script. 
# i have to include library('pracma') otherwise this code will not knit to HTML

library('pracma')
## 
## Attaching package: 'pracma'
## The following object is masked from 'package:mosaicCore':
## 
##     logit
lu(A)
## $L
##      [,1]      [,2] [,3]
## [1,]    1 0.0000000    0
## [2,]    3 1.0000000    0
## [3,]    4 0.6666667    1
## 
## $U
##      [,1] [,2]      [,3]
## [1,]    1    2  3.000000
## [2,]    0   -3 -8.000000
## [3,]    0    0  1.333333

Question 12:

Multiply the matrix by it’s inverse.

Answer 12:

# in order to multiply our matrix (which is A) and inverse of our matrix (which is B) we can use %% 
A%%B
##      [,1]          [,2]  [,3]
## [1,] -3.5 -8.881784e-16  1.25
## [2,]  3.0  1.000000e+00 -1.00
## [3,] -0.5 -1.776357e-15  0.50