Integrals
# Question 5
fx=function(x){3*(x^3)}
integrate(fx,lower=0,upper=10)
## 7500 with absolute error < 8.3e-11
# Question 6
library(mosaicCalc)
## Warning: package 'mosaicCalc' was built under R version 3.6.1
## Loading required package: mosaicCore
## Warning: package 'mosaicCore' was built under R version 3.6.1
## Registered S3 method overwritten by 'mosaic':
## method from
## fortify.SpatialPolygonsDataFrame ggplot2
##
## Attaching package: 'mosaicCalc'
## The following object is masked from 'package:stats':
##
## D
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: 0x0000000012cd8228>
Linear Algebra
# Question 9
# Create 3x3 matrix
A=matrix(c(1,2,3,3,3,1,4,6,8),nrow = 3, byrow=TRUE)
# Join an identity matrix to A
(AI<- cbind(A, diag(3)))
## [,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
#inverse of the matrix A
library(matlib)
## Warning: package 'matlib' was built under R version 3.6.1
echelon(cbind(A, diag(3)),verbose=TRUE, fractions=TRUE)
##
## Initial matrix:
## [,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
##
## row: 1
##
## exchange rows 1 and 3
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 4 6 8 0 0 1
## [2,] 3 3 1 0 1 0
## [3,] 1 2 3 1 0 0
##
## multiply row 1 by 1/4
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 3/2 2 0 0 1/4
## [2,] 3 3 1 0 1 0
## [3,] 1 2 3 1 0 0
##
## multiply row 1 by 3 and subtract from row 2
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 3/2 2 0 0 1/4
## [2,] 0 -3/2 -5 0 1 -3/4
## [3,] 1 2 3 1 0 0
##
## subtract row 1 from row 3
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 3/2 2 0 0 1/4
## [2,] 0 -3/2 -5 0 1 -3/4
## [3,] 0 1/2 1 1 0 -1/4
##
## row: 2
##
## multiply row 2 by -2/3
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 3/2 2 0 0 1/4
## [2,] 0 1 10/3 0 -2/3 1/2
## [3,] 0 1/2 1 1 0 -1/4
##
## multiply row 2 by 3/2 and subtract from row 1
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 0 -3 0 1 -1/2
## [2,] 0 1 10/3 0 -2/3 1/2
## [3,] 0 1/2 1 1 0 -1/4
##
## multiply row 2 by 1/2 and subtract from row 3
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 0 -3 0 1 -1/2
## [2,] 0 1 10/3 0 -2/3 1/2
## [3,] 0 0 -2/3 1 1/3 -1/2
##
## row: 3
##
## multiply row 3 by -3/2
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 0 -3 0 1 -1/2
## [2,] 0 1 10/3 0 -2/3 1/2
## [3,] 0 0 1 -3/2 -1/2 3/4
##
## multiply row 3 by 3 and add to row 1
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 0 0 -9/2 -1/2 7/4
## [2,] 0 1 10/3 0 -2/3 1/2
## [3,] 0 0 1 -3/2 -1/2 3/4
##
## multiply row 3 by 10/3 and subtract from row 2
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 0 0 -9/2 -1/2 7/4
## [2,] 0 1 0 5 1 -2
## [3,] 0 0 1 -3/2 -1/2 3/4
# Question 10
# Determinant of matrix A
det(A)
## [1] -4
# Question 11
# LU decomposition
A=matrix(c(1,2,3,3,3,1,4,6,8),nrow = 3, byrow=TRUE)
LU(A)
## $P
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1
##
## $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 matix A by its inverse (I)
I<-solve(A) # find the inverse using slove
I
## [,1] [,2] [,3]
## [1,] -4.5 -0.5 1.75
## [2,] 5.0 1.0 -2.00
## [3,] -1.5 -0.5 0.75
A%*%I # multiply matrix A and inverse I
## [,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