\[Solución~de~ejercicios\] \[Parte~1\\~ejercicios~1~al~10\]

#1#
3*4+6
## [1] 18
#2#
cos(5*pi/180)
## [1] 0.9961947
#3#
x<-4
3*sqrt(6+x)
## [1] 9.486833
#4#
y<-5.2

#5#
x <- 3
y <- 4
z <- (2*x) - (7*y)
z
## [1] -22
#6#.
??inv
#7#
A<-matrix(data=c(1,0,1,-2,0,5,2,0,2,2,3,1,-3,2,4,3),nrow=4,ncol=4)
A
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    2   -3
## [2,]    0    5    2    2
## [3,]    1    2    3    4
## [4,]   -2    0    1    3
#8#
B<-c(1,2,3,4)

#9#
C<-A%*%B
C
##      [,1]
## [1,]   -5
## [2,]   24
## [3,]   30
## [4,]   13
#10#
X<-matrix(data=c(5,1,2,3),nrow =2,ncol = 2)
X
##      [,1] [,2]
## [1,]    5    2
## [2,]    1    3
Y<-c(3,-1)
solve(X,Y)
## [1]  0.8461538 -0.6153846

\[Parte~2\\~ejercicios~del~1~al~27\]

#1.
w<-2*3+7

#2.
a<-4
b<--10
c<-3.2

#3.
y<-10
Y<-100
#Ambas variables son numerica, sin embargo, difieren dado que una es mayuscula y la otra minuscula, cualquier cambio en la escritura crea una variable nueva

#4.
X<-5.5
Y<--2.6
Z<-(2*X)-(3*Y)
Z
## [1] 18.8
#5.
W<-(3*Y)-Z+(X/Y)
W
## [1] -28.71538
#6.
r<-6.3
s<-5.8
final<-r+s-(r*s)
final
## [1] -24.44
#7.
this_is_the_result<-(r^2)-(s^2)
this_is_the_result
## [1] 6.05
#8
width<-1.5
Width<-2
WIDTH<-4.5
#las variables son completamente distintas 

#9.
#This line will no be executed.

#10.
s<-3.5 # esta es la variable s

#11.
y1<-7
y2<-9
y3<-y1-(y2/3)

#12.
# 2*m-5 , tengo primero que definir a m

#13.
cost<- 175
profit<-25
sale_price<-cost+profit

#14.
centigrade<-28
fahrenheit<- (centigrade*(9/5)+32)

#15.
format_short<-signif(14/9, digits=5)
format_short
## [1] 1.5556
format_long<-signif(14/9, digits = 17)

#16.
who<-ls()
who
##  [1] "a"                  "A"                  "b"                 
##  [4] "B"                  "c"                  "C"                 
##  [7] "centigrade"         "cost"               "fahrenheit"        
## [10] "final"              "format_long"        "format_short"      
## [13] "profit"             "r"                  "s"                 
## [16] "sale_price"         "this_is_the_result" "w"                 
## [19] "W"                  "width"              "Width"             
## [22] "WIDTH"              "x"                  "X"                 
## [25] "y"                  "Y"                  "y1"                
## [28] "y2"                 "y3"                 "z"                 
## [31] "Z"
#17.
whos<-ls.str()
whos
## a :  num 4
## A :  num [1:4, 1:4] 1 0 1 -2 0 5 2 0 2 2 ...
## b :  num -10
## B :  num [1:4] 1 2 3 4
## c :  num 3.2
## C :  num [1:4, 1] -5 24 30 13
## centigrade :  num 28
## cost :  num 175
## fahrenheit :  num 82.4
## final :  num -24.4
## format_long :  num 1.56
## format_short :  num 1.56
## profit :  num 25
## r :  num 6.3
## s :  num 3.5
## sale_price :  num 200
## this_is_the_result :  num 6.05
## w :  num 13
## W :  num -28.7
## who :  chr [1:31] "a" "A" "b" "B" "c" "C" "centigrade" "cost" "fahrenheit" ...
## width :  num 1.5
## Width :  num 2
## WIDTH :  num 4.5
## x :  num 3
## X :  num 5.5
## y :  num 10
## Y :  num -2.6
## y1 :  num 7
## y2 :  num 9
## y3 :  num 4
## z :  num -22
## Z :  num 18.8
#18.
#clear no found

