In this section we discuss how to use MATLAB graphics to solve systems of linear equations in two and three unknowns. We begin with two dimensions.

Linear Equations in Two Dimensions

The set of all solutions to the equation

is a straight line in the plane; this line has slope and -intercept equal to . We can use MATLAB to plot the solutions to this equation — though some understanding of the way MATLAB works is needed.

The plot command in MATLAB plots a sequence of points in the plane, as follows. Let and be vectors. Then

plot(X,Y)

will plot the points , , …, in the -plane.

To plot points on the line (??) we need to enter the -coordinates of the points we wish to plot. If we want to plot a hundred points, we would be facing a tedious task. MATLAB has a command to simplify this task. Typing

x = linspace(-5,5,100);

produces a vector with entries with the entry equal to , the last entry equal to , and the remaining entries equally spaced between and . MATLAB has another command that allows us to create a vector of points x. In this command we specify the distance between points rather than the number of points. That command is:
x = -5:0.1:5;
                                                                  

                                                                  

Producing x by either command is acceptable.

Typing

y = 2*x - 6;

produces a vector whose entries correspond to the -coordinates of points on the line (??). Then typing
plot(x,y)

produces the desired plot. It is useful to label the axes on this figure, which is accomplished by typing
xlabel(’x’)
 
ylabel(’y’)

We can now use MATLAB to solve the equation (??) graphically. Recall that (??) is: A solution to this system of equations is a point that lies on both lines in the system. Suppose that we search for a solution to this system that has an -coordinate between and . Then type the commands

x = linspace(-3,7,100);
 
y = 7 - x;  
plot(x,y)  
xlabel(’x’)  
ylabel(’y’)  
hold on  
y = (1 + x)/3;  
plot(x,y)  
axis(’equal’)  
grid

The MATLAB command hold on tells MATLAB to keep the present figure and to add the information that follows to that figure. The command axis(’equal’) instructs MATLAB to make unit distances on the and axes equal. The last MATLAB command superimposes grid lines. See Figure ??. From this figure you can see that the solution to this system is , which we already knew.


PIC

Figure 1: Graph of equations in (??)

There are several principles that follow from this exercise.

  • Solutions to a single linear equation in two variables form a straight line.
  • Solutions to two linear equations in two unknowns lie at the intersection of two straight lines in the plane.

It follows that the solution to two linear equations in two variables is a single point if the lines are not parallel. If these lines are parallel and unequal, then there are no solutions, as there are no points of intersection.

Linear Equations in Three Dimensions

We begin by observing that the set of all solutions to a linear equation in three variables forms a plane. More precisely, the solutions to the equation

form a plane that is perpendicular to the vector — assuming of course that the vector is nonzero.

This fact is most easily proved using the dot product. Recall from Chapter ?? (??) that the dot product is defined by where and . We recall from Chapter ?? (??) the following important fact concerning dot products: if and only if the vectors and are perpendicular.

Suppose that . Consider the plane that is perpendicular to the normal vector and that contains the point . If the point lies in that plane, then is perpendicular to ; that is,

If we use the notation then (??) becomes Setting puts equation (??) into the form (??). In this way we see that the set of solutions to a single linear equation in three variables forms a plane. See Figure ??.
PIC

Figure 2: The plane containing and perpendicular to .

We now use MATLAB to visualize the planes that are solutions to linear equations. Plotting an equation in three dimensions in MATLAB follows a structure similar to the planar plots. Suppose that we wish to plot the solutions to the equation

We can rewrite (??) as It is this function that we actually graph by typing the commands
[x,y] = meshgrid(-5:0.5:5);
 
z = 2*x - 3*y + 2;  
surf(x,y,z)

The first command tells MATLAB to create a square grid in the -plane. Grid points are equally spaced between and at intervals of on both the and axes. The second command tells MATLAB to compute the value of the solution to (??) at each grid point. The third command tells MATLAB to graph the surface containing the points . See Figure ??.


PIC

Figure 3: Graph of (??).

We can now see that solutions to a system of two linear equations in three unknowns consists of points that lie simultaneously on two planes. As long as the normal vectors to these planes are not parallel, the intersection of the two planes will be a line in three dimensions. Indeed, consider the equations

We can graph the solution using MATLAB , as follows. We continue from the previous graph by typing
hold on
 
z = -2*x + 3*y;  
surf(x,y,z)

The result, which illustrates that the intersection of two planes in is generally a line, is shown in Figure ??.


PIC

Figure 4: Line of intersection of two planes.

We can now see geometrically that the solution to three simultaneous linear equations in three unknowns will generally be a point — since generally three planes in three space intersect in a point. To visualize this intersection, as shown in Figure ??, we extend the previous system of equations to

Continuing in MATLAB type
z = 3*x - 0.2*y + 1;
 
surf(x,y,z)


PIC

