I'm currently studying basic growth functions. Since R is not really good at dealing with symbollic equations, I'm writing both the growth function, as well as it´s derivative to later apply the Newton-Raphson method. The code is looking like this:
invested <- c(10000, 20000, 15000, 30000)
rates <- c(5e-3, 6e-3, 7e-3, 8e-3)
amount <- 0
prediction <- function(t){
for(i in invested){
for(j in rates){
parcel <- i * exp(j * t)
}
amount <- amount + parcel
}
print(amount)
}
dt <- 0
dpred <- function(t){
for(i in invested){
for(j in rates){
parcial <- (j*i) * exp(j * t)
}
dt <- dt + parcial
}
print(dt)
}
The problem is it spits out prediction(1)
is 85,59 too much for the actual value I found testing with the following code:
(10000*exp(5e-3))+(20000*exp(6e-3))+(15000*exp(7e-3))+(30000*exp(8e-3))
The derivative dpred(1)
is also off by a quite big margin, according to my previous calculations.
Any ideas on why is the error so big and how to get rid of it?