0

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").

lmo
  • 37,904
  • 9
  • 56
  • 69
MarcinAu
  • 602
  • 2
  • 6
  • 18
  • You should provide the code that leads to this error. If your only goal is to solve an equation with yacas, it may be easier to use yacas directly (and not through Ryacas). – Vincent Zoonekynd Sep 24 '13 at 10:03
  • Ok, I added the code to my initial post. – MarcinAu Sep 24 '13 at 11:15
  • 1
    Why are you diving into a new language (`R`) to do something which involves a completely different tool (`yacas`)? You're making things doubly tough on yourself -- just use the application directly. Oh, and BTW "complex equation" has a very specific meaning in mathematics. Your equation may be complicated, but it's in the Reals. – Carl Witthoft Sep 24 '13 at 11:39
  • 1
    You are trying to solve, exactly, a polynomial equation of degree 60 (that will not work). You probably want an approximate numeric solution instead: in R, you can use `optimize`. Since your computations look like an internal rate of return, you may want to look at the `FinCal` or `financial` packages. – Vincent Zoonekynd Sep 24 '13 at 11:55
  • Carl, normally I would have used Matlab, but unfortunatelly I had to create a solution that would work for an organisation that doesn't use that package. Vincent, that you for your suggestions. I'll try to tackle that problem using one of the packages that you recommended. – MarcinAu Sep 24 '13 at 12:21

1 Answers1

1

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.

szczelo
  • 26
  • 3
  • Great. Thanks. Your code works perfectly. I've only restructured it so that it looks a bit clearer and moved inputs section to the top. I'll try to tune it up a little bit to remove incrementation and replace it with division of equally-sized ranges of possible APR. – MarcinAu Sep 24 '13 at 12:45