system of linear equations with unorganized unknowns
Show older comments
Hi everybody
First, I want to apologize if my question is answered already, but I couldn't find proper easy answer.
I have system of equation formed as [k]*[u]=[f] in which [k] is known and [u] and [f] have known and unknown variables. the matrix [k] is n by n and [f], [u] are n by 1 matrixes. I want to know what is the simplest way to solve this system of equation?
I am beginner in MATLAB and thanks in advance.
4 Comments
darova
on 11 Apr 2020
Are number of uknowns and number of equations equal?
Kasra Shamsaei
on 11 Apr 2020
darova
on 11 Apr 2020
Try equationsToMatrix if the system is linear
Try fsolve if it's not
Kasra Shamsaei
on 11 Apr 2020
Answers (2)
darova
on 11 Apr 2020
Here you go
u = sym('u',[10 1]);
f = sym('f',[10 1]);
f([2 4 6]) = 1; % constants
u([1 3 5 7:10]) = 1; % constants
k = rand(10); % coefficinet matrix
eqns = k*u-f; % symbolic equations
vars = symvar(eqns); % get all unknown variables
[A,B] = equationsToMatrix(eqns,vars);
A = double(A);
B = double(B);
res = A\B;
str = arrayfun(@char, vars, 'uniform', 0)';
table(str,res)
Ameer Hamza
on 11 Apr 2020
Edited: Ameer Hamza
on 11 Apr 2020
In most cases, if your matrix k is general, then the most efficient way is
u = k\f;
The above method is also recommended by MATLAB's documentation. But if matrix 'k' has some special properties such as it is a triangular matrix, then linsolve can give speed gain: https://www.mathworks.com/help/releases/R2020a/matlab/ref/linsolve.html#mw_f405bef0-76ac-4c5b-8c40-c65d1a0a331e.
Categories
Find more on Linear Algebra 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!