I've started using R to solve a complex equation. After generating the equation I tried to solve it using Ryacas
. Unfortunately, instead of giving me the result, Ryacas
returns the following:
CommandLine(1) : Max evaluation stack depth reached. Please use MaxEvalDepth to increase the stack size as needed.
CommandLine(1) : Max evaluation stack depth reached. Please use MaxEvalDepth to increase the stack size as needed.
Could you please tell me how to increase that stack size through Ryacas
? I've tried to do it in many ways, but I really don't know how to make use of the advice that Ryacas
gave me.
===== Edit =======
So here's the code that leads to generating an equation that I want to solve.
#define net and gross values
net=10000
gross=12563.49
#construct an array for cash flows
flows=matrix(nrow=1, ncol=60)
#populate the array with cash flows
flows[c(1:60)]=c(-297.21)
#generate the equation
#flows
eq1=NULL
for (i in 1:60) {
eq1=paste(eq1," + ", toString(flows[i]),"/((1 + x)^(",i, "/60)", ") ", collapse="")
}
#complete
equation=paste(toString(net), eq1, " == ", toString(gross), collapse="")
I then try to solve it using Solve(equation, "x").
This looks like an equation for an APR. Try a simple iteration like this one instead:
#inputs
instalments=60
net=12800
monthly=387.63
interest=0.1890
#function
CalculateAPR <- function(InitialPayout, InterestRate, N, MonthlyRepayment) {
i <- InterestRate
repeat{
DF <- sapply(1:N, function(N) { MonthlyRepayment/((1+i)^(N/12)) } )
if(InitialPayout>=sum(DF)) break()
i <- i + 0.00001
}
return(i)
}
#output
ans=CalculateAPR(net, interest, instalments, monthly)
rm(list = c('instalments', 'interest', 'monthly', 'net'))
print(ans)
You might want to try a more efficient algorithm than this one, which simply adds 0,001% to each iteration.