Figure 5: Point of intersection of three planes.

Unfortunately, visualizing the point of intersection of these planes geometrically does not really help to get an accurate numerical value of the coordinates of this intersection point. However, we can use MATLAB to solve this system accurately. Denote the matrix of coefficients by A, the vector of coefficients on the right hand side by b, and the solution by x. Solve the system in MATLAB by typing

A = [ -2 3 1; 2 -3 1; -3 0.2 1];
 
b = [2; 0; 1];  
x = A\b

The point of intersection of the three planes is at
x =
 
    0.0233  
    0.3488  
    1.0000

Three planes in three dimensional space need not intersect in a single point. For example, if two of the planes are parallel they need not intersect at all. The normal vectors must point in independent directions to guarantee that the intersection is a point. Understanding the notion of independence (it is more complicated than just not being parallel) is part of the subject of linear algebra. MATLAB returns “Inf”, which we have seen previously, when these normal vectors are (approximately) dependent. For example, consider Exercise ??.

Plotting Nonlinear Functions in MATLAB

Suppose that we want to plot the graph of a nonlinear function of a single variable, such as

on the interval using MATLAB. There is a difficulty: How do we enter the term ? For example, suppose that we type
                                                                  

                                                                  
x = linspace(-2,5);
 
y = x*x - 2*x + 3;

Then MATLAB responds with
??? Error using ==> *
 
Inner matrix dimensions must agree.

The problem is that in MATLAB the variable x is a vector of 100 equally spaced points x(1), x(2), …, x(100). What we really need is a vector consisting of entries x(1)*x(1), x(2)*x(2), …, x(100)*x(100). MATLAB has the facility to perform this operation automatically and the syntax for the operation is .* rather than *. So typing
x = linspace(-2,5);
 
y = x.*x - 2*x + 3;  
plot(x,y)

produces the graph of (??) in Figure ??.


PIC

Figure 6: Graph of .

In a similar fashion, MATLAB has the ‘dot’ operations of ./, ., and .ˆ, as well as .*.

Exercises

Find the equation for the plane perpendicular to the vector and containing the point . The equation of the desired plane is .
Note that a vector perpendicular to a plane is orthogonal to any vector connecting two points in the plane. So, if is one point in the plane perpendicular to and is any other point in that plane, then . Substituting into this formula, we obtain
Determine three systems of two linear equations in two unknowns so that the first system has a unique solution, the second system has an infinite number of solutions, and the third system has no solutions.

For example, the system has…

No solutions. Exactly one solution. Infinitely many solutions.

The system has…

No solutions. Exactly one solution. Infinitely many solutions.
while the system has…
No solutions. Exactly one solution. Infinitely many solutions.
Write the equation of the plane through the origin containing the vectors and . The equation for the plane is .
Note that the plane goes through the origin and contains the vectors and , and therefore contains the points , , and . The general equation for a plane is . We can substitute the coordinates of the three points into this equation to get the linear system We can solve the system by substitution to get and , which yields the equation of the plane.
Find a system of two linear equations in three unknowns whose solution set is the line consisting of scalar multiples of the vector .

For example, one such system is:

The solution set contains all multiples of the vector , so it contains the origin, since . The equation of any plane containing the origin is . Substituting the point implies that . Any two equations which satisfy that condition and are not multiples of one another will have the appropriate line as a solution set. For example, let in the first equation, and in the second equation to obtain the system given here.
  • Find a vector normal to the plane .
  • Find a vector normal to the plane .
  • Find the cosine of the angle between the vectors and .

Determine graphically the geometry of the set of solutions to the system of equations in the three unknowns : by sketching the plane of solutions for each equation individually. Describe in words why there are no solutions to this system. (Use MATLAB graphics to verify your sketch. Note that you should enter the last equation as z = 2 - 0*x - 0*y and the first two equations with 0*y terms. Try different views — but include view([0 1 0]) as one view.)
Use MATLAB to solve graphically the planar system of linear equations to an accuracy of two decimal points.

Hint: The MATLAB command zoom on allows us to view the plot in a window whose axes are one-half those of original. Each time you click with the mouse on a point, the axes’ limits are halved and centered at the designated point. Coupling zoom on with grid on allows you to determine approximate numerical values for the intersection point.

Use MATLAB to solve graphically the planar system of linear equations to an accuracy of two decimal points.
Use MATLAB to find an approximate graphical solution to the three dimensional system of linear equations Then use MATLAB to find an exact solution.
Use MATLAB to determine graphically the geometry of the set of solutions to the system of equations: Attempt to use MATLAB to find an exact solution to this system and discuss the implications of your calculations.

Hint: After setting up the graphics display in MATLAB, you can use the command view([0,1,0]) to get a better view of the solution point.

Use MATLAB to graph the function on the interval . How many relative maxima does this function have on this interval?