How to extract powers of a symbolic polynomial?
43 views (last 30 days)
Show older comments
I'm working with a symbolic polynomial
y = 0.96*z^3500 + 0.04*z^0
I can extract the coefficients easily with
coeffs(y)
but I cannot figure out a way to pull off the corresponding powers of z into a vector. I've tried doing some wonky stuff with logs, but nothing so far. Am I SOL?
Thank you!!
0 Comments
Answers (3)
Steven Lord
on 22 Mar 2021
syms z
y = 0.96*z^3500 + 0.04*z^0
[coefficients, powers] = coeffs(y)
syms y positive
exponents = simplify(subs(log(powers)./log(z), z, y))
1 Comment
Walter Roberson
on 19 Jul 2021
Different formulation for finding the exponents.
syms z
y = 0.96*z^randi(9999) + 0.04*z^randi(9999)
[coefficients, powers] = coeffs(y)
exponents = mapSymType(powers, 'power', @(Z) children(Z,2));
if powers(end) == 1; exponents(end) = 0; end
exponents
This particular code relies on an enhancement to children() that was made a small number of releases ago. A workaround is possible for older releases.
Shubham Rawat
on 22 Mar 2021
Hi Alina,
You may first find coefficients of all variables like this:
coef = sym2poly(y);
Then you can find all the index of all the non zero elements and -1 as indexing start from 1 in MATLAB:
polyPowers = find(coef) - 1;
Hope this Helps!
1 Comment
Davy Figaro
on 19 Jul 2021
This is good, but because symbolic polynomial powers are presented in descending order, you need to double flip to get the polyPowers to line up to the original polynomial:
polyPowers = flip(find(flip(coef))) - 1;
See Also
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!