How can I find all expressions added to a large formula?

6 vues (au cours des 30 derniers jours)
Hamza Yusuf
Hamza Yusuf le 10 Nov 2020
Commenté : Walter Roberson le 10 Nov 2020
In maple there is coeff(p, x^n) where you find all x coefficients to the n-th power in the polynomial p. in Matlab there is a coeffs(p,x)); where you find all the x on polynomial p. How do find a x^n on a polynomial n just like the one on maple. I have tried to make like this
(coeffs(p, x^2))
And for that I get the error.
Error using symengine
Invalid indeterminate.
Error in sym/coeffs (line 60)
cSym = mupadmex('symobj::coeffs',p.s, args{:});
Error in randomfile (line 36)
g = ((coeffs(p, x^2)));
it works when n needs to be one.

Réponse acceptée

Walter Roberson
Walter Roberson le 10 Nov 2020
Use coeff with two outputs and ismember the desired power in the second output to locate the corresponding coefficients. Or use the 'all' option, in which case you can use indexing to get the coefficients.
Doing the work in a single call without using any true functions is a bit messy.
Possibly there might be a way using feval(symengine)
  3 commentaires
Walter Roberson
Walter Roberson le 10 Nov 2020
n = randi([0,4]);
syms x
[c,t] = coeffs(16*x^2 + 19*x + 11)
[found, idx] = ismember(x^n, t) ;
if found
c = c(idx) ;
else
c = sym(0);
end
Walter Roberson
Walter Roberson le 10 Nov 2020
n = randi([0,2]);
syms x
y = 16*x^2 + 19*x + 11;
feval(symengine, 'coeff', y, x, n)
ans = 
16

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by