Solving Equations with Excel

advertisement
Solving Equations with Excel
One of the most important uses of computers in mathematics is to solve systems of equations.
Later, we shall see examples of equations that cannot be solved, except with the use of a
computer. In this section, we shall introduce one of the most powerful tools that comes with
Excel, The Excel Solver. This solver can be used to solve systems of equations as well as to
find maxima and minima. Here we show how to solve some simple equations using Solver.
One equation with one variable
Example 1
Let us use the Solver to solve equation 3x + 10 = x + 4. It simplifies the solution process if we
rewrite the equation to 3x + 10 – x – 4 = 0. Set up the spreadsheet so that it looks like Figure
1. The entries in row 1 indicate the contents in row 2. Cell A2 contains the argument x. x = 0
is entered as a preliminary guess. In cell B2 is the formula that corresponds to the equation we
want to solve. The formula takes the content of A2 as an argument. You can see how the
formula is entered in the formula bar. In cell B2 you see the result, 6, when the initial guess
on x is 0.
Figure 1, Solving 3x + 10 – x – 4 = 0
In order to solve the equation you now load the Solver tool. You find this tool on the Tools
menu in Excel.1 After loading the Solver, set the Solver up to solve the problem. You do this
by asking the Solver to set the content of cell B2 equal to zero by changing the content of A2.
The Solver Dialog box should look like Figure 2.
1
If you cannot find Solver on the Tools menu, the Solver has probably not been
installed. Consult the Excel documentation on how to install this feature.
1
Figure 2, Solver dialog box for the problem 3x + 10 – x – 4 = 0
Pressing Solve makes solver compute an answer. A dialog box appears that informs you
whether the Solver found a solution or not. With a linear problem in one variable, the Solver
will always find a solution. The spreadsheet should now look like
Figure 3.
Figure 3, The solution for the problem for 3x + 10 – x – 4 = 0
2
The dialog box reports that Solver has found a solution. In cell A2 the value of x that solves
the problem is reported to be x = –3. In cell B2 one can see that 3(-3) + 10 – (–3) – 4 actually
equals zero.
This example was easy. But for a number of reasons, Solver does not always find a solution.
Consider the equation
z
1
5
. Setting up the Solver in the same way as the
 
z  5 3 5 z
previous problem makes the Solver report that it could not find a feasible solution. It is easy
to see what goes wrong:
z
1
 5 = 4/3 for all values of z except x = 5 and x = –5
 
z  5 3 5 z
where the function is not defined. Consider now the equation x2 – 9= 0, whose solutions are x
= 3 and x = –3. Now try to solve this equation with the Solver. First enter the formula =A5^29 into cell B5. Then enter x = 0 as an initial guess in cell A5. Asking the Solver to find the
value in A5 that sets the expression in B5 equal to zero, will make Solver report either x = 3
or x = –3. But it will only report one solution!
We have now seen two problems that we may encounter when solving equations numerically.
1) We may try to solve equations that have no solution and 2) We may underreport the
number of solutions since the Solver reports only one solution at a time. We shall encounter
more problems later in this text. You should however note, that even with simple problems,
the Solver must be used with care and the results must be carefully interpreted.
Exercises
Use the Solver to solve the problems in the Further Problems for Section 2.3 in EMEA.
Solving equations and with two or more unknowns.
In this section we examine how to solve general systems of equations.
Example 2
Consider the system
2x  3y  18
3x  4y   7
There are many ways to solve this system in Excel. In the linear algebra chapter we examine
several methods that do not use the Solver. There are also several ways to set up the problem
3
with the Solver. The method we use here is to ask the solver to set one equation equal to zero,
while specifying other equations as constraints. Rewrite the first equation to 2x + 3y – 18 = 0
and the second equation to 3x – 4y + 7 = 0. Enter 1 into the cells in A2:B2. Enter
=2*A2+3*B2-18 into cell D2 and =3*A2-4*B2+7 into cell D3. The spreadsheet should look
like Figure 4.
Figure 4. Solving a system of equations
Now load the Solver and set it up so that is sets the content of D2 to zero by changing the
variables in A2:B2. We must also specify the constraint that the content of D3 should be equal
to zero. This is done by clicking the Add button. A new dialog box appears. After specifying
that the content of D3 should be equal to zero, this dialog box should look like Figure 5.
Figure 5. Adding constraints when solving equation systems
Insert the constraint in the Add Constraint dialog box and click OK. The Solver dialog box
should appear and look like Figure 6. Clicking Solve finds the solution x = 3 and y = 4.
4
Figure 6, Solver dialog box for solving 2x + 3y = 18 and 3x – 4y = –7
Example 3
The previous problem was linear. Let us now examine a problem that is a bit more
complicated. Lets try to find the solution of x = y2 and x + y = 2. Before solving the problem,
we should draw graphs of the equations to see if we can derive some useful information from
the graphs. If possible, you should always try to draw a picture like this. This is done in
Figure 7. We can see that the system appears to have two solutions. To solve the problem
with the Solver, enter the formula =A2-B2^2 into cell D2 and =A2+B2-2 into D3. Enter the
initial values x = 0 and y = 0 into cells A2:B2, then set up the Solver as above.
5
3
y  x2
x y 2
2
1
y
0
0
0.5
1
1.5
2
2.5
3
3.5
4
4.5
-1
-2
-3
x
Figure 7, x = y2 and x + y = 2
After setting the problem up like this, the Solver dialog box should still look like Figure 6.
Clicking Solve makes the solver report the solution x = 1 and y = 1. We know from Figure 7
that there is an additional solution. In order to find this solution, we must start with different
initial guesses for x and y. Trying with x = 2 and y = 2, does not help becuase the Solver again
reports the solution x = 1 and y = 1. If we start with x = 5 and y = –3, the Solver reports the
other solution x = 4 and y = –2. We can see that we had to try several starting points to find
the second solution. In this case, we were lucky because our drawing had indicated that there
are actually two solutions. In many cases, especially if we have more than two unknowns, it is
impossible to draw comprehensible figures that indicate the number of solutions. So,
numerically finding all solutions to a problem is hard and requires that we try to solve the
problem with a number of starting points. Also, it is not the case that the solver will find the
solutions that are “closest” to the starting point. The relationship between starting points and
solutions is far beyond the scope of this booklet, but it is worth noting that the seemingly
chaotic relationship between the starting point and the solution that results from this starting
point was one of the inspirations for the field of mathematics called Chaos theory.
6
5
Exercises
1)
Solve the systems of equations in Problem 1, Section 2.4 in EMEA.
2)
Solve the systems of equations in Problem 2, Section 2.4 in EMEA.
3)
Solve the systems of equations in Problem 4, Section 2.4 in EMEA.
4)
Examine the following two equations in two unknown.
 x 3  10x  10  01
