• Aucun résultat trouvé

Finding Roots of Polynomials with Solve and NSolve

Dans le document This page intentionally left blank (Page 163-171)

Graphics Primitives

2. Factor the following expressions and explain the differences in the resulting factorizations

4.2 Finding Roots of Polynomials with Solve and NSolve

Out[10]=

1

4+12 x/ +12 x/

Exercises 4.1

1. Let f+x/ 15x2x310x4.

a. Use a Plot to estimate the real roots of f+x/.

b. Use Factor to find the real roots of f+x/.

2. Factor the following expressions and explain the differences in the resulting factorizations.

a. 1xnxmxnm and 1xx3x4 b. 1x3 and 1xn

c. 1x4 and 1x2n

4.2 Finding Roots of Polynomials with Solve and NSolve

The Factor command together with the Plot command are a powerful set of tools for discovering the real roots of polynomials. But there are a few shortcomings. Notice, for instance, that we can only approximate real roots that happen to be irrational (inexpressible as a quotient of integers). In addition, complex roots (involving the imaginary number Ç, the square root of 1) are completely inaccessible. For these reasons we introduce the NSolve and Solve commands.

4.2 Finding Roots of Polynomials with Solve and NSolve 149

Let’s take another look at the polynomial 13xx2 from the previous section:

