How to extract polynomial "p" from symbolic solution "root(p,x,k)"?

조회 수: 1 (최근 30일)
When using solve() symbolically and a solution of the form root(f(z),z,k) is produced, is it possible to extract f(z) without manually copying and pasting?
For instance, if I assign an example solution to variable x :
x = root(z^2 + z + 1, z, 1)
Can z^2 + z + 1 or its coefficients be extracted from x?
sym2poly() does not seem to recognize root(). I suppose I could bluntly use regexp(), though it seems there must be a simpler solution.

채택된 답변

Walter Roberson
Walter Roberson 2022년 6월 10일
편집: Walter Roberson 2022년 6월 10일
use children() — or findSymType() and then children()
  댓글 수: 2
FingersCrossed
FingersCrossed 2022년 6월 10일
Perfect, thank you. Here's my use case for anyone else:
root_children = children(x);
poly = root_children{1};
Walter Roberson
Walter Roberson 2022년 6월 10일
children(x, 1)
would be shorter for that use case

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

추가 답변 (1개)

FingersCrossed
FingersCrossed 2022년 6월 10일
Use Walter's answer instead of this. Just for fun, here is a convoluted regexp() approach:
[cell_poly,~] = regexp(string(x),'root\((.*?),','tokens','match');
poly = str2sym(string(cell_poly));

카테고리

Help CenterFile Exchange에서 Polynomials에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by