Coeffs of non linear product terms

조회 수: 7 (최근 30일)
Punit
Punit 2012년 10월 4일
I need to find coefficient of a product term in a symbolic expression. An exaple is:
syms x y
[c, v] = coeffs(x+y+2*x*y,x*y);
But this code gives an error: Error in MuPAD command: The indeterminate is invalid. [coeff]
I know that if I write
[c, v] = coeffs(x+y+2*x*y,[x,y]);
then the first term will be the coefficiet of product term, but I need to get the coeff of only the product term. Also, I'm concerned about execution time, so I want to do it in minimum possible lines. I dont want to use an if loop to check which element in c is the product term. Does anyone know how I can possibly find the coefficient of a specific product term.

채택된 답변

Matt Fig
Matt Fig 2012년 10월 4일
편집: Matt Fig 2012년 10월 4일
I think a FOR loop would be fast enough here, but....
syms x y
[c, v] = coeffs(x+y+2*x*y,[x,y]);
C = arrayfun(@char,v,'Un',0);
C = c(~cellfun('isempty',regexp(C,'\w*\*\w*','match')))

추가 답변 (1개)

Punit
Punit 2012년 10월 4일
Thanks mat, This is working well, but I have another question. I have a symbolic matrix (~50 X 50) of similar expressions where linear, constant and x*y type bi-affine terms are present in each entry. In all, there are ~50 variables and about ~500 bi-affine terms. My goal is to find the coefficients of all the linear and nonlinear terms. Obviously, each term will have a matrix coefficient. In essence I want to write the matrix as -
M = F_0 + F_x*x + F_y*y + F_w*w
where w = x*y and F_(.) are the constant matrices. Currently, I'm running a for loop over rows and columns of the matrix. This entire thing is inside an optimizer, so the whole program is taking hours. Is there any way I can avoid the for loops over the matrix too?

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by