Isolate Coefficients of Multivariate Linear Polynomial

조회 수: 7 (최근 30일)
Reuvenk
Reuvenk 2012년 6월 17일
Hi, I have a symbolic expression
eqn = r - 2*x +7/4
I would like to isolate the coefficients of the variables (x, r and y (y's coefficient is 0)). How do I do that? I've tried using coeffs but that only lets me specify one variable. It would also be ok if I got two vectors back:
[ -2 , 1 , 7/4]
[x , r , 1]
even if they don't include y. Thanks!

채택된 답변

Alexander
Alexander 2012년 6월 18일
If you know that you have only linear terms and only x, r, and y, you could use diff to get the coefficients:
syms x r y
eqn = r - 2*x +7/4;
[diff(eqn, x), diff(eqn, r), diff(eqn, y)]
This gives:
ans =
[ -2, 1, 0]
  댓글 수: 1
Reuvenk
Reuvenk 2012년 6월 18일
That doesn't give the independent term, but I can easily find that by subbing 0 for all of the variables. Thanks for the answer!

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

추가 답변 (1개)

Christopher Creutzig
Christopher Creutzig 2012년 6월 27일
syms x r
eqn = r - 2*x +7/4
coeffs(eqn, [x, r])
ans =
[ 7/4, 1, -2]
Note that coeffs returns the coefficients in increasing order and leaves out zeroes:
>> coeffs(7*x^2*r^2 + 6*x^2*r + 5*x^2 + 4*x*r^2 + 3*x*r + 2*r^2, [x, r])
ans =
[ 2, 3, 4, 5, 6, 7]

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by