Nama = Izza Syahri Muharram || Nim = 220605110073 || Mata kuliah = Linear Algebra || Dosen Pengampu = Prof. Dr. SUHARTONO, M.Kom || Teknik Informatika || Universitas Islam Negeri Malang
library(matlib)
library(ISLR)
R menyediakan fungsi bawaan (solve()) untuk menyelesaikan sistem persamaan linear. format fungsi solve sebagai berikut
A <- matrix(c(1,2,1,1), nrow = 2, ncol = 2)
b <- c(4, 5)
A
## [,1] [,2]
## [1,] 1 1
## [2,] 2 1
b
## [1] 4 5
jika kita hanya memasukkan matriks persegi, maka output yang akan dihasilkan adalah invers dari matriks yang kita masukan jika kita mengalikan invers dengan matriks semula maka akan dihasilkan output berupa matriks identitas.
solve(A, b)
## [1] 1 3
Berikut adalah visualisasi geometri dari sistem persamaan linier pada Contoh 41 dari paket matlib di R. Hal yang menyenangkan tentang paket ini adalah Anda dapat memplot geometri dari sistem persamaan linier. Jika Anda mengetik
plotEqn(A,b)
## x[1] + x[2] = 4
## 2*x[1] + x[2] = 5
Kemudian R menampilkan gambar yang ditunjukkan pada Gambar 1.6. Dalam contoh ini keduanya garis yang didefinisikan oleh dua persamaan berpotongan di satu titik (1, 3), yang merupakan lingkaran pada gambar.
A <- matrix(c(0, -2, 1, 2, 3, 2, 3, -2, 3), nrow = 3, ncol = 3)
b <- c (6,-1,2)
A
## [,1] [,2] [,3]
## [1,] 0 2 3
## [2,] -2 3 -2
## [3,] 1 2 3
b
## [1] 6 -1 2
Solve(A,b)
## x1 = -4
## x2 = -1.15384615
## x3 = 2.76923077
Berikut adalah visualisasi geometri dari sistem persamaan linier pada Contoh 42 dari paket matlib di R. Dalam argumen fungsi, “xlim=” digunakan untuk mengatur rentang dari sumbu x dan “ylim” digunakan untuk mengatur rentang sumbu y. Kemudian keluaran R gambar yang ditunjukkan pada Gambar 1.7. Ini interaktif. Anda menahan tombol dari mouse dan putar gambar, sehingga Anda dapat melihat bagaimana persamaan liniernya berpotongan satu sama lain. Dalam contoh ini, tiga bidang linear didefinisikan oleh tiga persamaan berpot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0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 ,
0 , 0 , 0 , 0 , 0 , 1), nrow = 11, ncol = 18, byrow = TRUE)
b <- c(1298, 1948, 465, 605, 451, 338, 260, 183, 282, 127, 535)
Solve(A, b)
## x1 - 1*x11 - 1*x12 - 1*x13 - 1*x14 - 1*x15 - 1*x16 - 1*x17 - 1*x18 = -1483
## x2 + x11 = 605
## x3 + x12 = 451
## x4 + x13 = 338
## x5 + x14 = 260
## x6 + x15 = 183
## x7 + x16 = 282
## x8 + x17 = 127
## x9 + x18 = 535
## x10 + x11 + x12 + x13 + x14 + x15 + x16 + x17 + x18 = 1948
## 0 = 0
Dalam argumen fungsi matrix() " mendefinisikan urutan dari entri baris demi baris. Ini berarti elemen matriks diisi dari baris pertama dan ke baris kedua dan seterusnya. Sekarang kita menulis sisi kanan di R Ini berarti bahwa ada tak terhingga banyak solusi untuk sistem linier persamaan.
// // Di sini, kami tidak mengetik “nrow=” dan “ncol=”. Jika sudah jelas bisa kita lewati mengetik mereka. Dengan menggunakan fungsi showEqn() kita dapat melihat sistem persamaan linear
A <- matrix(c(0, -1, 1, 0, 1, 1, 0, 1, 3, -4, 2, 0, -1, 0, 4, -4), 4, 4)
b <- c(1, 1, 5, -2)
showEqn(A, b)
## 0*x1 + 1*x2 + 3*x3 - 1*x4 = 1
## -1*x1 + 1*x2 - 4*x3 + 0*x4 = 1
## 1*x1 + 0*x2 + 2*x3 + 4*x4 = 5
## 0*x1 + 1*x2 + 0*x3 - 4*x4 = -2
Sekarang dengan menggunakan fungsi eselon() kita dapat melihat bagaimana Penghapusan Gaussian bekerja. Dengan contoh ini, kita dapat mengetikkan R sebagai: Sistem Persamaan Linier dan Matriks
echelon(A, b, verbose=TRUE, fractions=TRUE)
##
## Initial matrix:
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0 1 3 -1 1
## [2,] -1 1 -4 0 1
## [3,] 1 0 2 4 5
## [4,] 0 1 0 -4 -2
##
## row: 1
##
## exchange rows 1 and 2
## [,1] [,2] [,3] [,4] [,5]
## [1,] -1 1 -4 0 1
## [2,] 0 1 3 -1 1
## [3,] 1 0 2 4 5
## [4,] 0 1 0 -4 -2
##
## multiply row 1 by -1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 -1 4 0 -1
## [2,] 0 1 3 -1 1
## [3,] 1 0 2 4 5
## [4,] 0 1 0 -4 -2
##
## subtract row 1 from row 3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 -1 4 0 -1
## [2,] 0 1 3 -1 1
## [3,] 0 1 -2 4 6
## [4,] 0 1 0 -4 -2
##
## row: 2
##
## multiply row 2 by 1 and add to row 1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 0 7 -1 0
## [2,] 0 1 3 -1 1
## [3,] 0 1 -2 4 6
## [4,] 0 1 0 -4 -2
##
## subtract row 2 from row 3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 0 7 -1 0
## [2,] 0 1 3 -1 1
## [3,] 0 0 -5 5 5
## [4,] 0 1 0 -4 -2
##
## subtract row 2 from row 4
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 0 7 -1 0
## [2,] 0 1 3 -1 1
## [3,] 0 0 -5 5 5
## [4,] 0 0 -3 -3 -3
##
## row: 3
##
## multiply row 3 by -1/5
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 0 7 -1 0
## [2,] 0 1 3 -1 1
## [3,] 0 0 1 -1 -1
## [4,] 0 0 -3 -3 -3
##
## multiply row 3 by 7 and subtract from row 1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 0 0 6 7
## [2,] 0 1 3 -1 1
## [3,] 0 0 1 -1 -1
## [4,] 0 0 -3 -3 -3
##
## multiply row 3 by 3 and subtract from row 2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 0 0 6 7
## [2,] 0 1 0 2 4
## [3,] 0 0 1 -1 -1
## [4,] 0 0 -3 -3 -3
##
## multiply row 3 by 3 and add to row 4
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 0 0 6 7
## [2,] 0 1 0 2 4
## [3,] 0 0 1 -1 -1
## [4,] 0 0 0 -6 -6
##
## row: 4
##
## multiply row 4 by -1/6
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 0 0 6 7
## [2,] 0 1 0 2 4
## [3,] 0 0 1 -1 -1
## [4,] 0 0 0 1 1
##
## multiply row 4 by 6 and subtract from row 1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 0 0 0 1
## [2,] 0 1 0 2 4
## [3,] 0 0 1 -1 -1
## [4,] 0 0 0 1 1
##
## multiply row 4 by 2 and subtract from row 2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 0 0 0 1
## [2,] 0 1 0 0 2
## [3,] 0 0 1 -1 -1
## [4,] 0 0 0 1 1
##
## multiply row 4 by 1 and add to row 3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 0 0 0 1
## [2,] 0 1 0 0 2
## [3,] 0 0 1 0 0
## [4,] 0 0 0 1 1
A <- matrix(c(0, -9, 4, 4, 5, 6, 3, 8, 4, 6, 0, 4, 7, 7, 5, 12), 4, 4)
b <- c(-9, -17, -3, 0)
showEqn(A, b)
## 0*x1 + 5*x2 + 4*x3 + 7*x4 = -9
## -9*x1 + 6*x2 + 6*x3 + 7*x4 = -17
## 4*x1 + 3*x2 + 0*x3 + 5*x4 = -3
## 4*x1 + 8*x2 + 4*x3 + 12*x4 = 0
Kami menggunakan paket matlib untuk contoh ini. Kami membuat matriks koefisien dan vektor ruas kanan persamaan sistem. Kemudian kita punya
A <- matrix(c(0, -9, 4, 4, 5, 6, 3, 8, 4, 6, 0, 4, 7, 7, 5, 12), 4, 4)
b <- c(-9, -17, -3, -12)
Dengan menggunakan fungsi showEqn() kita dapat melihat sistem persamaan linear.
showEqn(A, b)
## 0*x1 + 5*x2 + 4*x3 + 7*x4 = -9
## -9*x1 + 6*x2 + 6*x3 + 7*x4 = -17
## 4*x1 + 3*x2 + 0*x3 + 5*x4 = -3
## 4*x1 + 8*x2 + 4*x3 + 12*x4 = -12