
Re: Extract coefficients of custom variables in symbolic expression
Posted:
May 23, 2014 7:20 AM


On 06.03.14 10:41, François wrote: > Hello, > > I have a symbolic variable vg defined like this > vg =eg + Rg*ig  (Lg*(eg  vc  Rc*i1 + ig*(Rc + Rg)))/(L2 + Lg) > all other variables are symbolic as well. > Now I would like to extract the coefficients (made of symbolic variables Lg,Rg,Rc,L2) of the symbolic variables eg,vc,vi,i1,ig.
> With zero for the absent coefficients! I already tried > coeffs(di1,[i1 ig vc vi eg]) > but it does not return zero for the missing coefficients, which makes it worthless!
Since you have a multivariate polynomial, where would the ?missing? coefficients be? (Note that coeffs is not limited to the linear case, but think about what the output should be for coeffs(x^5*y^3+y^4+x^4*y^5+xy,[x,y]) etc.  it would be possible to return a sparse array, of course, but that does not seem to be what you are looking for.)
Does the twooutput form of coeffs help?
>> [cs, ts] = coeffs(vg,[i1 ig vc vi eg])
cs =
[ (Lg*Rc)/(L2 + Lg), Rg  (Lg*(Rc + Rg))/(L2 + Lg), Lg/(L2 + Lg), 1  Lg/(L2 + Lg)]
ts =
[ i1, ig, vc, eg]
I.e., the coefficient for ts(1) is cs(1) etc.
Or, if you're looking only at the linear case, try this:
>> tmp = arrayfun(@(var) diff(vg,var), ... [i1 ig vc vi eg],'UniformOutput',false); >> [tmp{:}].'
ans =
(Lg*Rc)/(L2 + Lg) Rg  (Lg*(Rc + Rg))/(L2 + Lg) Lg/(L2 + Lg) 0 1  Lg/(L2 + Lg)
HTH,
Christopher