In[1]:= NSolve$13xx2 0, x(

Out[1]= x‘ 3.30278,x‘0.302776

NSolve provides approximate numerical solutions to equations. It takes two arguments, separated as always by a comma. The first argument is an equation. Note that the double equal sign m is used for equations; this is because the single equal sign is used to assign values to expressions, an essen-tially different operation. You may also use the m button on the BasicMathInput palette. The second argument in the NSolve command (x in the example above) specifies the variable for which we want to solve. It may be obvious to you that you wish to solve for x, but it’s not to the computer.

For instance, there may be occasions when the equation you are solving involves more than one variable (we’ll see an example later in this section). Lastly, the NSolve command can take an optional third argument which specifies the number of digits of precision that you desire:

In[2]:= NSolve$13xx2 0, x, 15(

Out[2]= x‘ 3.30277563773199,x‘0.30277563773199

Now what about the output? First notice that it is in the form a list (a sequence of items separated by commas with a set of curly brackets around the whole thing). This is because there are typically numerous solutions to a given equation, so it is sensible to present them in a list. Now let’s focus on the items in this list. Each is of the form x‘solution. This looks strange at first, but it is easy enough to interpret. It is an example of a structure called a replacement rule, which will be explored later in this section.

You can smarten the appearance of the list of solutions by making a Grid of the results. As discussed in the last chapter (Section 3.5, see page 87) when Grid is applied to a such a list it will produce a neatly formatted column:

In[3]:= NSolve$13xx2 0, x, 35( ssGrid

Out[3]= x‘ 3.3027756377319946465596106337352480 x‘0.3027756377319946465596106337352480

Can Mathematica produce exact solutions to polynomial equations? The answer is sometimes. It is a mathematical fact that some polynomial equations involving powers of x that exceed 4 cannot be solved algebraically, period. However, if an equation can be solved algebraically, the Solve com-mand is the ticket. Here are the precise roots of the polynomial above:

In[4]:= Solve$13xx2 0, x( ssGrid

Out[4]=

1

2.3 132

1

2.3 132

Remember the quadratic formula? That’s all that’s happening here. In fact, if you ever forget the quadratic formula, you can have Mathematica derive it for you:

In[5]:= Clear#a, b, c, x';

Solve$a x2b xcm0, x( ssGrid

Out[6]=

b b24 a c

2 a

b b24 a c

2 a

Note the space between a and x2, and between b and x in the last input line; they are needed to indicate multiplication. This example also makes it clear why the second argument to the Solve command is so important; this is an equation that could be solved for a, for b, for c, or for x. You have to specify the variable for which you wish to solve.

Let’s look at a few more examples of these commands in action. We’ll start with theNSolvecommand and later address some special considerations for using the Solve command:

In[7]:= Plot$x3x2x3,x,3, 1(

Out[7]=

3 2 1 1 2

2 4

In[8]:= Factor$x3x2x3(

Out[8]= x,13 xx20

In[9]:= NSolve$x3x2x3m0, x(

Out[9]= x‘ 2.61803,x‘ 0.381966,x‘0.

Note that the factor x corresponds to the root x 0, but that the other roots are not revealed by the Factor command (although they would have been found had we replaced the x by 1.*x in the polynomial). The NSolve command reveals all roots, always.

Now let’s tweak things a little. We can shift the graph of this function up by one unit by adding 1 to its expression, and the resulting function should have only one real root (the dip on the right of the graph will be entirely above the x-axis):

4.2 Finding Roots of Polynomials with Solve and NSolve 151

In[10]:= Plot$1x3x2x3,x,3, 1(

Out[10]=

3 2 1 1 2

2 4 6

In[11]:= Factor$1x3x2x3(

Out[11]= 1x3 x2x3

This didn’t do a thing; the new function has no rational roots. What happens if we replace one of the integer coefficients with its decimal equivalent?

In[12]:= Factor$1.x3x2x3(

Out[12]= 1.+2.76929x/ ,0.3611030.230708 xx20

This reveals a real root near x 2.76929. But what about the quadratic factor?

In[13]:= NSolve$1x3x2x3m0, x(

Out[13]= x‘ 2.76929,x‘ 0.1153540.589743Ç,x‘ 0.1153540.589743Ç

Mathematica is reporting three roots. The first root reported is the x-intercept that we see in the plot.

The second two are complex numbers; they are each expressions of the form abÇ, where Ç is the imaginary number whose square is 1. They are purely algebraic solutions to the polynomial equation, bearing no obvious geometric relationship to its graph. Although you may not care to contemplate complex roots of equations, the Solve and NSolve commands will always display them if they exist. It is a fact that every polynomial whose highest power of x is n will have exactly n roots, some of which may be complex numbers (see the fundamental theorem of algebra in Section 4.4 on page 169). It is also true that any complex roots of a polynomial (whose coefficients are all real) come in conjugate pairs; one will be of the form abÇ, the other abÇ, as in the output above.

How can you extract one solution from a list of solutions? For instance, you may only need a real solution, or the context of the problem may dictate that only positive solutions be considered. You can extract a single solution from the list of solutions using double square brackets. This was dis-cussed in Section 3.11 on page 126. Here’s an example to illustrate:

In[14]:= sols Solve$x2225 0, x(

Out[14]= x‘ 15,x‘15

We have given the list of solutions the name sols (note the assignment operator = assigns the name sols to the output, while the equation operator m is used to produce equations). Here’s how to

extract the first element from a list (type È[[È to get 3, or you may also just use two square brackets back to back):

In[15]:= sols317

Out[15]= x‘ 15 and the second element:

In[16]:= sols327

Out[16]= x‘15

This method works for any list:

In[17]:= a, b, c, d, e327

Out[17]= b

You may also use the e3f7 button on the BasicMathInput palette to extract an item from a list.

To use one of the solutions provided by the NSolve or Solve command in a subsequent calculation, you need to understand the syntax of replacement rules. The symbol /. tells Mathematica to make a replacement. It is shorthand for a command called ReplaceAll. You first write an expression involv-ing x, then write /. and then write a replacement rule of the form x‘solution. The arrow ‘ is found on the BasicMathInput palette. You may type -> (the “minus” sign followed by the “greater than”

sign) in place of the arrow if you wish:

In[18]:= x2s. x‘3

Out[18]= 9

This last input line can be read as “Evaluate the expression x2, replacing x by 3.”

Here’s how you can use replacement rules to extract solutions generated by the Solve command:

In[19]:= xs. sols317

Out[19]= 15

In[20]:= xs. sols327

Out[20]= 15

In[21]:= x2s. sols327

Out[21]= 225

If you don’t specify which solution you want, you will get a list where x is replaced by each solution in turn:

4.2 Finding Roots of Polynomials with Solve and NSolve 153

In[22]:= xs. sols

Out[22]= 15, 15

You can do all of this in one step, generating output that is a list of solutions rather than a list of replacement rules:

In[23]:= xs. Solve$x2225m0, x(

Out[23]= 15, 15

You may also use replacement rules to test whether an equation holds for a particular value of x:

In[24]:= x2225m0s. sols317

Out[24]= True

In[25]:= x2225m0s. x‘10

Out[25]= False

Replacement rules take some getting used to, but they are enormously convenient. Here, for instance, we plot a polynomial, and include an Epilog to place a Point at each of the roots (this will work when all roots are real):

In[26]:= f#xB': 124 x15 x25 x33 x4x5

In[27]:= Plot$f#x',x,4, 3, Epilog‘PointSize#.02', Point#x, 0' s. NSolve#f#x'm0, x'(

Out[27]=

4 3 2 1 1 2 3

40 20 20 40

Now let’s look at the Solve command in greater detail. Note that you can find the exact roots (without any decimal approximations) for any polynomial whose degree is four or less:

In[28]:= Grid$Solve$x4x2m0, x(, Alignment‘Left(

Out[28]= approximate decimal solution to an equation you will be better served using NSolve. In particular, if you want a numerical approximation to a solution generated by the Solve command, as you might with the output generated above, it is not a good idea to apply the N command to the result. In some cases, for instance, you may end up with complex numbers approximating real roots (try solving x315x2 0; it has three real roots, yet applying N to the output of the Solve command produces complex numbers—see Exercise 2). The moral of the story: Use Solve to generate exact answers; use NSolve to generate numerical solutions to any required degree of accuracy.

Another consideration to be aware of when using the Solve command is that equations involving polynomials of degree 5 or more (i.e., where the highest power of x is 5 or more) may not have explicit algebraic solutions. This is a mathematical fact; there are equations of degree five with roots that cannot be represented in radicals. Here is what the output will look like in these situations:

In[29]:= Solve$x5x1m0, x(

Out[29]= x‘Root$1 Ó1 Ó15&, 1( ,x‘Root$1 Ó1 Ó15&, 2( ,x‘Root$1 Ó1 Ó15&, 3( , x‘Root$1 Ó1 Ó15&, 4( ,x‘Root$1 Ó1 Ó15&, 5(

The output here is comprised of Root objects, which are a means of cataloging the (in this case five) roots that cannot be expressed in algebraic form using radicals. When Root objects arise, it is always possible to apply N to the output to get numerical approximations. Alternately, NSolve can be used to get the same result:

After all this you may wonder why the Solve command is ever used, since NSolve seems to be more versatile and robust. The answer is that it depends on your purposes. For numerical solutions to specific problems, NSolve is probably all you need. But for exact algebraic solutions or the deriva-tion of general formulae, Solve is indispensable. Here are two more examples to illustrate the power

4.2 Finding Roots of Polynomials with Solve and NSolve 155

of this command. The first provides the general formula for the roots of a cubic equation of the

The next example illustrates how Solve may be used to put a given formula into a different form. It is a rather trite computation, but it illustrates the versatility of the Solve command:

In[33]:= Clear#e, m, c';

Solve$emm c2, m(

Out[34]= m‘ e c2!!

One last comment about the Solve command is in order. As you may expect, it will distinguish between decimals and fractions in the input, and adjust its output to match:

In[35]:= Solve$x20.25m0, x( a. Use Solve to find the real roots of f+x/ and compare your solutions with the values you found

in Exercise 1 of Section 4.1.

b. Use NSolve to approximate the real roots of f+x/ and compare your solutions with the values you found in Exercise 1 of Section 4.1.

2. Use Solve followed by N to approximate the roots of the polynomial x315x2. Then find the roots using NSolve. Which gives the better approximation?

3. Fix two real numbers p and q, and consider the following quadratic equation in the variable z:

z2q z 1

27p3 0.

a. Solve this equation in terms of p and q (use Mathematica, or work by hand using the quadratic formula).

b. Consider the expression z

2q z271p3

z , which has the same roots as the quadratic above. Use replacement rules to replace z by w3, then w by 1

6.3y 3 4p3y2 2, and then p by -ba2

31 and q by -2a39a b27c

27 1, and finally, y by -xa

31. Simplify the result and Collect the terms as ascending powers of x. What do you get?

c. Use the information in parts a and b to develop a means of using the quadratic formula to solve the general cubic equation x3a x2b xc 0. That is, take the solution to the qua-dratic in part a and transform it into a root of the cubic x3a x2b xc using the transforma-tions in part b. The idea to use these successive replacements for the purpose of solving a cubic was perfected in the 1500s by Italian mathematicians such as Cardano and Tartaglia.

d. Compare the output to that of Solve$x3a x2b xc 0, x(317.

Dans le document This page intentionally left blank (Page 163-171)