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);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.
y = 7 - x;
plot(x,y)
xlabel(’x’)
ylabel(’y’)
hold on
y = (1 + x)/3;
plot(x,y)
axis(’equal’)
grid
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 ??.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);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 ??.
z = 2*x - 3*y + 2;
surf(x,y,z)
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
hold onThe result, which illustrates that the intersection of two planes in is generally a line, is shown in Figure ??.
z = -2*x + 3*y;
surf(x,y,z)
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
z = 3*x - 0.2*y + 1;
surf(x,y,z)
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];The point of intersection of the three planes is at
b = [2; 0; 1];
x = A\b
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 typex = linspace(-2,5);Then MATLAB responds with
y = x*x - 2*x + 3;
??? Error using ==> *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
Inner matrix dimensions must agree.
x = linspace(-2,5);produces the graph of (??) in Figure ??.
y = x.*x - 2*x + 3;
plot(x,y)
In a similar fashion, MATLAB has the ‘dot’ operations of ./, ., and .ˆ, as well as .*.
Exercises
For example, the system has…
The system has…
For example, one such system is:
- Find a vector normal to the plane .
- Find a vector normal to the plane .
- Find the cosine of the angle between the vectors and .
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.
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.