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

3 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Paul
Paul el 14 de Jul. de 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 comentarios
Bruno Luong
Bruno Luong el 17 de Jul. de 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 el 18 de Jul. de 2022
Editada: Paul el 18 de Jul. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Operating on Diagonal Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by