I parte:Introducción a R
Actividad 1: Explicación de cómo se utiliza R(Rscript, consola,funciones, limpiar consola (ctr+l).
Actividad 2: Ingresar los vectores \(y=(1,2,0,4,10,-1), x=(1,2,3,4,5)\)
y<-c(1,2,0,4,10,-1)
x<-1:5
print(x) # Visualiza el vector x
## [1] 1 2 3 4 5
print(y) # Visualiza el vector y
## [1] 1 2 0 4 10 -1
Actividad 3: Para ingresar el vector v= ( 1, 3, 5, 7, 9, 11, 13, 15), se puede utilizar la función seq(a,b,by=n) donde a es el valor inicial, b el valor final y n el valor de separación entre los números del intervalo \([a,b]\).
v<- seq(1,15,by=2)
Actividad 4: Considerando el vector \(u=(11,11,11,11,11,11,11)\), se utiliza la función rep(n, r), donde n es el número de la entrada que se repite y r es la cantidad de veces que se repite n
Actividad 5: Se puede introducir un vector con base a otros vectores ingresados. Considerando los ejemplos anteriores se puede crear el vector \(m=( 1, 3, 5, 7, 9, 11, 13, 15,11,11,11,11,11,11,11)\).
v<- seq(1,15,by=2)
u<-rep(11, 7)
m<-c(v,u)
Actividad 6: Considerando el vector \(m\), obtener el valor del vector en la posición 5 y crear un vector \(p\) de la posición de 5 al 10.
Actividad 7: Borrar datos en R (rm(nombre del vector)).
Ejercicios:
Ingresar los siguientes vectores en R:
\((3,6,9,12,15,18,21,24,27,30,33,36)\)
\((15,16,17,18,20,21,22,23,24,25,26)\)
\((1,1,1,1,1,1,1)\)
\((1,1,1,1,1,1,1,3,6,9,12,15,18)\)
Actividad 8: Instalación de paquetes en R.
Introducción a RStudio
Actividad 9: Explicación de las utilidades de RStudio (consola, Rscript, paquetes, ayuda, datos).
Actividad 10: Gráficar los puntos (1,1),(2,4),(3,6),(4,8),(5,25),(6,36),(7,49),(8,61),(9,81),(10,100) utilizando la función plot.
x<-c(1,2,3,4,5,6,7,8,9,10)
y<-c(1,4,9,16,25,36,49,64,81,100)
plot(x,y)
Actividad 11. Instalación del paquete matlib desde RStudio.
Actividad 12:Ingresar las matrices
\[ A= \left( \begin{array}{ccc} 1 & 5 & 9 \\ 2 & 6 & 10 \\ 3 & 7 & 11 \\ 4 & 8 & 12 \\ \end{array} \right) \qquad B= \left( \begin{array}{cccc} 0 & 3 & 1 &0 \\ 2 & 4 & 5 &0\\ 0 & 3 & 1 & 1\\ \end{array} \right) \qquad I= \left( \begin{array}{ccc} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \\ \end{array} \right) \qquad O= \left( \begin{array}{cc} 0 & 0 \\ 0 & 0 \\ \end{array} \right) \]
A<-matrix(c(1:12), nrow = 4 , ncol = 3)
B<-matrix(c(0,2,0,3,4,3,1,5,1,0,0,1),nrow = 3,ncol= 4)
I<- diag(3)
O<- matrix(c(rep(0,4)), ncol=2, nrow=2)
# Otra forma para ingresar la matriz nula
library(matlib)
O<-J(2,2,constant=0) # se puede cambiar la constante.
OO<-J(3,2,constant=1)
Actividad 13: Realizar las siguientes operaciones \(A+A, AB, 8B,C^t ,|C|, A-8A\).
A<-matrix(c(1:12), nrow = 4 , ncol = 3)
B<-matrix(c(0,2,0,3,4,3,1,5,1,0,0,1),nrow = 3,ncol= 4)
I<- diag(3)
O<- matrix(c(rep(0,4)), ncol=2, nrow=2)
A+A # Suma de matrices.
## [,1] [,2] [,3]
## [1,] 2 10 18
## [2,] 4 12 20
## [3,] 6 14 22
## [4,] 8 16 24
A%*%B # Producto AB.
## [,1] [,2] [,3] [,4]
## [1,] 10 50 35 9
## [2,] 12 60 42 10
## [3,] 14 70 49 11
## [4,] 16 80 56 12
8*B # Producto de una constante a una matriz.
## [,1] [,2] [,3] [,4]
## [1,] 0 24 8 0
## [2,] 16 32 40 0
## [3,] 0 24 8 8
t(O) # Transpuesta de la matriz C.
## [,1] [,2]
## [1,] 0 0
## [2,] 0 0
det(O) # Determinante matriz C.
## [1] 0
A-8*A # Resta de matrices.
## [,1] [,2] [,3]
## [1,] -7 -35 -63
## [2,] -14 -42 -70
## [3,] -21 -49 -77
## [4,] -28 -56 -84
Actividad 14: Utilizar la función diag y modificar las entradas para ingresar la matriz \(D\). \[ D=\left( \begin{array}{ccc} 3 & 0 & 0 \\ 0 & 7 & 0 \\ 0 & 0 & 9 \\ \end{array} \right) \]
Ejercicio: Determinar \(B= (X X^t), C=-X^t Y\)
\[ X=\left( \begin{array}{cc} 1 & -2 \\ 1 & -1 \\ 1 & 0 \\ 1 & 1 \\ 1 & 2 \\ \end{array} \right) \qquad Y=\left( \begin{array}{c} 0 \\ 0 \\ 1 \\ 1 \\ 3 \\ \end{array} \right) \] II parte: Utilización de matlib
\[ P= \left( \begin{array}{ccccc} 1 & 2 & 3 & 1 & 2 \\ 2 & 4 & 6 & 1 & 2 \\ 3 & 6 & 9 & 1 & 2 \\ 4 & 8 & 12 & 1 & 2 \\ 5 & 10 & 15 & 1 & 2 \\ 6 & 12 & 18 & 1 & 2 \\ 7 & 14 & 21 & 1 & 2 \\ 8 & 16 & 24 & 1 & 2 \\ 9 & 18 & 28 & 1 & 2 \\ \end{array} \right) \] Procedimiento:
Creación de archivo M.csv separado por comas en Excel.
Importación de datos desde RStudio.
Utilizar el comando P<-as.matrix(M).
Actividad 2: Instalación del paquete matlib.
Actividad 3: Determinar la matriz inversa de L y B y el procedimiento.
\[ L= \left( \begin{array}{ccc} 1 & 2 & -4 \\ -1 & -1 & 5\\ 2 & 7 & -3\\ \end{array} \right) \]
Actividad 4: Hacer el procedimiento anterior con verbose=FALSE y latex=TRUE para encontrar la inversa de la matriz de N.
\[ N= \left( \begin{array}{ccc} 2 & 0 & 0 \\ 4 & 3 & 0 \\ 6 & 2 & 1 \\ \end{array} \right) \]
N<-matrix(c(2,4,6,0,3,2,0,0,1),ncol=3,nrow = 3)
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
library(matlib)
Inverse(N,verbose=FALSE, fractions=TRUE, latex=TRUE)
## [,1] [,2] [,3]
## [1,] 0.5000000 0.0000000 0
## [2,] -0.6666667 0.3333333 0
## [3,] -1.6666667 -0.6666667 1
Actividad 5: Sistemas de ecuaciones lineales
\[ \left\lbrace \begin{array}{lll} x& + 5y & =7\\ -2x & -7y & =-5\\ \end{array} \right. \] * Ingresar sistemas de ecuaciones lineales.
A<-matrix(c(1,-2,5,-7), ncol = 2, nrow = 2)
b<- c(7,-5)
showEqn(A, b)
## 1*x1 + 5*x2 = 7
## -2*x1 - 7*x2 = -5
library(matlib)
A<-matrix(c(1,-2,5,-7), ncol = 2, nrow = 2)
b<- c(7,-5)
plotEqn(A,b, xlim=c(-10,0), labels=TRUE)
## x1 + 5*x2 = 7
## -2*x1 - 7*x2 = -5
A<-matrix(c(1,-2,5,-7), ncol = 2, nrow = 2)
b<- c(7,-5)
library(matlib)
Solve(A,b,verbose = TRUE, fractions = TRUE)
##
## Initial matrix:
## [,1] [,2] [,3]
## [1,] 1 5 7
## [2,] -2 -7 -5
##
## row: 1
##
## exchange rows 1 and 2
## [,1] [,2] [,3]
## [1,] -2 -7 -5
## [2,] 1 5 7
##
## multiply row 1 by -1/2
## [,1] [,2] [,3]
## [1,] 1 7/2 5/2
## [2,] 1 5 7
##
## subtract row 1 from row 2
## [,1] [,2] [,3]
## [1,] 1 7/2 5/2
## [2,] 0 3/2 9/2
##
## row: 2
##
## multiply row 2 by 2/3
## [,1] [,2] [,3]
## [1,] 1 7/2 5/2
## [2,] 0 1 3
##
## multiply row 2 by 7/2 and subtract from row 1
## [,1] [,2] [,3]
## [1,] 1 0 -8
## [2,] 0 1 3
## x1 = -8
## x2 = 3
Actividad 6: Gráficar el siguiente sistema \[ \left\lbrace \begin{array}{llllll} x& + & y & +& 2z & = 9\\ 2x & + & 4y &-&3z & = 0\\ 3x & +& 6y & -&5z &= 1\\ \end{array} \right. \]
B<-matrix(c(1,2,3,1,4,6,2,-3,-5),ncol =3,nrow= 3)
c<- c(9,0,1)
library(matlib)
plotEqn3d(B,c)
Actividad 7: Reducción Gaussiana
B<-matrix(c(1,2,3,1,4,6,2,-3,-5),ncol =3,nrow= 3)
c<- c(9,0,1)
library(matlib)
gaussianElimination(B,c,fractions = TRUE, verbose = TRUE)
##
## Initial matrix:
## [,1] [,2] [,3] [,4]
## [1,] 1 1 2 9
## [2,] 2 4 -3 0
## [3,] 3 6 -5 1
##
## row: 1
##
## exchange rows 1 and 3
## [,1] [,2] [,3] [,4]
## [1,] 3 6 -5 1
## [2,] 2 4 -3 0
## [3,] 1 1 2 9
##
## multiply row 1 by 1/3
## [,1] [,2] [,3] [,4]
## [1,] 1 2 -5/3 1/3
## [2,] 2 4 -3 0
## [3,] 1 1 2 9
##
## multiply row 1 by 2 and subtract from row 2
## [,1] [,2] [,3] [,4]
## [1,] 1 2 -5/3 1/3
## [2,] 0 0 1/3 -2/3
## [3,] 1 1 2 9
##
## subtract row 1 from row 3
## [,1] [,2] [,3] [,4]
## [1,] 1 2 -5/3 1/3
## [2,] 0 0 1/3 -2/3
## [3,] 0 -1 11/3 26/3
##
## row: 2
##
## exchange rows 2 and 3
## [,1] [,2] [,3] [,4]
## [1,] 1 2 -5/3 1/3
## [2,] 0 -1 11/3 26/3
## [3,] 0 0 1/3 -2/3
##
## multiply row 2 by -1
## [,1] [,2] [,3] [,4]
## [1,] 1 2 -5/3 1/3
## [2,] 0 1 -11/3 -26/3
## [3,] 0 0 1/3 -2/3
##
## multiply row 2 by 2 and subtract from row 1
## [,1] [,2] [,3] [,4]
## [1,] 1 0 17/3 53/3
## [2,] 0 1 -11/3 -26/3
## [3,] 0 0 1/3 -2/3
##
## row: 3
##
## multiply row 3 by 3
## [,1] [,2] [,3] [,4]
## [1,] 1 0 17/3 53/3
## [2,] 0 1 -11/3 -26/3
## [3,] 0 0 1 -2
##
## multiply row 3 by 17/3 and subtract from row 1
## [,1] [,2] [,3] [,4]
## [1,] 1 0 0 29
## [2,] 0 1 -11/3 -26/3
## [3,] 0 0 1 -2
##
## multiply row 3 by 11/3 and add to row 2
## [,1] [,2] [,3] [,4]
## [1,] 1 0 0 29
## [2,] 0 1 0 -16
## [3,] 0 0 1 -2
Actividad 8: Encontrar el determinante de la matriz por medio de la definición desarrollandola en la primera fila.
\[ B= \left( \begin{array}{ccc} 1 & 1 & 2 \\ 2 & 4 & -3 \\ 3 & 6 & -5 \\ \end{array} \right) \]
B<-matrix(c(1,2,3,1,4,6,2,-3,-5),ncol =3,nrow= 3)
library(matlib)
DET =( minor(B,1,1) #Suprime la fila 1 y columna 1
- minor(B,1,2) #Suprime la fila 1 y columna 2
+2*minor(B,1,3)) #Suprime la fila 1 y columna 2
det(B)
## [1] -1
En la siguiente dirección Web, puedes encontrar el documento del taller https://sites.google.com/view/talleresalr/p%C3%A1gina-principal
Referencias utiles
Friendly, M., Fox, J., Monette, G., Sanchez, G. y Chalmers, P. (2018) matlib: Matrix Functions for Teaching and Learning Linear Algebra and Multivariate Statistics.