# 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

2 ビュー (過去 30 日間)
Mohammad Shojaei Arani 2021 年 10 月 19 日
コメント済み: Rik 2021 年 12 月 24 日
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 2021 年 10 月 19 日
You could use a try-catch routine to catch the error.
##### 1 件のコメント表示非表示 なし
Mohammad Shojaei Arani 2021 年 11 月 13 日
Thanks for your kind help!

サインインしてコメントする。

### その他の回答 (1 件)

Steven Lord 2021 年 11 月 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.
##### 2 件のコメント表示非表示 1 件の古いコメント
Rik 2021 年 12 月 24 日
Given that you can provide x as an array, the only way I see performance issues popping up is if you have millions of polynomials, or polynomials with millions of terms.
In the latter case I doubt there would be much point in calculating both ends of the polynomial: either the high powers grow so large that the small powers dont matter (for abs(x)>1), or the high powers will become so small that they are hardly more than noise (for abs(x)<1).
0.1^1000,2^1000
ans = 0
ans = 1.0715e+301

サインインしてコメントする。

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by