Coeffs of non linear product terms

7 views (last 30 days)
Punit
Punit on 4 Oct 2012
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.

Accepted Answer

Matt Fig
Matt Fig on 4 Oct 2012
Edited: Matt Fig on 4 Oct 2012
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')))

More Answers (1)

Punit
Punit on 4 Oct 2012
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?

Tags

Products

Community Treasure Hunt

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

Start Hunting!