Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Extract coefficients of custom variables in symbolic expression
Replies: 1   Last Post: May 23, 2014 7:20 AM

 Messages: [ Previous | Next ]
 Christopher Creutzig Posts: 323 Registered: 2/24/09
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+x-y,[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 two-output 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

Date Subject Author
3/6/14 François
5/23/14 Christopher Creutzig