MATLAB Answers

0

How to substitute multiple array syms easily?

Asked by Elias Hasle on 12 Oct 2019
Latest activity Edited by Elias Hasle on 12 Oct 2019
Setup:
A = sym('A', 4)
B = sym('B', 3)
x = A*A(1:4,1)+[B(1:3,1); 0]
subs(x, {A, B}, {diag([3,2,1,0]), diag([1,3,5])})
Output:
A =
[ A1_1, A1_2, A1_3, A1_4]
[ A2_1, A2_2, A2_3, A2_4]
[ A3_1, A3_2, A3_3, A3_4]
[ A4_1, A4_2, A4_3, A4_4]
B =
[ B1_1, B1_2, B1_3]
[ B2_1, B2_2, B2_3]
[ B3_1, B3_2, B3_3]
x =
A1_1^2 + B1_1 + A1_2*A2_1 + A1_3*A3_1 + A1_4*A4_1
B2_1 + A1_1*A2_1 + A2_1*A2_2 + A2_3*A3_1 + A2_4*A4_1
B3_1 + A1_1*A3_1 + A2_1*A3_2 + A3_1*A3_3 + A3_4*A4_1
A1_1*A4_1 + A2_1*A4_2 + A3_1*A4_3 + A4_1*A4_4
Error using sym/subs>normalize (line 231)
Entries in second argument must be scalar.
Error in sym/subs>mupadsubs (line 157)
[X2,Y2,symX,symY] = normalize(X,Y); %#ok
Error in sym/subs (line 145)
G = mupadsubs(F,X,Y);
Error in debug_sym (line 4)
subs(x, {A, B}, {diag([3,2,1,0]), diag([1,3,5])})
Attempts at using
syms A B
for the initial declaration have failed at the indexing stage (the definition of x), because the symbols are then assumed to be scalar. It seems, though, that subs can handle them correctly.
The most promising (actually the only) workaround I have found is to apply the substitutions one at a time, which connects to the common logic for dimension checking:
subs(subs(x, A, diag([3,2,1,0])), B, diag([1,3,5]))
(Output:)
ans =
10
0
0
0
but this reduces readability and I assume it will also harm performance, particularly when doing many substitutions.

  0 Comments

Sign in to comment.

Tags

Products


Release

R2018b

0 Answers