# How to tell matlab to, instead of throwing an error message, give me a Boolean (e.g., 0 means I get error) when I try to use Horner command

25 views (last 30 days)
Mohammad Shojaei Arani on 19 Oct 2021
Commented: Mohammad Shojaei Arani on 16 Nov 2021 at 10:53
Hello friends,
In my code I need to know beforehand whether matlab can calculate Horner representation for my polynomial. Sometimes. it is not possible to find Horner representations in matlab due to depth limit of 32 for the nested paranthethis. In such cases I get the error message:
Error using symengine
Error: Nesting of {, [, and ( cannot exceed a depth of 32.
Error in symengine
Error in sym/matlabFunction (line 190)
g = symengine('makeFhandle',varnames,body);
Instead, I really need matlab to inform me whther Horner representation is possible or not. The reason is that I need to calculate Horner representation for many polynomials. So, I need to write a matlab function where the input is a polynomial and I would like the output to be the Honer representation if this is possible or the same polynomial whenever Horner representation is not possible (no error message).
Finally, I hope you know a way to ask matlab to consider a nested polynomials for a depth bigger than 32 (I hope this is possible as I really need this).
Thanks a lot in advance and I look forward to hearing from you soon!
Babak

the cyclist on 19 Oct 2021
You could use a try-catch routine to catch the error.
Mohammad Shojaei Arani on 13 Nov 2021 at 7:41

Steven Lord on 13 Nov 2021 at 15:13
Rather than explicitly type out a 32nd degree polynomial, why not use a for loop? Start with your accumulator containing the value of the highest coefficient. For each following coefficient multiply the accumulator by x then add that coefficient. This has an added benefit of not needing to be modified if you later on want to evaluate a 33rd degree polynomial, or a 34th degree, or .... It's also one of the approaches the polyval function in MATLAB uses.
Mohammad Shojaei Arani on 16 Nov 2021 at 10:53
Thanks Steven,
Well, you are suggesting to implement the Horner scheme by myself. That is fine and doable but I am
not sure if using a for-loop is the most efficient way to do so. Imagine, I have an extremely long polynomial
and want to use the for-loop, then who knows how much time it takes. And, imagine that I need to do this millions of times. I gess that matlab does it rather differently and I am not an expert on these things.
I find it very strange that matlab has limitations for this not so complex problem.