Derivatives

Find the derivatives with respect to x for the following:

library(Deriv)
library (mosaic)
library(mosaicCalc)
library(matlib)

f1 <- function(x) 1 - exp(lambda *x)
Deriv(f1)
## function (x) 
## -(lambda * exp(lambda * x))
f2 <- function(x) (x-a) / (b-a)
Deriv(f2)
## function (x) 
## 1/(b - a)
f3 <- function(x) ((x-a)^2) / ((b-a)(c-a))
Deriv(f3)
## function (x) 
## 2 * ((x - a)/(b - a)(c - a))
f4 <- function(x) 1 - ((b-x)^2 / ((b-a) (c-a)))
Deriv(f4)
## function (x) 
## 2 * ((b - x)/(b - a)(c - a))

Integrals

Solve the following definite and indefinite integrals

library (Deriv)
library(mosaicCalc)
library(mosaic)

f5 <- function(x) 3*x^3
integrate(f5, 0, 10)
## 7500 with absolute error < 8.3e-11
lambda <- 1
#integration by parts
#let u = x and dv = e^x
#du = 1dx and v = integral of e^-x
#du - integral of vdu
#1 - - e^-x + c ==> 1 + e^-x + c
ff <- function(x) x * lambda
Deriv(ff) 
## function (x) 
## lambda
antiD(exp(-lambda * x) ~x)
## function (x, C = 0, lambda = 1) 
## 1/(-lambda) * exp(-lambda * x) + C
#lambda - 1/(-lambda) * exp(-lambda * x) + C

#Let b = 1 and a = -2
b<- 1
a<- -2
f7 <- function(x) 1/(b - a)
integrate(Vectorize(f7), 0, 0.5)
## 0.1666667 with absolute error < 1.9e-15

Linear Algebra

library(matlib)

#Create matrix X
Xmatrix <- matrix(c(1, 2, 3, 3, 3, 1, 4, 6, 8), nrow = 3, byrow = TRUE)
print(Xmatrix)
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    3    3    1
## [3,]    4    6    8
XI <- cbind(Xmatrix, diag(3)) #identity matrix joined to xmatrix
print(XI)
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1    2    3    1    0    0
## [2,]    3    3    1    0    1    0
## [3,]    4    6    8    0    0    1
#Gaussian Elimination
XI[2,] <- -3 *XI[1,] + XI[2,]    
XI[3,] <- -4 * XI[1,] + XI[3,]   
XI[2,] <- -(1/3) * XI[2,]         
XI[1,] <- -2 * XI[2,] + XI[1,]    
XI[3,] <- 2 * XI[2,] + XI[3,]     
XI[3,] <- (3/4) * XI[3,]      
XI[1,] <- (7/3) * XI[3,] + XI[1,] 
XI[2,] <- -(8/3) * XI[3,] + XI[2,]

#last three columns are the inverse of xmatrix
inverseX <- XI[,-(1:3)]  

inverseX #inverse of X
##      [,1] [,2]  [,3]
## [1,] -4.5 -0.5  1.75
## [2,]  5.0  1.0 -2.00
## [3,] -1.5 -0.5  0.75
#Finding the determinant of x
det(Xmatrix) 
## [1] -4
#Conduct LU decomposition
library(Matrix)

#expand(lu(Xmatrix))
luX <- lu(Xmatrix)
expandluX <- expand(luX)
L<-expandluX$L 
U<-expandluX$U 

#display L - Lower triangular matrix
L
## 3 x 3 Matrix of class "dtrMatrix" (unitriangular)
##      [,1]       [,2]       [,3]      
## [1,]  1.0000000          .          .
## [2,]  0.7500000  1.0000000          .
## [3,]  0.2500000 -0.3333333  1.0000000
#display U - Upper triangular matrix
U
## 3 x 3 Matrix of class "dtrMatrix"
##      [,1]       [,2]       [,3]      
## [1,]  4.0000000  6.0000000  8.0000000
## [2,]          . -1.5000000 -5.0000000
## [3,]          .          . -0.6666667
#Multiply the matrix by it's inverse.
inverseX %*% Xmatrix
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1