Evaluating symbolic matrix equations

I have a symbolic matrix of size 20x3, which I have created using the sym() function. I have another matrix B which is calculated using certain rows of A, or in general the matrix B is a function of A i.e., . Once I obtain all the numerical entries of A, how can I evaluate B after substituting A with the obtained numerical values?
A = sym('a', [20, 3]); % Create a 20x3 symbolic matrix
% Create a matrix B whose values depend on some function of A's elements
B(1, :) = cross(A(1, :), A(3, :));
B(2, :) = A(5, :) .* A(10, :) .* A(20, :);
% Substitute A with numerical values
A = subs(A, A, rand(20, 3)); % Now, A is a 20x3 matrix with random entries
% How do I evaluate the matrix B?
subs(B) ?
subs(B, A) ? % Both commands aren't working

 採用された回答

Nishanth Rao
Nishanth Rao 2021 年 7 月 10 日

0 投票

After some hit and trials, I found the solution:
A = sym('a', [20, 3]); % Create a 20x3 symbolic matrix
% Create a matrix B whose values depend on some function of A's elements
B(1, :) = cross(A(1, :), A(3, :));
B(2, :) = A(5, :) .* A(10, :) .* A(20, :);
% Substitute B with A, where A is substitued with 20x3 numerical entries:
B = subs(B, A, rand(20, 3));

7 件のコメント

Nishanth Rao
Nishanth Rao 2021 年 7 月 10 日
You can use eval(B) later to simplify the entries of B
Walter Roberson
Walter Roberson 2021 年 7 月 10 日
eval() of a symbolic expression can give you error messages or can give you unexpected results. eval() of a symbolic expression is the same as eval(char()) of the expression, but char() of a symbolic expression is in a language that is not MATLAB and is not the internal symbolic language, MuPAD.
You can simplify() or vpa() (or sometimes, double()) symbolic expressions, but eval() is risky.
Nishanth Rao
Nishanth Rao 2021 年 7 月 11 日
Oh.. I see. Thanks a lot for the information.
zym
zym 2022 年 7 月 22 日
編集済み: zym 2022 年 7 月 22 日
Thanks for this! I was just wondering if you happen to know why subs does not work with multiple matrices? It seems I need to use subs twice to evaluate the expression
clear;
A = sym('a', [2, 2]);
B = sym('b', [2, 2]);
C=A*B;
%this does not works
subs(C, {A, B}, {rand(2, 2), rand(2, 2)})
%this works
Ceval1 = subs(C, A, rand(2, 2))
Ceval3 = subs(Ceval1, B, rand(2, 2))
Torsten
Torsten 2022 年 7 月 22 日
編集済み: Torsten 2022 年 7 月 22 日
Works.
A = sym('a', [2, 2]);
B = sym('b', [2, 2]);
C=A*B;
%this does not works
subs(C, [A, B], [rand(2, 2), rand(2, 2)])
ans = 
Walter Roberson
Walter Roberson 2022 年 7 月 23 日
It is a syntax restriction. When you use the {} form of subs(), the second parameter must be a cell array in which each entry is a scalar symbolic variable, and the third parameter must be a cell array the same size as the second parameter
A = sym('a', [2, 2]);
B = sym('b', [2, 2]);
C=A*B;
Anum = rand(2,2);
Bnum = rand(2,2);
Ac = num2cell(A);
Bc = num2cell(B);
Anc = num2cell(Anum);
Bnc = num2cell(Bnum);
subs(C, [Ac, Bc], [Anc, Bnc])
ans = 
note here that [Ac, Bc] is a cell array, and [Anc, Bnc] is a cell array the same size
zym
zym 2022 年 7 月 27 日
Thank you for the clarification. This is very useful

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2021 年 7 月 10 日

0 投票

Read about double.
A = subs(A, A, rand(20, 3));
A = double(A) ;

3 件のコメント

Nishanth Rao
Nishanth Rao 2021 年 7 月 10 日
Please go through the question, I am asking how to evaluate matrix B, and not A
KSSV
KSSV 2021 年 7 月 10 日
subs(B) what you want to substitute here? In the above line you have substituted in A. Also the dimensions of A and B are different. How you want to substitute?
Nishanth Rao
Nishanth Rao 2021 年 7 月 10 日
編集済み: Nishanth Rao 2021 年 7 月 10 日
Please look at the accepted answer. I am sorry if the question isn't clear. For your convenience, I'll explain it here.
I have a symbolic matrix A. Now the matrix B (may have different size than A), is some function of the matrix A. Or to be even clearer, the matrix B depends on some of the elements of A. In general, I can write it mathematically as . Now, lets say from say sensor measurements (for eg.,), I will get all the numerical entries of A. How do I calculate the matrix B?

サインインしてコメントする。

製品

リリース

R2020b

質問済み:

2021 年 7 月 10 日

コメント済み:

zym
2022 年 7 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by