Ejemplo 1: Gráficar el siguiente sistema de ecuaciones lineales. \[ \left\{ \begin{array}{ccccc} x&-&2y & = & -1 \\ -x&+&3y & = & 3 \end{array} \right. \]
library(matlib)
A=matrix(c(1,-2,-1,3),ncol = 2,byrow = TRUE)
b=c(-1,3)
plotEqn(A,b,var=c("x","y"),solution = TRUE)
## x - 2*y = -1
## -x + 3*y = 3
Ejemplo 2: Encontrar la matriz inversa de \(N\). \[ N=\left( \begin{array}{ccc} 2 & 0 & 0 \\ 4 & 3 & 0 \\ 6 & 2 & 1 \\ \end{array} \right) \]
Si queremos solo la respuesta, se utilizara la función \(solve(N)\).
N=matrix(c(2,0,0,4,3,0,6,2,1),ncol = 3,byrow = TRUE)
solve(N)
## [,1] [,2] [,3]
## [1,] 0.5000000 1.850372e-17 0
## [2,] -0.6666667 3.333333e-01 0
## [3,] -1.6666667 -6.666667e-01 1
Para efectos educativos, se quiere que las entradas de la matriz \(N^{-1}\) sean dadas en forma fraccionaria. Para ello utilizamos la ayuda de la función \(fractions()\) del paquete \(MASS\).
library(MASS)
fractions(solve(N))
## [,1] [,2] [,3]
## [1,] 1/2 0 0
## [2,] -2/3 1/3 0
## [3,] -5/3 -2/3 1
Para optener el procedimiento para calcular la inversa de la matriz, se utiliza el paquete \(matlib\).
library(matlib)
N=matrix(c(2,0,0,4,3,0,6,2,1),ncol = 3,byrow = TRUE)
Inverse(N,verbose=TRUE,fractions=TRUE)
##
## Initial matrix:
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 2 0 0 1 0 0
## [2,] 4 3 0 0 1 0
## [3,] 6 2 1 0 0 1
##
## row: 1
##
## exchange rows 1 and 3
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 6 2 1 0 0 1
## [2,] 4 3 0 0 1 0
## [3,] 2 0 0 1 0 0
##
## multiply row 1 by 1/6
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 1/3 1/6 0 0 1/6
## [2,] 4 3 0 0 1 0
## [3,] 2 0 0 1 0 0
##
## multiply row 1 by 4 and subtract from row 2
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 1/3 1/6 0 0 1/6
## [2,] 0 5/3 -2/3 0 1 -2/3
## [3,] 2 0 0 1 0 0
##
## multiply row 1 by 2 and subtract from row 3
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 1/3 1/6 0 0 1/6
## [2,] 0 5/3 -2/3 0 1 -2/3
## [3,] 0 -2/3 -1/3 1 0 -1/3
##
## row: 2
##
## multiply row 2 by 3/5
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 1/3 1/6 0 0 1/6
## [2,] 0 1 -2/5 0 3/5 -2/5
## [3,] 0 -2/3 -1/3 1 0 -1/3
##
## multiply row 2 by 1/3 and subtract from row 1
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 0 3/10 0 -1/5 3/10
## [2,] 0 1 -2/5 0 3/5 -2/5
## [3,] 0 -2/3 -1/3 1 0 -1/3
##
## multiply row 2 by 2/3 and add to row 3
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 0 3/10 0 -1/5 3/10
## [2,] 0 1 -2/5 0 3/5 -2/5
## [3,] 0 0 -3/5 1 2/5 -3/5
##
## row: 3
##
## multiply row 3 by -5/3
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 0 3/10 0 -1/5 3/10
## [2,] 0 1 -2/5 0 3/5 -2/5
## [3,] 0 0 1 -5/3 -2/3 1
##
## multiply row 3 by 3/10 and subtract from row 1
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 0 0 1/2 0 0
## [2,] 0 1 -2/5 0 3/5 -2/5
## [3,] 0 0 1 -5/3 -2/3 1
##
## multiply row 3 by 2/5 and add to row 2
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 0 0 1/2 0 0
## [2,] 0 1 0 -2/3 1/3 0
## [3,] 0 0 1 -5/3 -2/3 1
## [,1] [,2] [,3]
## [1,] 0.5000000 0.0000000 0
## [2,] -0.6666667 0.3333333 0
## [3,] -1.6666667 -0.6666667 1
Ejemplo 3: Ingresar la matriz \(A\) y calcular \(A^t A-x^2 I_3\) con \(I_3\) la matriz identidad.
\[ A=\left( \begin{array}{ccc} 0 & 1 & x \\ 1 & x & 0 \\ 1 & 2 & x \\ \end{array} \right) \]
library(rSymPy)
## Loading required package: rJython
## Loading required package: rJava
##
## Attaching package: 'rJava'
## The following object is masked from 'package:matlib':
##
## J
## Loading required package: rjson
sympyStart()
x=Var("x") #Definir variable x
sympy("A = Matrix([[0,1,x], [1,x,0],[1,2,x]])")
## [1] "[0, 1, x]\n[1, x, 0]\n[1, 2, x]"
# Para visualizar de mejor manera la matriz se utiliza la función cat().
cat(sympy("A = Matrix([[0,1,x], [1,x,0],[1,2,x]])"))
## [0, 1, x]
## [1, x, 0]
## [1, 2, x]
# Respuesta
cat(sympy("A.T*A-x**2*eye(3)"))
## [2 - x**2, 2 + x, x]
## [ 2 + x, 5, 3*x]
## [ x, 3*x, x**2]
Para utilizar exponentes se ingresan dos símbolos de asteriscos, como ejemplo \(x**2\) es para indicar \(x^2\).
En este enlace puesdes ver trabajos del tema: https://sites.google.com/view/talleresalr/p%C3%A1gina-principal Referencias utiles
Friendly, M., Fox, J., Chalmers, P., Monette, G. & Sanchez, G. (2018). matlib: Matrix Functions for Teaching and Learning Linear Algebra and Multivariate Statistics. Recuperado de https://cran.rproject.org/web/packages/matlib/index.html G Grothendieck .(2012). rSymPy: R interface to SymPy computer algebra system. Recuperado de https://cran.r-project.org/web/packages/rSymPy/index.html
SymPy Development Team (2016). SymPy. Recuperado de http://www.sympy.org/es/ Ripley, B., Venables, B., Bates,M., Hornik, K., Gebhardt,A. & Firth.D.(2018). MASS: Support Functions and Datasets for Venables and Ripley’s MASS. Recuperado de https://cran.rproject.org/web/packages/MASS/index.html