Source : https://dtkaplan.github.io/RforCalculus/

Aljabar Linier

Perhitungan untuk melakukan operasi aljabar linier termasuk yang paling penting dalam sains. Sangat penting bahwa unit yang digunakan untuk mengukur kinerja komputer untuk perhitungan ilmiah disebut “gagal”, singkatan dari “operasi titik mengambang” dan didefinisikan dalam perhitungan aljabar linier.

Bagi Anda, masalah dengan menggunakan komputer untuk melakukan aljabar linier terutama bagaimana mengatur masalah sehingga komputer dapat menyelesaikannya. Notasi yang akan kita gunakan telah dipilih secara khusus untuk berhubungan dengan jenis masalah yang akan Anda gunakan aljabar linier: menyesuaikan model dengan data. Ini berarti bahwa notasi akan sangat kompak.

library(matlib)
## Warning: package 'matlib' was built under R version 4.2.2
matriks1 <- matrix(c(2, 1, 1, 1, 2, 3, 2, 2, 1), 3, 3)
matriks2 <- matrix(c(4, 3, 1, 2, 2, 8, 4, 5, 6), 3, 3)

eq1 <- c(48, 26, 36)
eq2 <- c(38, 27, 36)

matriks1
##      [,1] [,2] [,3]
## [1,]    2    1    2
## [2,]    1    2    2
## [3,]    1    3    1
matriks2
##      [,1] [,2] [,3]
## [1,]    4    2    4
## [2,]    3    2    5
## [3,]    1    8    6

Juga bisa digunakan untuk menyelesaikan soal yang berkaitan dengan mencari nilai x, y, z.

Tentukan nilai x, y, dan z dari sistem persamaan linear tiga variabel:

10x-8y+z=65

4x-y+7z=48

x-y+2z=12

library(matlib)

coba1 <- matrix(c(10, 4, 1, -8, -1, -1, 1, 7, 2), 3, 3)

sam1 <- c(65, 48, 12)

#dimana X1 = X, X2 = y, X3 = Z
showEqn(coba1, sam1)
## 10*x1 - 8*x2 + 1*x3  =  65 
##  4*x1 - 1*x2 + 7*x3  =  48 
##  1*x1 - 1*x2 + 2*x3  =  12
#Nilai X  y  Z
solve(coba1, sam1)
## [1] 7 1 3
echelon(coba1, sam1, verbose = TRUE, fractions = TRUE)
## 
## Initial matrix:
##      [,1] [,2] [,3] [,4]
## [1,] 10   -8    1   65  
## [2,]  4   -1    7   48  
## [3,]  1   -1    2   12  
## 
## row: 1 
## 
##  multiply row 1 by 1/10 
##      [,1] [,2] [,3] [,4]
## [1,]    1 -4/5 1/10 13/2
## [2,]    4   -1    7   48
## [3,]    1   -1    2   12
## 
##  multiply row 1 by 4 and subtract from row 2 
##      [,1] [,2] [,3] [,4]
## [1,]    1 -4/5 1/10 13/2
## [2,]    0 11/5 33/5   22
## [3,]    1   -1    2   12
## 
##  subtract row 1 from row 3 
##      [,1]  [,2]  [,3]  [,4] 
## [1,]     1  -4/5  1/10  13/2
## [2,]     0  11/5  33/5    22
## [3,]     0  -1/5 19/10  11/2
## 
## row: 2 
## 
##  multiply row 2 by 5/11 
##      [,1]  [,2]  [,3]  [,4] 
## [1,]     1  -4/5  1/10  13/2
## [2,]     0     1     3    10
## [3,]     0  -1/5 19/10  11/2
## 
##  multiply row 2 by 4/5 and add to row 1 
##      [,1]  [,2]  [,3]  [,4] 
## [1,]     1     0   5/2  29/2
## [2,]     0     1     3    10
## [3,]     0  -1/5 19/10  11/2
## 
##  multiply row 2 by 1/5 and add to row 3 
##      [,1] [,2] [,3] [,4]
## [1,]    1    0  5/2 29/2
## [2,]    0    1    3   10
## [3,]    0    0  5/2 15/2
## 
## row: 3 
## 
##  multiply row 3 by 2/5 
##      [,1] [,2] [,3] [,4]
## [1,]    1    0  5/2 29/2
## [2,]    0    1    3   10
## [3,]    0    0    1    3
## 
##  multiply row 3 by 5/2 and subtract from row 1 
##      [,1] [,2] [,3] [,4]
## [1,]  1    0    0    7  
## [2,]  0    1    3   10  
## [3,]  0    0    1    3  
## 
##  multiply row 3 by 3 and subtract from row 2 
##      [,1] [,2] [,3] [,4]
## [1,] 1    0    0    7   
## [2,] 0    1    0    1   
## [3,] 0    0    1    3

