Knowing when a problem is expressible as a set of coupled, linear algebraic equations is key to using the mathematics of linear algebra to get a solution to your problem. To use the methods of linear algebra to solve your problem, you need to generate two key objects:
Once these are identified and created, you can then get a solution to your problem.
Procedure for setting up a matrix problem and getting it set up in MATLAB.
Example: The equations
x + 4y = 2z x + 2xy - z2 = 14
do not form a linear set of equations due to the mixed term, 2xy
and the squared z-term.
To save time and reduce the chance for errors, pick an ordering for the unknowns and use that ordering in all equations. Also, if an unknown is missing from an equation, write that unknown into your equations by giving it a coefficient of 0 for the particular equation. Be sure to include zeros on the right-hand side if no constant is involved.
Example: If your equations look like
t + 2z - w = 5 w = t - 6z 5 - 4t = z
then pick an ordering for the unknowns (t, w and z, in this example) and re-write them as
t - w + 2z = 5 t - w - 6z = 0 4t - 0w + z = 5
Lining up the variables in each equation is a Very Good Thing as it
helps you visually check your work.
Example: Using the final results from the last example, the rows of the coefficient matrix for this problem are
Eq. 1/Row 1: 1 -1 2 Eq. 2/Row 2: 1 -1 -6 Eq. 3/Row 3: 4 0 1
Example: Using the example above,
Eq. 1/Row 1: 5 Eq. 2/Row 2: 0 Eq. 3/Row 3: 5
Example: The example used so far is small enough to do at the command line.
A = [1 -1 2 % row #1 - starts the definition 1 -1 -6 % row #2 4 0 1] % row 3 - ends the definition b = [5 0 5]' % the transpose is important!
These may sound trivial for the example just given but, for larger problems, taking time to ensure a correct set-up is critical for getting meaningful results (if you get results at all!).
If you have successfully translated your equations to a coefficient
matrix/right-hand side vector pair, you are ready to find
solutions for the
unknown quanitities in your problem.
WARNINGJust because you have sucessfully created a coefficient matrix and right-hand side vector that represents your linear system of equations, do not assume that you will be able to find a solution. Beyond the mundane problems of typographical errors is the issue of whether or not a set of linear equations actually has a solution. For example, your set of equations may contain an equation that is a (linear) combination of two (or more) other equations in the set. Such an equation is called a linearly dependent equation. Linearly dependent equations are redundant equations - they provide no new information about the connection of the variables in the problem. When present, linearly dependent equations will stop you from obtaining a solution. A discussion of these issues is given below. |
If you have the coefficient matrix stored in an array called A and the right-hand side vector in an array called b, then the solution to your problem is simply
x = A\b
which you should read as "A under b" or "b divided by A from the left".
That's it, from the solution procedure point of view. A few more
details about solving sets of equations are available in the
linear algebra summary page.
OK. So A\b did not "work". Either you didn't get a solution or the solution is "wrong" in some sense. What could be the problem? Lots of things, most of which are not related to MATLAB (directly). Each problem is unique, more or less, and the following items give some avenues that might help you find what is "wrong" with your problem.
rank(A)
If the rank is not N, then there are N-rank(A)
linearly dependent equations in your set. The problem usually is that
you do not know which ones are linearly dependent. If this is your
stumbling block, the best solution is to carefully review the
formulation of your problem and the way it lead to the system of
linear equations you are using. The discussion of
solvability may help here.
cond(A)
Qualitatively, the (magnitude of the) logarithm of the condition number is about how may digits of accuracy you lose in the result you compute. For MATLAB (which operates using "double precision"), you have at most 15 or 16 digits to work with and so condition numbers above 1e10 should be cause for serious concern about accuracy.
A fundamental concern of the topic of linear algebra is to detemine the structure of the solutions to sets of linear equations. There are many nuances associated with this issue and a good book on linear algebra is recommended if you need more answers than I present here. I find the 3rd edition of Noble and Daniels' "Applied Linear Algebra" (Prentice-Hall, 1988) to be an excellent reference if you are interested in getting a good hold on these ideas. The information that follows in this section is taken from Chapter 4 of this source.
Given a linear system represented by the coefficient matrix, A, and right-hand side vector, b, then three possibilities exist for the solution x of Ax = b:
A 2-by-2 system of equations simply illustrates these possibilites since you can visualize the nature of the solutions by drawing lines in 2D space.
The existence of solutions is usually described in terms of the rank of the coefficient matrix. The rank of a matrix tells you the number of independent rows (or columns) in the matrix and hence tell you the number of independent equations that are in your set. To get a unique solution, you need to have the rank of your coefficient matrix equal to the number of unknowns in your problem.
The following table summarizes the solvability of a system of equations
in terms to the rank of various matrices:
| The system Ax = b has ... | when ... |
|---|---|
| no solution | rank(A) < rank([A b]) |
| one solution | rank(A) = rank([A b]) = N |
| infinite number of solutions | rank(A) = rank([A b]) < N |
where A is M-by-N, x is N-by-1 and
b is M-by-1. The notation [A b] means that
b is appended to A as an additional column. The MATLAB
function rank will determine the rank of a matrix. See the
online help information for more on this function.