How to expand a symbolic matrix expression?
26 次查看(过去 30 天)
显示 更早的评论
I want to expand all terms in a complex symbolic matrix expression by the distributive law of the expression.
tempa1 = symmatrix('A1',[2 2]);
tempb1 = symmatrix('B1',[2 2]);
tempc1 = symmatrix('C1',[2 2]);
tempd1 = symmatrix('D1',[2 2]);
for nn = 2:5
tempa2 = symmatrix(sprintf('A%d',nn),[2 2]);
tempb2 = symmatrix(sprintf('B%d',nn),[2 2]);
tempc2 = symmatrix(sprintf('C%d',nn),[2 2]);
tempd2 = symmatrix(sprintf('D%d',nn),[2 2]);
tempa = tempa1*tempb2+tempc1*tempd2+tempb1*tempa2+tempd1;
tempb = tempa2*tempb1+tempc2*tempd1+tempb2*tempa1+tempd2;
tempc = tempa1*tempb1+tempc1*tempd1+tempb1*tempa1+tempd2;
tempd = tempa2*tempb2+tempc2*tempd2+tempb2*tempa2+tempd1;
tempa1 = tempa;
tempb1 = tempb;
tempc1 = tempc;
tempd1 = tempd;
end
vec_1 = symmatrix('vec_1',[2 1]);
result = tempa*vec_1;
When the variable result is executed, it is output as a complex matrix expression.
The variable result is a 2 by 2 symbolic matrix, and each element is also returned as a symbolic expression.
However, the information I need is not an element expression, but a string of expressions output when the variable result is executed.
When the variable result is executed, looking at the output result, the variable vec_1 is expressed by multiplying the result of tempa outside the bracket.
I want all the brackets to be solved and each term to be multiplied by vec_1.
A bulti-in function called 'expand' is identified as the most similar function to what I want.
However, this function is not supported for symmatrix input.
To use this function, if you run symmatrix2sym on the variable result, it returns all element representations.
Please, let me know that.
Thank you.
3 个评论
Paul
2023-8-4
Hi Lee Jong Hyun,
I could be wrong, but I don't think you'll be able to easily get the result you're looking for.
But suppose you could, i.e., get an expression for tempa in the form (n = 2)
syms A1 A2 B1 B2 C1 D1 D2 [2 2] matrix
syms vec_1 [2 1] matrix
tempa = A1*B2*vec_1 + B1*A2*vec_1 + C1*D2*vec_1 + D1*vec_1
What whould be the next step in the process? Maybe there's a way to achieve the goal without forming tempa as shown?
采纳的回答
Star Strider
2023-8-3
I am not certain what you intend by ‘expand’.
One option could be:
sympref('AbbreviateOutput',false);
that I added, and another the expand function (that does not work here). If you want to see the elements of the matrices, you would need to define their elements, as described in Convert Numeric Matrix to Symbolic Matrix Variable.
sympref('AbbreviateOutput',false);
tempa1 = symmatrix('A1',[2 2]);
tempb1 = symmatrix('B1',[2 2]);
tempc1 = symmatrix('C1',[2 2]);
tempd1 = symmatrix('D1',[2 2]);
for nn = 2:5
tempa2 = symmatrix(sprintf('A%d',nn),[2 2]);
tempb2 = symmatrix(sprintf('B%d',nn),[2 2]);
tempc2 = symmatrix(sprintf('C%d',nn),[2 2]);
tempd2 = symmatrix(sprintf('D%d',nn),[2 2]);
tempa = tempa1*tempb2+tempc1*tempd2+tempb1*tempa2+tempd1;
tempb = tempa2*tempb1+tempc2*tempd1+tempb2*tempa1+tempd2;
tempc = tempa1*tempb1+tempc1*tempd1+tempb1*tempa1+tempd2;
tempd = tempa2*tempb2+tempc2*tempd2+tempb2*tempa2+tempd1;
tempa1 = tempa;
tempb1 = tempb;
tempc1 = tempc;
tempd1 = tempd;
end
vec_1 = symmatrix('vec_1',[2 1]);
result = tempa*vec_1
.
4 个评论
Star Strider
2023-8-4
I am not certain how to get that result, since the functions I would normally use do not work on symmatrix expressions.
Dyuman Joshi
2023-8-4
It might not be possible to get the result OP wants to obtain (as mentioned above) because one can not manipulate the result obtained from the code.
These are the all functions that accept symbolic matrix variables as input, the only functions that manipulates an expression are formula and displayFormula, both of which give the same output.
methods symmatrix
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!