#19.
Area<-5*7
Perimetro<-(2*5)+(2*7)
#20.
area_circulo<-pi*(6.45^2)
perimetro_circulo<-2*pi*6.45
#21.
x<-4/5
z<-14/17

#22.
y<-2*x-z
y
## [1] 0.7764706
#23.
radius<-pi*(2/3)^2
#24.
radius_sphere<-4*pi*(2/3)^2
#25.
double(radius)
## [1] 0
#26.
double(radius_sphere)
## [1] 0 0 0 0 0
#27.
y<-y
date<-date

\[parte~3\\ejercicios~del~1~al~25\]

#1.
sqrt(10)
## [1] 3.162278
#2
factorial(7)
## [1] 5040
#3
cos(45*180/pi)
## [1] -0.5918127
#4.
cos(45)
## [1] 0.525322
#5
sin(45)
## [1] 0.8509035
#6
tan(45)
## [1] 1.619775
#7
atan(1.5)
## [1] 0.9827937
#8
tan(3*pi/2)
## [1] 5.443746e+15
#9
exp(3)
## [1] 20.08554
#10
log(3.5)
## [1] 1.252763
#11
log10(3.5)
## [1] 0.544068
#12
round(2.43)
## [1] 2
#13
5%/%4
## [1] 1
#14
abs(-3.6)
## [1] 3.6
#15 al 17.
1.5-2*sqrt(6.7/5)
## [1] -0.8151674
sin(180)^2+ cos(180)^2
## [1] 1
log10(0)#no puede elevarse a cero 
## [1] -Inf
#18
l <- 3*180/2
k <- 2*180
2*sin(l)*cos(k)
## [1] 0.09988533
#19
h <- 45
#20
format(sqrt(45), digits = 3)
## [1] "6.71"
#21
s<-sin(45)
#22
c<-cos(45)
#23
t<-tan(45)
#24
e1<-exp(pi/2)
#24
e1
## [1] 4.810477

\[parte~4\]

#1
w<-c(2,4,-6,0)
#2
w[2]
## [1] 4
# 3 al 8
z<-function(x){(pi/2)*x}
z(w)
## [1]  3.141593  6.283185 -9.424778  0.000000
y<-z(w)
y[1:3]
## [1]  3.141593  6.283185 -9.424778
length(y)
## [1] 4
min(y)
## [1] -9.424778
max(y)
## [1] 6.283185
#9
r<-(2.5+c(1, 4, 7, 8))
#10
s<-c(2,4,6,8,10,12,14,16,18,20)
#11
v1<-c(9,3,-2,5,0)
v2<-c(1,2,-4)
vt<-c(v1,v2)
#12
vt2<-c(v1,4)
vt2
## [1]  9  3 -2  5  0  4
#13
va<-c(0.2,1.3,-3.5)
vb<-c(0.5,-2.5,1.0)
vab<-c(va,vb)
vab
## [1]  0.2  1.3 -3.5  0.5 -2.5  1.0
#14
vab[1:3]
## [1]  0.2  1.3 -3.5
vab[4:6]
## [1]  0.5 -2.5  1.0
#15
multiply<-va*vb
multiply
## [1]  0.10 -3.25 -3.50
#16
multiply2<-va[1:2]*vb[1:2]
#17
divide<-va/vb
#18
pot<-va%*%vb
pot
##       [,1]
## [1,] -6.65
#19
aa<-c(1,3,5)
ab<-c(3,6)
ac<-c(aa,ab)
ac
## [1] 1 3 5 3 6
#no se presento mensage de error
#20
aa<-ac[1:3]
#21
w<-c(0.1,1.3,-2.4)
w1<-c(w,5+w)
#22
w2<-w1-2-w
#23
w3<-w1*1.5*w
#24
w4<-w1*(w/10)
#25
w5<-w1*(3-2*(w/5))
#26
k<-pi
b<-c(0, pi/3, 2*pi/3, pi)
sin(b)
## [1] 0.000000e+00 8.660254e-01 8.660254e-01 1.224647e-16
cos(b)
## [1]  1.0  0.5 -0.5 -1.0
tan(b)
## [1]  0.000000e+00  1.732051e+00 -1.732051e+00 -1.224647e-16
#27
exp(b)
## [1]  1.000000  2.849654  8.120527 23.140693
#28
sqrt(b)
## [1] 0.000000 1.023327 1.447203 1.772454
#29
3^b
## [1]  1.000000  3.159659  9.983445 31.544281
#no hubo error porque lo toma como valores numérico en el vector b
#30
m3<-3^b[1]
#31 y 32
#1´s<- c(1,2,3,4) no permite esas abreviaciones

