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

조회 수: 5 (최근 30일)
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
Bruno Luong
Bruno Luong 2020년 11월 16일
편집: Bruno Luong 2020년 11월 16일
This is a careless example given: rank Ksys is 1, so not invertible.

댓글을 달려면 로그인하십시오.

답변 (1개)

Bruno Luong
Bruno Luong 2020년 11월 16일
편집: Bruno Luong 2020년 11월 16일
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)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by