Exercícios


Seção 11.1


Item a

myfib4 <- function(thresh, printme){
  if(printme == TRUE){  
    fib.a <- 1
    fib.b <- 1
    cat(fib.a,fib.b,sep=", ")
    repeat{
      temp <- fib.a+fib.b
      fib.a <- fib.b
      fib.b <- temp
      cat(", ", fib.b,sep="")
      if(fib.b>thresh){
        break
      }
    }
  } else {
    fibseq <- c(1,1)
    counter <- 2
      repeat{
      fibseq <- c(fibseq,fibseq[counter-1]+fibseq[counter])
      counter <- counter+1
        if(fibseq[counter]>thresh){
            break
        }
      }
    return(fibseq)
  }
}


myfib4(thresh=150,printme=TRUE)
## 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233
myfib4(1000000,T)
## 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269
myfib4(150,FALSE)
##  [1]   1   1   2   3   5   8  13  21  34  55  89 144 233
myfib4(1000000,printme=F)
##  [1]       1       1       2       3       5       8      13      21      34
## [10]      55      89     144     233     377     610     987    1597    2584
## [19]    4181    6765   10946   17711   28657   46368   75025  121393  196418
## [28]  317811  514229  832040 1346269



Início



Seção 11.2


Lazy evaluation - argumentos são usados, ou seja, a expressão só é avaliada, se o argumento for requisitado.

... - se passado como argumento da função, assume as elipses passadas como argumentos de funçoes interiores. Assim argumentos das funções interiores podem ser acessados.


Item a

comp_interest <- function(P, i, t = 12, y, plotit = TRUE, ...){
  # P - principal investment ammount
  # i - interest rate per annum, in text as percentage, ex 22.9 is 22.9%
  # t - frequency of interest paid per year
  # y - amount of years
  # Fi = P*(1 + (i/(100*t)))^(t*y)

  points <- c(1:as.integer(t*y)) # eixo x
  Fi <- NA

  for(p in 0:length(points)){
    Fi[p+1] <- P*(1 + (i/(100*t)))^(p)
  }


#plot at each integer time t
#definir eixo x como length e y como os valores de F a cada t

  if(plotit){
    F_ano <- seq(from = 1, to = length(Fi), by = t)[-1]
    plot(x = c(1:length(F_ano)), y = Fi[F_ano], type="s", ...) 
  } else{
    return(Fi)
  }
}


#i
itemi <- comp_interest(P = 5000, i = 4.4, y = 10, plotit = F)
itemi[length(itemi)] #dá apenas o resultado final
## [1] 7757.291
#ii
comp_interest(P = 100, i = 22.9, y = 20, xlab = "Year (y)", ylab = "Balance(F)", main = "Compound interest calculator")

#iii
itemiii <- comp_interest(P = 100, i = 22.9,t = 1, y = 20, plotit = F)[-1]

comp_interest(P = 100, i = 22.9, y = 20, xlab = "Year (y)", ylab = "Balance(F)", main = "Compound interest calculator", col = "blue")
lines(itemiii, col = "red")


Item b

quad <- function(k1, k2, k3){
  
  if(missing(k1) | missing (k2) | missing (k3)) {
    return("Não é possível rodar")
    }
  if((k2^2 - 4*k1*k3) < 0){
    print("Delta negativo, sem solução nos reais.")
  } else if((k2^2 - 4*k1*k3) == 0){
    return(-k2/(2*k1))
  } else {
    return(c((-k2-(k2^2 - 4*k1*k3)^(0.5))/(2*k1), (-k2+(k2^2 - 4*k1*k3)^(0.5))/(2*k1)))
  }
}


#i
quad(2, -1, -5)
## [1] -1.350781  1.850781
quad(1, 1, 1)
## [1] "Delta negativo, sem solução nos reais."
#ii
quad(1.3, -8, -3.13)
## [1] -0.3691106  6.5229567
quad(2.25, -3, 1)
## [1] 0.6666667
quad(1.4, -2.2, -5.1)
## [1] -1.278312  2.849740
quad(-5, 10.11, -9.9)
## [1] "Delta negativo, sem solução nos reais."
#iii

#ativa a mensagem do missing

quad(k2 = 2, k3 = 1)
## [1] "Não é possível rodar"




Início



Seção 11.3


Item c

gmean <- function(element){
      prod(element)^(1/length(element))
    }

geolist <- function(x){
  
  for (i in 1:length(x)){
    
    if(is.matrix(x[[i]])){
      x[[i]] <- apply(x[[i]], MARGIN = 1, FUN = gmean)
    } else{
      x[[i]] <- gmean(x[[i]])
    }
    
  }
  return(x)
}


#i
foo <- list(1:3,matrix(c(3.3,3.2,2.8,2.1,4.6,4.5,3.1,9.4),4,2),
matrix(c(3.3,3.2,2.8,2.1,4.6,4.5,3.1,9.4),2,4))

geolist(foo)
## [[1]]
## [1] 1.817121
## 
## [[2]]
## [1] 3.896152 3.794733 2.946184 4.442972
## 
## [[3]]
## [1] 3.388035 4.106080
#ii
bar <- list(1:9,matrix(1:9,1,9),matrix(1:9,9,1),matrix(1:9,3,3))
geolist(bar)
## [[1]]
## [1] 4.147166
## 
## [[2]]
## [1] 4.147166
## 
## [[3]]
## [1] 1 2 3 4 5 6 7 8 9
## 
## [[4]]
## [1] 3.036589 4.308869 5.451362



Início