#33
v<-c(0.35,-1,0.24,1.3,-0.03)
sort(v)
## [1] -1.00 -0.03  0.24  0.35  1.30
sort(v, decreasing = TRUE)
## [1]  1.30  0.35  0.24 -0.03 -1.00
#35
vv<-c(2,4,-3,0,1,5,7)
mean(vv)
## [1] 2.285714
range(vv)
## [1] -3  7
median(vv)
## [1] 2
#36
x<-c("r","s","t","u","v")
#37 y 38
x1<-c(1,0,-2,3,5)
y<-c(x,x1)
y[3]
## [1] "t"
#39
#yy<-y*(2+3*y) no se logra dado que un vector contiene letras
#40 y 41
#x*y son el mismo calor que y*x

\[parte~5\]

#1
A<- matrix(data = c(3,1,0,3,-2,5),nrow = 2,ncol = 3)
#2
A[2,2]
## [1] 3
#3
B<- matrix(data = A*((3*pi)/2), nrow =2, ncol=3)
B
##           [,1]     [,2]      [,3]
## [1,] 14.137167  0.00000 -9.424778
## [2,]  4.712389 14.13717 23.561945
#4
B[1,3]
## [1] -9.424778
#5
c<-matrix(data = B[c(1:1,2:2)], nrow=1,ncol=1)
#6
dim(B)
## [1] 2 3
#7 y 8
length(B)
## [1] 6
#9
sum(B[,1])
## [1] 18.84956
sum(B[,2])
## [1] 14.13717
sum(B[,3])
## [1] 14.13717
min(B[,1])
## [1] 4.712389
max(B[,1])
## [1] 14.13717
#10

v3<- matrix(data = c(1,3,0,-4,5,3,1,0,2,2,-1,1),nrow=3, ncol=4)
v3
##      [,1] [,2] [,3] [,4]
## [1,]    1   -4    1    2
## [2,]    3    5    0   -1
## [3,]    0    3    2    1
#11
m1<- matrix(data=c(1,7,3,2,5,1,0,-3,1),nrow=3,ncol=3)
m1
##      [,1] [,2] [,3]
## [1,]    1    2    0
## [2,]    7    5   -3
## [3,]    3    1    1
m2<-matrix(data=c(1,3,2,3,5,3,-2,7,0),nrow=3,ncol=3)
m2
##      [,1] [,2] [,3]
## [1,]    1    3   -2
## [2,]    3    5    7
## [3,]    2    3    0
sum1<-m1+m2
subs1<-m1-m1

