Homework 1

Derivatives

Find the derivatives with respect to x of the following:

1Q

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

1A

library(Deriv)

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

# derivative of function
ans=Deriv(f1)
ans
## function (x) 
## lambda * exp(-(lambda * x))

2Q

\[ F(x|b>a) = \frac{x - a}{b-a} \]

2A

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

# derivative of function
ans=Deriv(f2)
ans
## function (x) 
## 1/(b - a)

3Q

\[ F(x|a<x \leq c \leq b) = \frac{(x-a)^2}{(b-a)(c-a)} \]

3A

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

# derivative of function
ans=Deriv(f3)
ans
## function (x) 
## 2 * ((x - a)/(b - a)(c - a))

4Q

\[ F(x|a<x \leq c < x < b) = 1 - \frac{(b-x)^2}{(b-a)(c-a)} \]

4A

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

# derivative of function
ans=Deriv(f4)
ans
## function (x) 
## 2 * ((b - x)/(b - a)(c - a))

Integrals

Solve the following definite and indefinite integrals

5Q

\[ \int_{0}^{10}3x^{3}dx \]

5A

# store function
f5=function(x)(3*(x^3))

# integrate definite integral
ans=integrate(Vectorize(f5),0,10)
ans
## 7500 with absolute error < 8.3e-11

6Q

\[ \int_{0}^{x}x\lambda e^{-\lambda*x} dx \]

6A

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
# integrate indefinite integral
ans=antiD(x*lambda*exp(-lambda*x)~x)
ans
## function (x, lambda, C = 0) 
## {
##     numerical_integration(.newf, .wrt, as.list(match.call())[-1], 
##         formals(), from, ciName = intC, .tol)
## }
## <environment: 0x7ff9724b2620>

7Q

\[ \int_{0}^{.5}\frac{1}{b-a}dx \]

7A

# assign values to variables a & b
a<-0
b<-1

# store function
f7=function(x)(1/(b-a))

# integrate definite integral
ans=integrate(Vectorize(f7),0,.5)
ans
## 0.5 with absolute error < 5.6e-15

8Q

\[ \int_{0}^{x}x\frac{1}{\Gamma(\alpha)\beta^\alpha}x^{\alpha-1}e^{-\beta x}dx \]

8A

# integrate indefinite integral
ans=antiD(x^{alpha+1}/gamma*alpha*beta^{alpha}*e^{beta}~x)
## Warning in if (regexpr(rhsVar, deparse(lform[[2]], width.cutoff = 500))
## == : the condition has length > 1 and only the first element will be used

## Warning in if (regexpr(rhsVar, deparse(lform[[2]], width.cutoff = 500))
## == : the condition has length > 1 and only the first element will be used

## Warning in if (regexpr(rhsVar, deparse(lform[[2]], width.cutoff = 500))
## == : the condition has length > 1 and only the first element will be used
ans
## function (x, alpha, gamma, beta, e, C = 0) 
## {
##     numerical_integration(.newf, .wrt, as.list(match.call())[-1], 
##         formals(), from, ciName = intC, .tol)
## }
## <environment: 0x7ff9749ff1c0>

Linear Algebra

With the following matrix,

\[ \quad x=\begin{bmatrix} 1 & 2 & 3 \\ 3 & 3 & 1 \\ 4 & 6 & 8 \end{bmatrix} \]

9Q

Invert it using Gaussian row reduction.

9A

library(matlib)
## Warning in rgl.init(initValue, onlyNULL): RGL: unable to open X11 display
## Warning: 'rgl_init' failed, running with rgl.useNULL = TRUE
# store matrix
A9<-rbind(c(1,2,3),c(3,3,1),c(4,6,8))
A9
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    3    3    1
## [3,]    4    6    8
# determine whether matrix is solvable
gaussianElimination(A9, numeric(3))
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    1    0    0
## [3,]    0    0    1    0
# find inverse matrix by elimination
gaussianElimination(A9, diag(3))
##      [,1] [,2] [,3] [,4] [,5]  [,6]
## [1,]    1    0    0 -4.5 -0.5  1.75
## [2,]    0    1    0  5.0  1.0 -2.00
## [3,]    0    0    1 -1.5 -0.5  0.75
ans = inv(A9)
ans
##      [,1] [,2]  [,3]
## [1,] -4.5 -0.5  1.75
## [2,]  5.0  1.0 -2.00
## [3,] -1.5 -0.5  0.75
# validate above solution by finding inverse using solve method
B9=solve(A9)
B9
##      [,1] [,2]  [,3]
## [1,] -4.5 -0.5  1.75
## [2,]  5.0  1.0 -2.00
## [3,] -1.5 -0.5  0.75

10Q

Find the determinant.

10A

# find determinant of matrix
ans=det(A9)
ans
## [1] -4

11Q

Conduct LU decomposition.

11A

library(pracma)
## 
## Attaching package: 'pracma'
## The following objects are masked from 'package:matlib':
## 
##     angle, inv
## The following object is masked from 'package:mosaicCore':
## 
##     logit
# use lu() method for LU decomposition
ans=lu(A9)
ans
## $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

12Q

Multiply the matrix by its inverse.

12A

# multiply matrix A9 and its inverse B9
ans=A9%*%B9
ans
##               [,1]          [,2]         [,3]
## [1,]  1.000000e+00 -2.220446e-16 4.440892e-16
## [2,] -4.440892e-16  1.000000e+00 2.220446e-16
## [3,]  0.000000e+00  0.000000e+00 1.000000e+00