# Load the libraries
library(Deriv)
library(mosaicCalc)
## Loading required package: mosaicCore
## 
## Attaching package: 'mosaicCalc'
## The following object is masked from 'package:stats':
## 
##     D

Homework 1 Derivatives Find the derivatives with the respect to x of the following.

  1. \(F(x|x≥0)=1−{e}^{−λx}\)
# Declare the functions
fnc1 <- function(x){
   (1-exp(-lambda*x))
}
# Do the derivation
ans <- Deriv(fnc1)
# show the result
ans
## function (x) 
## lambda * exp(-(lambda * x))
  1. \(F(x|b>a)=\frac{(x−a)}{(b−a)}\)
# Declare the functions
fnc2 <- function(x){
   (x-a)/(b-a)
}
# Do the derivation
ans <- Deriv(fnc2)
# show the result
ans
## function (x) 
## 1/(b - a)

3.\(F(x|a<x≤c≤b)=\frac{{(x−a)}^2}{(b−a)(c−a)}\)

# Declare the functions
fnc3 <- function(x){
   (x-a)^2/((b-a)*(c-a))
}
# Do the derivation
ans <- Deriv(fnc3)
# show the result
ans
## function (x) 
## 2 * ((x - a)/((b - a) * (c - a)))
  1. \(F(x|a≤c<x<b)=1−\frac{{(b−x)}^2}{(b−a)(c−a)}\)
# Declare the functions
fnc4 <- function(x){
   1 - (b-x)^2/((b-a)*(c-a))
}
# Do the derivation
ans <- Deriv(fnc4)
# show the result
ans
## function (x) 
## 2 * ((b - x)/((b - a) * (c - a)))

Integrals Solve the following definite and indefinite integrals 5. \[ \int_0^{10}3{x}^3 \, dx \]

# Declare the functions
q5 <- function(x){
  3*x^3
}
# Do the integration
integrate(q5, 0, 10)
## 7500 with absolute error < 8.3e-11
  1. \[ \int_0^x x\lambda{e}^{-\lambda x} \, dx \]
# Declare the function
fnc <- antiD(x*lambda*(exp(-lambda*x))~x)
# Show calculation
fnc(x = 5,lambda = 5)
## [1] 0.2
  1. \[ \int_0^{0.5} \frac{1}{b-a} \, dx \]
# Declare the functions
q7 <- function (x, C = 0, a, b) 
{
  1/(a - b) * x 
}
# Do the integration
integrate(q7, a = 5, b  = 10, lower = 0, upper = 0.5)
## -0.025 with absolute error < 2.8e-16
  1. \[ \int_0^x x\frac{1}{\Gamma{(\alpha)}\beta^\alpha}x^{\alpha-1}e^{-\beta x} \, dx \]
# Declare the function
fnc8 <- antiD((1/(gamma(alpha)*beta^alpha))*(x^alpha)*exp(-(beta*x))~x)
# Do the integratin
fnc8(x = 5, alpha = 5, beta = 6)
## [1] 1.378181e-08
X <- matrix(c(1,2,3,3,3,1,4,6,8), byrow = TRUE, ncol = 3)

Linear Algebra With the following matrix,

\[\mathbf{X} = \left[\begin{array} {rrr} 1 & 2 & 3 \\ 3 & 3 & 1 \\ 4 & 6 & 8 \end{array}\right] \]

  1. Invert it using Gaussian row reduction.
XI <-  cbind(X, diag(3))
# Make the lower values 0
XI[2,] <- XI[2,] - 3*XI[1,]     # row 2 <- row 2 - 3 * row 1

XI[3,] <- XI[3,] - 4 * XI[1,]      # row 3 <- row 3 - 4 * row 2

XI[2,] <-  -1 * XI[2,] + XI[3,]          # row 2 <- -1 * row 2 + row 3

XI[3,] <-  2 * XI[2,] + XI[3,]  # row 3 <- 2 * row 2 + row 3
XI[3, ] <- XI[3,]/4 # row 3 <- row 3 / 4

# Make the upper values 0
XI[1,] <- XI[1, ] - 2 * XI[2,] # row 1 <- row 1 - 2 * row 2
XI[2, ] <- XI[2, ] - 4 * XI[3,] # row 2 <- row 2 - 4 * row 3
XI[1, ] <- XI[1, ] + 5 * XI[3,] # row 1 <- row1 + 5 * row 3
# Soluition
XI
##      [,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
  1. Find the determinant.
# Find the determinant
det(X)
## [1] -4
  1. Conduct LU decomposition
# Find n
  n <- nrow( X )
# Declare empty L matrix
  L <- diag( 1, nrow=n, ncol=n )
  # Declare diagonal matrix
  U <- matrix( 0, nrow=n, ncol=n )
  for ( i in 1:n ) {
    ipos <- i + 1
    imat <- i - 1
    for ( j in 1:n ) {
      U[i,j] <- X[i,j]
      if ( imat > 0 ) {
        for ( k in 1:imat ) {
          U[i,j] <- U[i,j] - L[i,k] * U[k,j]
        }
      }
    }
    if ( ipos <= n ) {
      for ( j in ipos:n ) {
        L[j,i] <- X[j,i]
        if ( imat > 0 ) {
          for ( k in 1:imat ) {
            L[j,i] <- L[j,i] - L[j,k] * U[k,i]
          }
        }
        L[j,i] <- L[j,i] / U[i,i]
      }    
    }
  }
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
  1. Multiply the matrix by it’s inverse.
X %*% solve(X)
##               [,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