Extracting variables from symbolic expression in a matrix form

1 次查看(过去 30 天)
I have a long equation in symbolic math toolbox which has the following form:
A*u1+B*u2+C*u3
A, B and C are long relationships consisting of symbolic variables. I would like to extract the A B C in from u1, u2 and u3 and write them in a matrix form. In other words I would like to shape a relationship such us
[A B C]*{u}
where u is
u=[u1;u2;u3]
How can I do that?

采纳的回答

Walter Roberson
Walter Roberson 2013-4-29
You could use coeffs but the output is a bit of a nuisance for multivariates.
You could call into MuPAD and use coeff.
But really, the easiest way is likely to use
A = subs(Expression, {u2,u3}, {0,0});
B = subs(Expression, {u1,u3}, {0,0});
C = subs(Expression, {u1,u2}, {0,0});
  2 个评论
Giorgos Papakonstantinou
Thank you very much Walter.
I would like to ask a relevant question with the symbolic math toolbox.
Suppose I have:
h=a+b;
k=c1*h +c2*h+c3*h;
then matlab would give me a result like this:
k=(a+b)*c1+(a+b)*c2+(a+b)*c3
Is there anyway to force matlab to display k at the following more compact form:
k=c1*h +c2*h+c3*h;
Walter Roberson
Walter Roberson 2013-4-29
If you define k before you define h, then I think it might display in the shorter form.
syms h a b
k = c1*h +c2*h+c3*h;
h = a + b;
disp(k)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by