Coeffs of non linear product terms

3 次查看(过去 30 天)
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?

类别

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