Azzera filtri
Azzera filtri

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 visualizzazione (ultimi 30 giorni)
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 Commenti

Accedi per commentare.

Risposte (1)

Bruno Luong
Bruno Luong il 16 Nov 2020
Modificato: Bruno Luong il 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)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by