Effacer les filtres
Effacer les filtres

Function to solve unknowns in Ka=b. Where K is a square matrix fully known, however, knowns and unknowns are scattered in a & b.

1 vue (au cours des 30 derniers jours)
Hello,
I am trying to write a function to solve unknowns in Ka=b. The tricky part is unknowns are scattered in a & b and it has to work for different compatible matrix sizes. The inputs are K, a, b and I am trying to output the solved variables in a & b. Are there functions already similar to this? How would you go about this process wise?
Assume:
  1. K is a square matrix fully known
  2. a & b are column matrices with knowns and unknowns
  3. dimensions can vary but assume they are always compatible.
  4 commentaires
Bruno Luong
Bruno Luong le 16 Nov 2020
Modifié(e) : Bruno Luong le 16 Nov 2020
This is a careless example given: rank Ksys is 1, so not invertible.
JT
JT le 16 Nov 2020
Apologies. Assume Ksys is an invertible matrix.

Connectez-vous pour commenter.

Réponses (1)

Bruno Luong
Bruno Luong le 16 Nov 2020
Modifié(e) : Bruno Luong le 16 Nov 2020
You might need to carry out some normalization of a, b so that they are unitless before doing this.
% Generate test data
m = 12;
n = 10;
K = rand(m,n);
a = rand(n,1);
b = K*a;
known_a=rand(size(a))>0.4;
known_b=rand(size(b))>0.4;
% Solve a(~known_a) and b(~known_b)
% from K, a(known_a) and b(known_b)
tlsqflag = true; % recommend true
[m,n] = size(K);
M = [K, -eye(m)];
known_ab = [known_a(:); known_b(:)];
X = zeros(m+n,1);
X(known_ab,1) = [a(known_a); b(known_b)];
if ~tlsqflag
% not recommended
Y = -M*X;
X(~known_ab) = M(:,~known_ab)\Y;
else
% recommended method
[~,~,V] = svd([M(:,~known_ab), M*X],0);
Vend = V(:,end);
X(~known_ab) = Vend(1:end-1)/Vend(end);
end
a = X(1:n)
b = X(n+1:end)
% Check residu
norm(K*a-b)/norm(b)

Catégories

En savoir plus sur Creating and Concatenating 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