. ex  y
5x  3y  30
a) Make a drawing in Excel that indicates how many solutions the system has.
b) Find all the solutions with the Solver.
Newton's Method – What Goes On Inside The Solver
When you use the Solver, there is a large number of calculations that goes on in the
background without you ever noticing. This section explains one of the simplest algorithms
that a computer can use to find a solution to an equation of the form f(x) = 0. Although the
method is simple, it is still the foundation for many of the algorithms used to solve such
problems. If you click the Options button in the Solver dialog box, you will see that Newton's
method is the default method for solving equations as well as solving maximisation
problems.2 Newton's method is an iterative procedure that starts with an initial guess, x0, of
the solution, uses this initial guess to produce a new guess, x1, and so on. The points generated
by Newton's method are given by the formula
xn  1  xn 
af
af
f xn
,
f  xn
n  0,1,2
Let use this formula to find an approximate value for the zero of f(x) = e2x – e = 0. Choose x0
= 1. Enter this value into the cell A1. We need that f'(x) = 2e2x. Next, enter the formula =A1(exp(2*A1)-exp(1))/(2*exp(2*A1)) into cell A2 and copy the content of cell A2 into A3:A8.
The formulas should look like Figure 8 after they have been entered in the spreadsheet.
2
In chapter 3, you will see that a first step in finding a numerical solution to a
optimisation problem to solve an equation f   x   0 .
7
Figure 8, Newton's method used on e2x – 1 = 0
The result is a sequence that seems to converge to 0.5. Examining f(x) it is clear to see that x =
0.5 is in fact the solution.
Example 4
Let us consider another example where we examine one of the problems that may turn up
when we try to solve equations numerically. Consider the equation
f  x   e 3x  5e x  4  0
A plot of this function is given in Figure 9. The graph reveals that it has two solutions. One
solution where x = 0 and one somewhere in the interval (0.25 0.5).
5
4
3
2
f (x )
1
0
-1
-0.5
0
0.5
-1
-2
x
Figure 9, e3x-5ex + 4
8
1
1.5
2
Let us try to see how easy it is to miss a solution when using Newton’s method. Let us say
that we do not know how many solutions the equation has, but that we will try to check for
solutions and hope that with different starting points we will find all solutions. AS we
obviously cannot start from all points on the real line, we must specify a number of points
from which to start. In Table 1, we have given starting points for Newton’s method and the
solution that Newton’s method found after 20 iterations. Examining the table, we can see
something quite remarkable. Even if the starting points in [-2, –1] are closer to the solution x
= 0, the algorithm overshoots and finds the solution x = 0.445681. There is only a narrow
band of starting points that leads to the solution x = 0. The troubling implication of this is that
it is easy to miss solutions. When solving problems where there is only one unknown, we can
always plot the equation and visually inspect if we have found all solutions.
Table 1, Solutions and starting points
Starting
point
The found
solution
-2
0.445681
-1.75
0.445681
-1.5
0.445681
-1.25
0.445681
-1
0.445681
-0.75
0
-0.5
0
-0.25
0
0
0
0.25
39.20115
0.5
0.445681
0.75
0.445681
1
0.445681
1.25
0.445681
1.5
0.445681
1.75
0.445681
However, if we have problems with more than one unknown it is difficult to make such
drawings and there is always a probability that we may miss solutions. This insight applies to
9
all computer programs that solve equations and not only to our implementation in Excel. In
particular, this problem also applies to the Solver in Excel. Note also the peculiarity when we
start the algorithm from 0.25. Here Newton’s method terminated at the value 39.2 after 20
iterations. This is wildly off the mark. Actually, if we had continued for another 200
iterations, the algorithm would have converged to the solution x
case is that we started from a starting point where f’(x)
0.45. The problem in this
0. Because the algorithm divides by
f’(x), the algorithm takes a huge step far from the initial starting point.
Exercises
Find as many solutions as you can with Newton’s method and the Solver for the following
equations.
a) f(x) = x 3  2x 2  3x  3 = 0
b) f(x) = xe x  1 = 0
c) f(x) = sin x  0.5 x = 0 (The derivative of sinx is cosx)
10
Download