How to get the coefficients of an equation

Dear all,
Is it possible to extract the coefficients of an equantion defined as below:
syms x1 x2
eq = 2*x1 + x2 <= 0
eq = 
By using coeffs and sym2poly did not work when it has the relational operator "<=".
c = coeffs(eq)
c = 
1
c = sym2poly(eq)
Error using sym/sym2poly
Not a polynomial.

2 commentaires

Dyuman Joshi
Dyuman Joshi le 8 Nov 2023
What is the expected output?
[1 2] or [2 1]? or something else?
[2 1]

Connectez-vous pour commenter.

 Réponse acceptée

How about -
syms x1 x2
eq = 2*x1 + x2 <= 0
eq = 
out = flip(coeffs(lhs(eq)))
out = 

1 commentaire

Good solution!
This way I can also have the term in the right side
syms x1 x2
eq = 2*x1 + x2 <= 5
eq = 
out = flip(coeffs(rhs(eq)))
out = 
5
Thanks!

Connectez-vous pour commenter.

Plus de réponses (1)

Maybe it is appropriate to work in a reverse order, e.g.:
syms x1 x2
Coeff = [2;1];
eq = [x1, x2]*Coeff<=0
eq = 

Catégories

En savoir plus sur Symbolic Math Toolbox dans Centre d'aide et File Exchange

Produits

Version

R2023a

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by