Function to solve unknowns in Ka=b. Where K is a square matrix fully known, however, knowns and unknowns are scattered in a & b.
2 views (last 30 days)
Show older comments
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:
- K is a square matrix fully known
- a & b are column matrices with knowns and unknowns
- dimensions can vary but assume they are always compatible.
4 Comments
Bruno Luong
on 16 Nov 2020
Edited: Bruno Luong
on 16 Nov 2020
This is a careless example given: rank Ksys is 1, so not invertible.
Answers (1)
Bruno Luong
on 16 Nov 2020
Edited: Bruno Luong
on 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)
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!