In general I want to extract coefficients of specific variables of a polynomial (even of variables whose coefficients are zero).
I try to explain it step by step:
1) I have a first polynomial. Exemplary extract:
myPolynomial = Subscript[a, d, 0, 0, 0] +
Iq^2*Subscript[a, d, 0, 0, 2] + Id*Subscript[a, d, 0, 1, 0]
(...and so on)
2) Each Subscript[a, d, x, y, z]
is a variable. x
, y
, and z
are indices of a summation. Id
, Iq
are the coefficients.
3) At first, I want to collect all coefficients of that first polynomial. In the example, this should lead to a list or something that contains {1, Iq^2, Id}
. The corresponding variables are {Subscript[a, d, 0, 0, 0], Subscript[a, d, 0, 0, 2], Subscript[a, d, 0, 1, 0]}
.
At this point I want to mention that I have to extract the final results to Matlab. For this step I use the "ToMatlab"-function. Code:
myPolynomial /. Plus -> List /. c_.*Subscript[__] :> c // ToMatlab;
With it, my exported Matlab result has following form: [1, Iq^2, Id]
;
Ok.. this was the first part, which is already running.
5) Now I want to differentiate the just mentioned polynomial respective (e.g.) Id
. This leads to a second polynomial. In the example this would lead to
myPolynomial2 = 0 + 0 + Id*Subscript[a, d, 0, 1, 0]
Now I want to automatically extract the coefficients of this polynomial. Again Subscript[a, d, x, y, z]
with different values of x
, y
and z
are the variables. I want to extract the coefficients in such a way, that each coefficient is at the same place in the resulting list, as the coefficient from the first polynomial. E.g.: For the first list, the coefficients of the variables were listed in following order:
{Subscript[a, d, 0, 0, 0], Subscript[a, d, 0, 0, 2], Subscript[a, d, 0, 1, 0]}
Therefore the result for the coefficients of the second polynomial should have following form: {0, 0, Id}
.
However, I am only able to collect the coefficients without the zeros, so the first coefficient of the second list does not belong to the same variable, as the first coefficient of the first list.
Subscript
is really bad practice, it makes your code hard to read. Also, you don't give a concrete example , coded polynomial, over which to test different methods. I guess you should look atCoefficientList
, hard to tell. If you expect to inspire anybody to volunteer their time to look at your problem, then start by doing your part. Get the Informed badge by taking the tour. Please edit your question and give a concrete example we can copy&paste and the expected outcome. $\endgroup$Coefficient
function? reference.wolfram.com/language/ref/Coefficient.html $\endgroup$