Di rstudio juga bisa digunakan untuk mencari hasil operasi matriks dan juga untuk menunjukkan langkah-langkah penyelesaiannya.

#Perkalian Matriks
matriks1*matriks2
##      [,1] [,2] [,3]
## [1,]    8    2    8
## [2,]    3    4   10
## [3,]    1   24    6
echelon(matriks1*matriks2, verbose = TRUE, fractions = TRUE)
## 
## Initial matrix:
##      [,1] [,2] [,3]
## [1,]  8    2    8  
## [2,]  3    4   10  
## [3,]  1   24    6  
## 
## row: 1 
## 
##  multiply row 1 by 1/8 
##      [,1] [,2] [,3]
## [1,]   1  1/4    1 
## [2,]   3    4   10 
## [3,]   1   24    6 
## 
##  multiply row 1 by 3 and subtract from row 2 
##      [,1] [,2] [,3]
## [1,]    1  1/4    1
## [2,]    0 13/4    7
## [3,]    1   24    6
## 
##  subtract row 1 from row 3 
##      [,1] [,2] [,3]
## [1,]    1  1/4    1
## [2,]    0 13/4    7
## [3,]    0 95/4    5
## 
## row: 2 
## 
##  exchange rows 2 and 3 
##      [,1] [,2] [,3]
## [1,]    1  1/4    1
## [2,]    0 95/4    5
## [3,]    0 13/4    7
## 
##  multiply row 2 by 4/95 
##      [,1] [,2] [,3]
## [1,]    1  1/4    1
## [2,]    0    1 4/19
## [3,]    0 13/4    7
## 
##  multiply row 2 by 1/4 and subtract from row 1 
##      [,1]  [,2]  [,3] 
## [1,]     1     0 18/19
## [2,]     0     1  4/19
## [3,]     0  13/4     7
## 
##  multiply row 2 by 13/4 and subtract from row 3 
##      [,1]   [,2]   [,3]  
## [1,]      1      0  18/19
## [2,]      0      1   4/19
## [3,]      0      0 120/19
## 
## row: 3 
## 
##  multiply row 3 by 19/120 
##      [,1]  [,2]  [,3] 
## [1,]     1     0 18/19
## [2,]     0     1  4/19
## [3,]     0     0     1
## 
##  multiply row 3 by 18/19 and subtract from row 1 
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1 4/19
## [3,]    0    0    1
## 
##  multiply row 3 by 4/19 and subtract from row 2 
##      [,1] [,2] [,3]
## [1,] 1    0    0   
## [2,] 0    1    0   
## [3,] 0    0    1
#Matriks Transpose
t(matriks2)
##      [,1] [,2] [,3]
## [1,]    4    3    1
## [2,]    2    2    8
## [3,]    4    5    6
echelon(t(matriks2), verbose = TRUE, fractions = TRUE)
## 
## Initial matrix:
##      [,1] [,2] [,3]
## [1,] 4    3    1   
## [2,] 2    2    8   
## [3,] 4    5    6   
## 
## row: 1 
## 
##  multiply row 1 by 1/4 
##      [,1] [,2] [,3]
## [1,]   1  3/4  1/4 
## [2,]   2    2    8 
## [3,]   4    5    6 
## 
##  multiply row 1 by 2 and subtract from row 2 
##      [,1] [,2] [,3]
## [1,]    1  3/4  1/4
## [2,]    0  1/2 15/2
## [3,]    4    5    6
## 
##  multiply row 1 by 4 and subtract from row 3 
##      [,1] [,2] [,3]
## [1,]    1  3/4  1/4
## [2,]    0  1/2 15/2
## [3,]    0    2    5
## 
## row: 2 
## 
##  exchange rows 2 and 3 
##      [,1] [,2] [,3]
## [1,]    1  3/4  1/4
## [2,]    0    2    5
## [3,]    0  1/2 15/2
## 
##  multiply row 2 by 1/2 
##      [,1] [,2] [,3]
## [1,]    1  3/4  1/4
## [2,]    0    1  5/2
## [3,]    0  1/2 15/2
## 
##  multiply row 2 by 3/4 and subtract from row 1 
##      [,1]  [,2]  [,3] 
## [1,]     1     0 -13/8
## [2,]     0     1   5/2
## [3,]     0   1/2  15/2
## 
##  multiply row 2 by 1/2 and subtract from row 3 
##      [,1]  [,2]  [,3] 
## [1,]     1     0 -13/8
## [2,]     0     1   5/2
## [3,]     0     0  25/4
## 
## row: 3 
## 
##  multiply row 3 by 4/25 
##      [,1]  [,2]  [,3] 
## [1,]     1     0 -13/8
## [2,]     0     1   5/2
## [3,]     0     0     1
## 
##  multiply row 3 by 13/8 and add to row 1 
##      [,1] [,2] [,3]
## [1,]   1    0    0 
## [2,]   0    1  5/2 
## [3,]   0    0    1 
## 
##  multiply row 3 by 5/2 and subtract from row 2 
##      [,1] [,2] [,3]
## [1,] 1    0    0   
## [2,] 0    1    0   
## [3,] 0    0    1
#determinan matriks
det(matriks1)*matriks1
##      [,1] [,2] [,3]
## [1,]  -10   -5  -10
## [2,]   -5  -10  -10
## [3,]   -5  -15   -5
echelon(det(matriks1)*matriks1, verbose = TRUE, fractions = TRUE)
## 
## Initial matrix:
##      [,1] [,2] [,3]
## [1,] -10   -5  -10 
## [2,]  -5  -10  -10 
## [3,]  -5  -15   -5 
## 
## row: 1 
## 
##  multiply row 1 by -1/10 
##      [,1] [,2] [,3]
## [1,]   1  1/2    1 
## [2,]  -5  -10  -10 
## [3,]  -5  -15   -5 
## 
##  multiply row 1 by 5 and add to row 2 
##      [,1]  [,2]  [,3] 
## [1,]     1   1/2     1
## [2,]     0 -15/2    -5
## [3,]    -5   -15    -5
## 
##  multiply row 1 by 5 and add to row 3 
##      [,1]  [,2]  [,3] 
## [1,]     1   1/2     1
## [2,]     0 -15/2    -5
## [3,]     0 -25/2     0
## 
## row: 2 
## 
##  exchange rows 2 and 3 
##      [,1]  [,2]  [,3] 
## [1,]     1   1/2     1
## [2,]     0 -25/2     0
## [3,]     0 -15/2    -5
## 
##  multiply row 2 by -2/25 
##      [,1]  [,2]  [,3] 
## [1,]     1   1/2     1
## [2,]     0     1     0
## [3,]     0 -15/2    -5
## 
##  multiply row 2 by 1/2 and subtract from row 1 
##      [,1]  [,2]  [,3] 
## [1,]     1     0     1
## [2,]     0     1     0
## [3,]     0 -15/2    -5
## 
##  multiply row 2 by 15/2 and add to row 3 
##      [,1] [,2] [,3]
## [1,]  1    0    1  
## [2,]  0    1    0  
## [3,]  0    0   -5  
## 
## row: 3 
## 
##  multiply row 3 by -1/5 
##      [,1] [,2] [,3]
## [1,] 1    0    1   
## [2,] 0    1    0   
## [3,] 0    0    1   
## 
##  subtract row 3 from row 1 
##      [,1] [,2] [,3]
## [1,] 1    0    0   
## [2,] 0    1    0   
## [3,] 0    0    1
#invers matriks
det(matriks1)
## [1] -5
solve(matriks1)
##      [,1] [,2] [,3]
## [1,]  0.8   -1  0.4
## [2,] -0.2    0  0.4
## [3,] -0.2    1 -0.6
echelon(solve(matriks1), verbose = TRUE, fractions = TRUE)
## 
## Initial matrix:
##      [,1] [,2] [,3]
## [1,]  4/5   -1  2/5
## [2,] -1/5    0  2/5
## [3,] -1/5    1 -3/5
## 
## row: 1 
## 
##  multiply row 1 by 5/4 
##      [,1] [,2] [,3]
## [1,]    1 -5/4  1/2
## [2,] -1/5    0  2/5
## [3,] -1/5    1 -3/5
## 
##  multiply row 1 by 1/5 and add to row 2 
##      [,1] [,2] [,3]
## [1,]    1 -5/4  1/2
## [2,]    0 -1/4  1/2
## [3,] -1/5    1 -3/5
## 
##  multiply row 1 by 1/5 and add to row 3 
##      [,1] [,2] [,3]
## [1,]    1 -5/4  1/2
## [2,]    0 -1/4  1/2
## [3,]    0  3/4 -1/2
## 
## row: 2 
## 
##  exchange rows 2 and 3 
##      [,1] [,2] [,3]
## [1,]    1 -5/4  1/2
## [2,]    0  3/4 -1/2
## [3,]    0 -1/4  1/2
## 
##  multiply row 2 by 4/3 
##      [,1] [,2] [,3]
## [1,]    1 -5/4  1/2
## [2,]    0    1 -2/3
## [3,]    0 -1/4  1/2
## 
##  multiply row 2 by 5/4 and add to row 1 
##      [,1] [,2] [,3]
## [1,]    1    0 -1/3
## [2,]    0    1 -2/3
## [3,]    0 -1/4  1/2
## 
##  multiply row 2 by 1/4 and add to row 3 
##      [,1] [,2] [,3]
## [1,]    1    0 -1/3
## [2,]    0    1 -2/3
## [3,]    0    0  1/3
## 
## row: 3 
## 
##  multiply row 3 by 3 
##      [,1] [,2] [,3]
## [1,]    1    0 -1/3
## [2,]    0    1 -2/3
## [3,]    0    0    1
## 
##  multiply row 3 by 1/3 and add to row 1 
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1 -2/3
## [3,]    0    0    1
## 
##  multiply row 3 by 2/3 and add to row 2 
##      [,1] [,2] [,3]
## [1,] 1    0    0   
## [2,] 0    1    0   
## [3,] 0    0    1
#Perkalian antar matriks
matriks1%*%matriks2
##      [,1] [,2] [,3]
## [1,]   13   22   25
## [2,]   12   22   26
## [3,]   14   16   25
echelon(((1/2)*t(matriks2)-(1/4)*det(matriks1)), verbose = TRUE, fractions = TRUE)
## 
## Initial matrix:
##      [,1] [,2] [,3]
## [1,] 13/4 11/4  7/4
## [2,]  9/4  9/4 21/4
## [3,] 13/4 15/4 17/4
## 
## row: 1 
## 
##  multiply row 1 by 4/13 
##      [,1]  [,2]  [,3] 
## [1,]     1 11/13  7/13
## [2,]   9/4   9/4  21/4
## [3,]  13/4  15/4  17/4
## 
##  multiply row 1 by 9/4 and subtract from row 2 
##      [,1]   [,2]   [,3]  
## [1,]      1  11/13   7/13
## [2,]      0   9/26 105/26
## [3,]   13/4   15/4   17/4
## 
##  multiply row 1 by 13/4 and subtract from row 3 
##      [,1]   [,2]   [,3]  
## [1,]      1  11/13   7/13
## [2,]      0   9/26 105/26
## [3,]      0      1    5/2
## 
## row: 2 
## 
##  exchange rows 2 and 3 
##      [,1]   [,2]   [,3]  
## [1,]      1  11/13   7/13
## [2,]      0      1    5/2
## [3,]      0   9/26 105/26
## 
##  multiply row 2 by 11/13 and subtract from row 1 
##      [,1]   [,2]   [,3]  
## [1,]      1      0 -41/26
## [2,]      0      1    5/2
## [3,]      0   9/26 105/26
## 
##  multiply row 2 by 9/26 and subtract from row 3 
##      [,1]   [,2]   [,3]  
## [1,]      1      0 -41/26
## [2,]      0      1    5/2
## [3,]      0      0 165/52
## 
## row: 3 
## 
##  multiply row 3 by 52/165 
##      [,1]   [,2]   [,3]  
## [1,]      1      0 -41/26
## [2,]      0      1    5/2
## [3,]      0      0      1
## 
##  multiply row 3 by 41/26 and add to row 1 
##      [,1] [,2] [,3]
## [1,]   1    0    0 
## [2,]   0    1  5/2 
## [3,]   0    0    1 
## 
##  multiply row 3 by 5/2 and subtract from row 2 
##      [,1] [,2] [,3]
## [1,] 1    0    0   
## [2,] 0    1    0   
## [3,] 0    0    1
#Operasi matriks
(1/2)*t(matriks2)-(1/4)*det(matriks1)
##      [,1] [,2] [,3]
## [1,] 3.25 2.75 1.75
## [2,] 2.25 2.25 5.25
## [3,] 3.25 3.75 4.25
echelon(((1/2)*t(matriks2)-(1/4)*det(matriks1)), verbose = TRUE, fractions = TRUE)
## 
## Initial matrix:
##      [,1] [,2] [,3]
## [1,] 13/4 11/4  7/4
## [2,]  9/4  9/4 21/4
## [3,] 13/4 15/4 17/4
## 
## row: 1 
## 
##  multiply row 1 by 4/13 
##      [,1]  [,2]  [,3] 
## [1,]     1 11/13  7/13
## [2,]   9/4   9/4  21/4
## [3,]  13/4  15/4  17/4
## 
##  multiply row 1 by 9/4 and subtract from row 2 
##      [,1]   [,2]   [,3]  
## [1,]      1  11/13   7/13
## [2,]      0   9/26 105/26
## [3,]   13/4   15/4   17/4
## 
##  multiply row 1 by 13/4 and subtract from row 3 
##      [,1]   [,2]   [,3]  
## [1,]      1  11/13   7/13
## [2,]      0   9/26 105/26
## [3,]      0      1    5/2
## 
## row: 2 
## 
##  exchange rows 2 and 3 
##      [,1]   [,2]   [,3]  
## [1,]      1  11/13   7/13
## [2,]      0      1    5/2
## [3,]      0   9/26 105/26
## 
##  multiply row 2 by 11/13 and subtract from row 1 
##      [,1]   [,2]   [,3]  
## [1,]      1      0 -41/26
## [2,]      0      1    5/2
## [3,]      0   9/26 105/26
## 
##  multiply row 2 by 9/26 and subtract from row 3 
##      [,1]   [,2]   [,3]  
## [1,]      1      0 -41/26
## [2,]      0      1    5/2
## [3,]      0      0 165/52
## 
## row: 3 
## 
##  multiply row 3 by 52/165 
##      [,1]   [,2]   [,3]  
## [1,]      1      0 -41/26
## [2,]      0      1    5/2
## [3,]      0      0      1
## 
##  multiply row 3 by 41/26 and add to row 1 
##      [,1] [,2] [,3]
## [1,]   1    0    0 
## [2,]   0    1  5/2 
## [3,]   0    0    1 
## 
##  multiply row 3 by 5/2 and subtract from row 2 
##      [,1] [,2] [,3]
## [1,] 1    0    0   
## [2,] 0    1    0   
## [3,] 0    0    1