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

3 vues (au cours des 30 derniers jours)
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

Réponse acceptée

Paul
Paul le 14 Juil 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 commentaires
Bruno Luong
Bruno Luong le 17 Juil 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 le 18 Juil 2022
Modifié(e) : Paul le 18 Juil 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Operating on Diagonal Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by