Substitute symbolic matrices into numerical matrices

1 回表示 (過去 30 日間)
Ali Daher
Ali Daher 2022 年 9 月 25 日
コメント済み: Torsten 2022 年 9 月 25 日
Let's say I am solving a linear system of Equations of the form: Ax=b at each timestep. Matrix A always always has the same structure, for instance A = [a b c; -a 2b c-d; -d 2c a]. During each timestep iteration I update the values of a, b, c ,d, but the structure of A (and of b) remains the same in terms of those symbols. Is there a way to construct a symbolic matrix and then just substitute the symbols with their updated values at each timestep rather than reconstruct the whole A matrix (and b vector) at each timestep?
Many thanks!

回答 (1 件)

Torsten
Torsten 2022 年 9 月 25 日
編集済み: Torsten 2022 年 9 月 25 日
Do you know Cramer's rule ?
A = sym('A',[3 3]);
b = sym('b',[3 1]);
x = sym('x',[3 1]);
sol = solve(A*x==b,x)
sol = struct with fields:
x1: (A1_2*A2_3*b3 - A1_3*A2_2*b3 - A1_2*A3_3*b2 + A1_3*A3_2*b2 + A2_2*A3_3*b1 - A2_3*A3_2*b1)/(A1_1*A2_2*A3_3 - A1_1*A2_3*A3_2 - A1_2*A2_1*A3_3 + A1_2*A2_3*A3_1 + A1_3*A2_1*A3_2 - A1_3*A2_2*A3_1) x2: -(A1_1*A2_3*b3 - A1_3*A2_1*b3 - A1_1*A3_3*b2 + A1_3*A3_1*b2 + A2_1*A3_3*b1 - A2_3*A3_1*b1)/(A1_1*A2_2*A3_3 - A1_1*A2_3*A3_2 - A1_2*A2_1*A3_3 + A1_2*A2_3*A3_1 + A1_3*A2_1*A3_2 - A1_3*A2_2*A3_1) x3: (A1_1*A2_2*b3 - A1_2*A2_1*b3 - A1_1*A3_2*b2 + A1_2*A3_1*b2 + A2_1*A3_2*b1 - A2_2*A3_1*b1)/(A1_1*A2_2*A3_3 - A1_1*A2_3*A3_2 - A1_2*A2_1*A3_3 + A1_2*A2_3*A3_1 + A1_3*A2_1*A3_2 - A1_3*A2_2*A3_1)
And now please don't tell me that in reality, your matrix A is 50x50 :-)
  2 件のコメント
Ali Daher
Ali Daher 2022 年 9 月 25 日
In reality, my matrix is yeah around 100x100 =D.
Torsten
Torsten 2022 年 9 月 25 日
And for such a matrix size, you use symbolic maths ? You will have much time to drink your coffee ...
Use numerical matrices and backslash without any structural analysis of the matrix:
A = rand(100);
b = rand(100,1);
x = A\b
x = 100×1
0.2890 0.3542 0.0288 -0.6519 -0.9014 1.1733 0.4072 0.8347 -0.2271 0.7861

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

カテゴリ

Help Center および File ExchangeLinear Algebra についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by