Solve system of equations with some knowns and unknowns in the same matrix

5 visualizaciones (últimos 30 días)
I am trying to solve for x in the equation Ax=b in matlab, but not all elements of x and b are unknown. I know the value of a few elements in x, and the rest are unknown, and I know the value of the elements in b for the x values that are unknown (ex. if I know x1, I dont know b1. If I dont know x2, I know b2, etc.). How can I automatically and efficiently solve for all of the unknowns in both vectors without manually writing the problem out as a system of equations each time?

Respuesta aceptada

Bruno Luong
Bruno Luong el 25 de Oct. de 2022
Editada: Bruno Luong el 25 de Oct. de 2022
% Random example
A = rand(5,5);
x = rand(5,1),
x = 5×1
0.7978 0.8825 0.0743 0.2101 0.6719
b = A*x,
b = 5×1
1.4535 1.7942 1.2279 1.4185 1.3270
[m,n] = size(A);
% put NaN at the position where x is unknown;
xunknown = randperm(n,3);
x(xunknown) = NaN;
% same for b
bunknown = randperm(m,2);
b(bunknown) = NaN;
% Reconstruct unknown x and then b
xunknown = isnan(x);
bunknown = isnan(b);
x(xunknown)=A(~bunknown,xunknown)\(b(~bunknown)-A(~bunknown,~xunknown)*x(~xunknown))
x = 5×1
0.7978 0.8825 0.0743 0.2101 0.6719
b(bunknown)=A(bunknown,:)*x
b = 5×1
1.4535 1.7942 1.2279 1.4185 1.3270
  2 comentarios
Yusuf
Yusuf el 25 de Oct. de 2022
Thanks for the help. Going off my original question, if only x had knowns and unknowns (b is fully known), how would the solution change?
Bruno Luong
Bruno Luong el 25 de Oct. de 2022
The solution I give is applicable for all cases.

Iniciar sesión para comentar.

Más respuestas (1)

David Goodmanson
David Goodmanson el 26 de Oct. de 2022
Editada: David Goodmanson el 26 de Oct. de 2022
Hi Yusuf,
I'm later on an answer (and losing the race more often, to the extent that there is a race, which to be honest there sometimes is), but here is a slightly different way.
% data
n = 7;
m = 3;
A = rand(n,n);
i = sort(randperm(n,m))'; % index of unknown x
j = setdiff(1:n,i)'; % index of unknown b, the complementary index to i
x = zeros(n,1);
x(j) = rand(size(j)) % known x, zeros elsewhere
b = zeros(n,1);
b(i) = rand(size(i)) % known b, zeros elsewhere
%solve
G = zeros(n,n);
G(:,i) = A(:,i);
G(j,j) = -eye(length(j));
g = b-A(:,j)*x(j);
z = G\g
% z is a vector of all the unknowns,
% x(i) for the i indices
% b(j) for the j indices
% check
x(i) = z(i); % insert unknowns into orginal x vector
b(j) = z(j); % insert unknowns into orginal b vector
A*x-b % should be small
  2 comentarios
Bruno Luong
Bruno Luong el 26 de Oct. de 2022
Hi David;
My comments on yor method:
  • probably specific sparse construction of G when A is sparse
  • The eye and b on j components can be multiplied by a constants in both side, and might be some care needed to adjust the consant so as the condition of G is not bad (relative to that of A) when MATLAB internal factorization method such as QR during the "\'.
David Goodmanson
David Goodmanson el 26 de Oct. de 2022
Hi Bruno, that's a reasonable point about scaling part of the matrix I called G. I like the method because it gives all the unknowns in one go with backslash, different from the standard (and probably more foolproof) two-step process that you used.

Iniciar sesión para comentar.

Categorías

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

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by