Once you have finished this chapter, you should be able to solve by hand systems of linear algebraic equations that have few variables and use MATLAB to solve1 systems that have many variables. If the number of equations in the set equalthe number of unknown variables, the matrix A is square and MATLAB provides two ways of solving the equation set Ax = b:
1. The matrix inverse method; solve for x by typing x = inv (A) *b.
2. The matrix left-division method; solve for x by typing x = A \b,
If A is square and if MATLAB does not generate an error message when you use one of these methods, then the set has a unique solution, which is given by the left-division method. You can always check the solution for x by typing A*x to see if the result is the same as b. If so, the solution is correct. If you receive an error message, the set is under-determined, and either it does not have a solution or it has more than one solution. In such a case, if you need more information, you must use the following procedures.
For under-determined and over-determined sets, MATLAB provides three ways of dealing with the equation set Ax = b. (Note that the matrix inverse method will never work with such sets.)
- The matrix left-division method; solve for x by typing x = A\B.
- The pseudoinverse method; solve for x by typing x = pinv (A) *b.
- The reduced row echelon form (RREF) method. This method uses the MATLAB command rref to obtain a solution.
Table 6.6-1 summarizes the appropriate commands. You should be able to determine whether a unique solution, an infinite number of solutions, or no solution exists. You can get this information by testing for existence and uniqueness of solutions using the following test.
Existence and uniqueness of solutions. The set Ax = b with m equations and n unknowns has solutions if and only if rank[A] = rank[A b] (I). Let r = rank[A]. If condition (I) is satisfied and if r = n, then the solution is unique. If condition (I) is satisfied but r < n, an infinite number of solutions exists and r unknown variables can be expressed as linear combinations of the other n – r unknown variables, whose values are arbitrary
. Homogeneous case. The homogeneous set Ax = 0 is a special case in which b = O.
For this case rank[A] = rank[A b] always, and thus the set always has the trivial solution x =O. A nonzero solution. in which at least one unknown is nonzero. exists
if and only if rank[A] < n. If m < n, the homogeneous set always has a nonzero solution.
In an under-determined system not enough information is given to determine the values of all the unknown variables.
- An infinite number of solutions might exist in which one or more of the unknowns are dependent on the remaining unknowns.
- For such systems Cramer’s method and the matrix inverse method will not work because either A is not square or because IAI = 0.
- The left-division method will give a solution with some of the unknowns arbitrarily set equal to zero, but this solution is not the general solution.
- An infinite number of solutions might exist even when the number of equations equals the number of unknowns. The left-division method fails to give a solution in such cases.
- In cases that have an infinite number of solutions, some of the unknowns can be expressed in terms of the remaining unknowns, whose values are arbitrary. The rref command can be used to find these relations.
An over-determined system is a set of equations that has more independent equations than unknowns.
- For such a system Cramer’s method and the matrix inverse method will not work because the A matrix is not square.
- Some over-determined systems have exact solutions, which can be obtained with the left-division method A\b,
- For over-determined systems that have no exact solution, the answer given by the left-division method satisfies the equation set only in a least squares sense.
- When we use MATLAB to solve an over-determined set, the program does not tell us whether the solution is exact. We must determine this information ourselves. The first step is to check the ranks of A and [A b] to see whether a solution exists; if no solution exists, then we know that the left-division solution is a least squares answer.
In this chapter you saw that the set of linear algebraic equations Ax = b with m equations and n unknowns has solutions if and only if (I) rank[A] =rank[A b). Let r = rank[A]. If condition (I) is satisfied and if r = n then the solution is unique. If condition (I) is satisfied but r < n, an infinite number of solutions exists; in addition, r unknown variables can be expressed as linear combinations of the other n – r unknown variables, whose values are arbitrary. In this case we can use the rref command to find the relations between the variables. The pseudocode in Table 6.6-2 can be used to outline an equation solver program before writing it.
A condensed flowchart appears in Figure 6.6-1. From this chart or the pseudocode, we can develop the script file shown in Table 6.6-3. The program uses
Table 6.6-2 Pseudocode for the linear equation solver
If the rank of A equals the rank of [A b], then determine whether the rank of A equals the number of unknowns. If so. there is a unique solution, which can be computed using left division. Display the results and stop. Otherwise, there is an infinite number of solutions, which can be found from the augmented matrix . Display the results and stop. Otherwise (if the rank of A does not equal the rank of [A bD, then there are no solutions. Display this message and stop.
Table 6.6-3 MATLAB program to solve linear equations
% Script file lineq.m
% Solves the set Ax = b; given A and b.
% Check the ranks of A and [A b].
if rank(A) == rank([A b])
% The ranks are equal.
size_A = size(A);
% Does the rank of A equal the number of unknowns?
if rank(A) == size_A(2)
% Yes. Rank of A equals the number of unknowns.
disp(‘There is a unique solution, which is:’)
x = A\b % Solve using left division.
% Rank of A does not equal the number of unknowns.
disp(‘There is an infinite number’ of solutions.’)
disp(‘The augmented matrix of the reduced system is:’)
rref([A b]) % Compute the augmented matrix.
% The ranks of A and [A b] are not equal.
disp(‘There are no solutions.’)
the given arrays A and b to check the rank conditions, the left-division method to obtain the solution, if one exists, and the rref method if there is an infinite number of solutions. Note that the number of unknowns equals the number of columns in A, which is given by s i z e_A ( 2 ) , the second element in s i z e_A. Note also that the rank of A cannot exceed the number of columns- in A.
Test Your Understanding
16.6-1 Type in the script file lineq. In given in Table 6.6-3and run it for the following cases. Hand check the answers.
a. A [l,-l;l,l],b = [3;5)
~ A [l,-1;2,-2),b [3;6)
c. A [1, -1 ; 2 , – 2 ) ,b = [ 3 ; 5)