#12 al 14
mult<-m1*m2
divi<-m1/m2
#no se evidencian errores dado que la matriz es de la misma dimensióny es numerica
#15
x<-matrix(data = c(1,2,-3,5,-2,3,5,-2,0,6,2,4,1,2,1,4),nrow=4,ncol=4)
x1<-matrix(data = x+5,nrow=4,ncol=4)
x1
##      [,1] [,2] [,3] [,4]
## [1,]    6    3    5    6
## [2,]    7    8   11    7
## [3,]    2   10    7    6
## [4,]   10    3    9    9
#16
x2<-matrix(data = x-3,nrow=4,ncol=4)
x2
##      [,1] [,2] [,3] [,4]
## [1,]   -2   -5   -3   -2
## [2,]   -1    0    3   -1
## [3,]   -6    2   -1   -2
## [4,]    2   -5    1    1
#17
x3<-matrix(data = x*-3,nrow=4,ncol=4)
x3
##      [,1] [,2] [,3] [,4]
## [1,]   -3    6    0   -3
## [2,]   -6   -9  -18   -6
## [3,]    9  -15   -6   -3
## [4,]  -15    6  -12  -12
#18
x4<-matrix(data = x/2,nrow=4,ncol=4)
x4
##      [,1] [,2] [,3] [,4]
## [1,]  0.5 -1.0    0  0.5
## [2,]  1.0  1.5    3  1.0
## [3,] -1.5  2.5    1  0.5
## [4,]  2.5 -1.0    2  2.0
#19
x5<-matrix(data = -3*x/2.4+55,nrow=4,ncol=4)
x5
##       [,1]  [,2] [,3]  [,4]
## [1,] 53.75 57.50 55.0 53.75
## [2,] 52.50 51.25 47.5 52.50
## [3,] 58.75 48.75 52.5 53.75
## [4,] 48.75 57.50 50.0 50.00
#20
B<-matrix(data= c(pi/2, 2*pi/3, 2*pi/3, pi),nrow=2, ncol=2)
B
##          [,1]     [,2]
## [1,] 1.570796 2.094395
## [2,] 2.094395 3.141593
sin(B)
##           [,1]         [,2]
## [1,] 1.0000000 8.660254e-01
## [2,] 0.8660254 1.224647e-16
cos(B)
##               [,1] [,2]
## [1,]  6.123234e-17 -0.5
## [2,] -5.000000e-01 -1.0
tan(B)
##               [,1]          [,2]
## [1,]  1.633124e+16 -1.732051e+00
## [2,] -1.732051e+00 -1.224647e-16
#21
sqrt(B)
##          [,1]     [,2]
## [1,] 1.253314 1.447203
## [2,] 1.447203 1.772454
#22
#sqrtm(B)
#23
exp(B)
##          [,1]      [,2]
## [1,] 4.810477  8.120527
## [2,] 8.120527 23.140693
#24 ??expm

#expm(B)

#25
log(B)
##           [,1]      [,2]
## [1,] 0.4515827 0.7392648
## [2,] 0.7392648 1.1447299
#26
#logm(B)
#27 y 28
4^B
##           [,1]     [,2]
## [1,]  8.824978 18.23692
## [2,] 18.236920 77.88023
#29
B^4
##           [,1]     [,2]
## [1,]  6.088068 19.24130
## [2,] 19.241302 97.40909
#30
ls<-matrix(data= c(1,2,3,4,5,6),nrow=2,ncol=3)
ls
##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6
#31 y 32
os<-matrix(data= c(3,4,5,4,5,6),nrow=2,ncol=3)
os
##      [,1] [,2] [,3]
## [1,]    3    5    5
## [2,]    4    4    6
#33
ls<-matrix(data= c(1,2,3,4),nrow=2,ncol=2)
#34
os<-matrix(data= c(3,4,5,4),nrow=2,ncol=2)
#35
diag(nrow=2,ncol=2)
##      [,1] [,2]
## [1,]    1    0
## [2,]    0    1
#36
C<- matrix(data= c( 1,2,1,2,2,5,3,3,-3,2,7,-1,0,-3,-2,3),nrow=4,ncol=4)
t(C)
##      [,1] [,2] [,3] [,4]
## [1,]    1    2    1    2
## [2,]    2    5    3    3
## [3,]   -3    2    7   -1
## [4,]    0   -3   -2    3
#37
C+C # si es simetrica la matriz
##      [,1] [,2] [,3] [,4]
## [1,]    2    4   -6    0
## [2,]    4   10    4   -6
## [3,]    2    6   14   -4
## [4,]    4    6   -2    6
#38
diag(C)
## [1] 1 5 7 3
#39
qr(C)
## $qr
##            [,1]       [,2]       [,3]       [,4]
## [1,] -3.1622777 -6.6407831 -1.8973666  0.6324555
## [2,]  0.6324555 -1.7029386 -5.5198701  4.5803177
## [3,]  0.3162278  0.5426063 -5.3787577 -0.6475024
## [4,]  0.6324555 -0.6764480  0.5883922 -0.4488098
## 
## $rank
## [1] 4
## 
## $qraux
## [1] 1.3162278 1.4979925 1.8085757 0.4488098
## 
## $pivot
## [1] 1 2 3 4
## 
## attr(,"class")
## [1] "qr"
#40 da un valor escalar
det(C)
## [1] -13
#41
C2<-solve(C)

