How can I find the degree of a given "anonymous function" like f=@(x) x^2+2x; given the functions are only polynomials?

댓글 수: 1

dpb
dpb 2019년 4월 27일
편집: dpb 2019년 4월 27일
Convert to string via func2str and regexp() to return powers of exponentials--find max thereof. Of course, that presumes someone doesn't write a function like using the explicit form of Horner's rule--
f=@(x) (x+2).*x;
in which case you've got more work to do... :)

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2019년 4월 27일

0 개 추천

Build a vector
X = realmax.^(1./(1:50));
Evaluate the function at X. The first result that is finite is probably the degree. However, it is possible for a polynomial with sufficiently large coefficients to generate an infinity "early", or for with sufficiently small leading coefficient to be "late" relative to this, so you should use that as a starting point to do more cross-checking.

댓글 수: 3

Ashish Sahu
Ashish Sahu 2019년 4월 28일
Thanks, it seems to work.
Ashish Sahu
Ashish Sahu 2019년 4월 28일
This method doesn't seem to work for degree 4 & degree 5 polynomials.
f=@(x) x^4;
X = realmax.^(1./(1:50));
for i=1:50
Y = f(X(i));
if Y<Inf
break
end
end
degree = i % degree of polynomial is degree
I am getting degree = 5.
Walter Roberson
Walter Roberson 2019년 4월 28일
x^4 is a leading coefficient of 1 which is "suffiently small" in terms of what I wrote above.
Perhaps a slightly different X would help? You should analyze why this case fails to figure out what changes to make.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Mathematics에 대해 자세히 알아보기

질문:

2019년 4월 27일

댓글:

2019년 4월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by