Solving linear matrix equation
5 views (last 30 days)
Show older comments
Hi. I want to slove a linear matrix equation AX=B by this code : X=A\B When i run this code it will show me the solved X matrix. But i can not understand that which value is for which variable. It just show me the X matrix without showing the name of each variable.i had defined my variables by sym. And there are too many variables that i can not name them one by one in the code.for example , There are variables ,for example x y z ... in my equations. How can i understand which cell of X matrix us the value of x or y or z?! Please help me!!
6 Comments
Answers (1)
John D'Errico
on 5 Jun 2018
Edited: John D'Errico
on 5 Jun 2018
I think you do not understand how MATLAB works, how it returns results? Perhaps you have only used other tools like Excel or Mathematics, that will often show results somewhat differently.
When you write this:
A = rand(5,5);
B = rand(5,1);
X = A\B;
MATLAB returns the result in a vector X.
X =
-0.84086
3.7698
3.8571
-8.0047
2.4444
But MATLAB does not name the individual components of the vector. They are simply
X(1), X(2), X(3), ...
That is the "name" associated with an element of a vector (or an array). Thus we can extract an element as:
X(1)
ans =
-0.84086
If you really felt you needed named variables, you could have used the symbolic toolbox, formulating a linear system of equations in those variables, then solving the system. The result would have had named variables containing the desired values. But it really is not necessary here, and it would be much more slowly computed in symbolic form. Essentially, that would be vast overkill for a simple problem.
4 Comments
John D'Errico
on 5 Jun 2018
Edited: John D'Errico
on 5 Jun 2018
Sorry, but you make no sense in context of this discussion: "my variables are arrays from a matrix". And what do the presence of some zeros have to do with anything? Zeros are just a number.
You have some unknowns that you wish to solve for. Why not just use solve? Let me guess. You have either more or fewer unknowns than equations. So solve found no solution, for good reason. Either there are no solutions at all, or there are infinitely many solutions.
I think a big problem in this question is that I am trying to chase a moving target. Every time you get an answer, the question changes, and you reveal some additional aspect of some totally different and hidden question.
If you insist on using equationstomatrix, then you need to learn how to use it properly. The problem is not with the solve. You need to formulate the problem properly, as otherwise you will get random garbage from any solver. But we are still peeking around corners at the problem, not being told the entire problem.
So go back to equationstomatrix. Learn to use it, and to use it properly. Verify that the matrix it produces is one that makes sense, and where the variables are known. Only then should you try to solve anything.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!