Is it possible to solve multiple linear systems of equations in parallel with one matrix operation?

3 ビュー (過去 30 日間)
I'm wondering if there's a way to do the following calculations in one go (i.e. without the for loop).
V = nan([na nv]);
for i=1:nv
% Solve homogenous system of equations
V(:,i) = [Aa-L(i)*Ia] \ (-Ba*Phi(i));
end
For the purposes of this example, let's consider the variables having the following values:
Aa = [ 0.819 0;
-0.544 1];
na = size(Aa,2);
Ba = [1; 0];
Phi = [1 1];
L = [0.7515 0.7165];
nv = numel(L);
Ia = eye(na);
Which yields the solution:
V =
-14.8148 -9.7561
-32.4316 -18.7207

採用された回答

Paul
Paul 2022 年 7 月 14 日
Hi Bill,
pagemldivide introduced in 2022a can do the trick. Whether or not this is really better than the loop ....
Aa = [ 0.819 0;
-0.544 1];
Ba = [1; 0];
Phi = [1 1];
L = [0.7515 0.7165];
V = reshape(pagemldivide(Aa - reshape(L,1,1,[]) .* eye(size(Aa)) , -Ba .* reshape(Phi,1,1,[]) ) , numel(Ba),numel(L))
V = 2×2
-14.8148 -9.7561 -32.4316 -18.7207
  5 件のコメント
Bruno Luong
Bruno Luong 2022 年 7 月 17 日
@Paul But, I am surprised by this ...
It looks like the discrepency occurs when matrix size is between 2 and 9, beyond that pagemldivide seems to match backslash.
nmax = 100;
errx = zeros(1,nmax);
for n=1:nmax
A = rand(n,n,10);
B = rand(n,n,10);
X = pagemldivide(A,B);
errx(n) = norm(X(:,:,end)-A(:,:,end)\B(:,:,end));
end
plot(1:nmax, errx);
xlabel('Matrix size');
ylabel('Error betwen pagemldivide');
Paul
Paul 2022 年 7 月 18 日
編集済み: Paul 2022 年 7 月 18 日
I as surprised by this too. Never would have occurred to me to test for different dimensions, I'm glad it occurred to you.
Further discussion here.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by