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

2 views (last 30 days)
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

Accepted Answer

Paul
Paul on 14 Jul 2022
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 Comments
Bruno Luong
Bruno Luong on 17 Jul 2022
@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 on 18 Jul 2022
Edited: Paul on 18 Jul 2022
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!