#42
C*C2#si se calcula la identidad de la matriz
##             [,1]        [,2] [,3]       [,4]
## [1,] -10.5384615  12.7692308   18  0.0000000
## [2,]  10.6153846 -15.3846154    6  3.2307692
## [3,]  -0.3076923   0.2307692    0 -0.1538462
## [4,]   3.2307692  -3.4615385   -1 -0.4615385
#43
norm(C)
## [1] 13
#44
eigen(C)
## eigen() decomposition
## $values
## [1]  8.19439889+0.000000i  3.93791318+2.661524i  3.93791318-2.661524i
## [4] -0.07022524+0.000000i
## 
## $vectors
##                [,1]                 [,2]                 [,3]           [,4]
## [1,]  0.24942346+0i 0.1235076+0.3224730i 0.1235076-0.3224730i  0.88639724+0i
## [2,] -0.41481971+0i 0.2577553+0.3723022i 0.2577553-0.3723022i -0.44089112+0i
## [3,] -0.87469708+0i 0.3369753-0.1771706i 0.3369753+0.1771706i  0.02228749+0i
## [4,]  0.02485078+0i 0.7285386+0.0000000i 0.7285386+0.0000000i -0.13934927+0i
#45 y 46
qr(C)
## $qr
##            [,1]       [,2]       [,3]       [,4]
## [1,] -3.1622777 -6.6407831 -1.8973666  0.6324555
## [2,]  0.6324555 -1.7029386 -5.5198701  4.5803177
## [3,]  0.3162278  0.5426063 -5.3787577 -0.6475024
## [4,]  0.6324555 -0.6764480  0.5883922 -0.4488098
## 
## $rank
## [1] 4
## 
## $qraux
## [1] 1.3162278 1.4979925 1.8085757 0.4488098
## 
## $pivot
## [1] 1 2 3 4
## 
## attr(,"class")
## [1] "qr"
#47
diag(5)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    0    0    0    0
## [2,]    0    1    0    0    0
## [3,]    0    0    1    0    0
## [4,]    0    0    0    1    0
## [5,]    0    0    0    0    1
#48
F1<-diag(7)
sum(F1[1:7,])
## [1] 7
sum(F1[,1:7])
## [1] 7
sum(diag(F1))
## [1] 7
diag(F1)
## [1] 1 1 1 1 1 1 1

\[parte~6\]

#1.
#Este es un comentario
cost <- 200
sale_price <- 250
profit <- sale_price-cost

#2. 
sphere_vol<- function(r){#funcion del volumen de la esfera
  3/4*pi*r}
sphere_vol(2)
## [1] 4.712389
#3. 

Rectangle_Area <- function(a,b){ #calculate the area of a regtangle   
  a*b}
Rectangle_Area(3,6)
## [1] 18
Rectangle_Area(2.5,5.5)
## [1] 13.75
#4. 
#Example9
x<-   c() 
for (i in 1:7){
  x[i]=i^3
  }
x
## [1]   1   8  27  64 125 216 343
#5.
#example 10

y<- matrix(nrow=4,ncol=4)
for (i in 1:4) {
  for (j in 1:4) {
    y[j,i] = i^2 - j^2
  }
  
}
y
##      [,1] [,2] [,3] [,4]
## [1,]    0    3    8   15
## [2,]   -3    0    5   12
## [3,]   -8   -5    0    7
## [4,]  -15  -12   -7    0
#6.
#Example 11
tol <- 0.0
n <- 3
while(tol <= 1.5){
  n=n + 1
  tol= tol+0.1
}
n
## [1] 18
tol
## [1] 1.5
#7.
precio <- function(items){
  if (items>5){
    price=items*160
  }  else {
    price=items*130
  }
  print(paste("PRECIO DADO:", price))
}
precio(3)
## [1] "PRECIO DADO: 390"
precio(9)
## [1] "PRECIO DADO: 1440"
#8.
precio2 <- function (items){
  if (items<5){
    if (items<3){
      price=items*130
  } else {
    price=items*160}
  } 
  else {
    price=items*200
  }
  print(paste("PRECIO DADO", price))
}
precio2(2)
## [1] "PRECIO DADO 260"
precio2(4)
## [1] "PRECIO DADO 640"
precio2(6)
## [1] "PRECIO DADO 1200"

\[Parte~7\]

#1 y 2
x=c(1,2,3,4,5,6,7)
y= c(10,15,23,43,30,10,12)
length(y)
## [1] 7
length(x)
## [1] 7
plot(x,y, main = "Grafico de vectores",xlab="independiente", ylab = "dependiente")

#3 y 4
x<- -6:6
y<- 2*x^3 + 5
plot(x,y, col="blue",pch=1)

#5
x1<- x <- seq(0,3*pi/2,length.out=10)
y1<- 2*sin(x/3)
z1<- 2*cos(x/3)

plot(x1,y1)

plot(x1, y1, type = "l",
 main = "Ambos",
 ylab = " dependientes",
 col = "red")
lines(x1, z1, col = "blue")
legend("topright", c("2*cos(x/3)", "2*sin(x/3)"), fill = c("blue", "red"))

#6
y<-2*x^3 - 4
z<- x+1
w<- 2-sqrt(x)
v<- x^2 + 3
x<-c(1,2,3,4,5,6,7,8,9,10)

plot(x,y)

plot(x,z)

plot(x,w)

plot(x,v)

#7
library(plot3D)
## Warning in fun(libname, pkgname): couldn't connect to display ":0"
z_valores<-function(x,y){
  2*sin(x*y)}
x<-c(1,2,3,4,5,6,7,8,9,10)
y<-c(1,2,3,4,5,6,7,8,9,10)

z<-outer(x,y,z_valores)
persp(x,y,z)

#8
m<-matrix(data= c(0.1,0.2,0.5,0.4,0.1,0.2,0.8,0.9,0.4,0.3,0.5,0.5,0.9,0.5,0.4,0.7,0.6,0.4,0.7,0.6,0.3,0.3,0.4,0.9,0.8),nrow=5,ncol=5)
m
##      [,1] [,2] [,3] [,4] [,5]
## [1,]  0.1  0.2  0.5  0.7  0.3
## [2,]  0.2  0.8  0.5  0.6  0.3
## [3,]  0.5  0.9  0.9  0.4  0.4
## [4,]  0.4  0.4  0.5  0.7  0.9
## [5,]  0.1  0.3  0.4  0.6  0.8
library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout

fig <- plot_ly(z = ~m)
fig <- fig %>% add_surface()

fig

```