Learning Mathematica Basics—Algebraic Operations II

Solve each of the following problems using Mathematica exclusively.

1.  Mathematica's Solve command is where we begin our work today.  Ask Mathematica for a syntax description using the ? command.

2.   As with so many commands in Mathematica, Solve has a variety of different flavors.  The most basic version of the command is the second one shown—Solve[eqns].  Let's use it to solve the equation  5x + 3 = 17.  Issue the following command and hit ENTER:

Solve[5x+3==17]

3.   You should have gotten the answer 14/5.  A couple of points about the above exercise are of note.  First, notice that when declaring an equation in Mathematica a double equals sign is used.  This is to distinguish equations from assignment of value (either the single equals (=) or the colon equals (:=) depending on whether you want delayed or immediate assignment, remember?)

== is a logical operator—it returns a value of True or False.  To see this type in the following equations, and hit ENTER after each:

5==10
4==4
2x==6

4.   Notice that the first two of these gave predictable results, whereas the last one simply returned the equation unevaluated.  The reason for this is fairly obvious—since we haven't given x a value during this session then the third equation remains conditional—i.e. it could be True or False depending on the value of x chosen.

The second point to be noticed about problem 2 is the form in which Mathematica gives us the solution to the equation.  Unlike our "on paper" solutions, where we would write "x=14/5", Mathematica gives us the result in the form of a replacement rule.  Remember learning about these in the previous notebook—we used replacement rules when we wished to do substitutions into algebraic expressions without making any "permanent" changes to the variables.

A little thought allows us to justify why Mathematica chooses to give us the answer in this form:

a.  x=14/5 is an assignment of value.  We don't want to assign x a value, just to find out which value makes the equation true.

b.  If we plan on using the solution we find in later calculations then most likely we'll wish to substitute it into another expression, in which case we'd need the solution in replacement rule form anyway.

Whew!  Let's do something a little more interesting.  Let's see how good Mathematica really is at solving equations.  After all, that first one wasn't much of a challenge.

Ask Mathematica to solve the equation:   7x^4 + 3x^3 + 2x^2 + 6x - 3 = 0.

(As usual, this expression is not in Mathematica form, so it's your job to put it that way.)

5.   Whoah!  That answer was huge.  It should have consisted of four separate solutions, since the polynomial was fourth degree.  If you sift through the above gibberish carefully you'll find that you did indeed get exactly that.  Exactly being the operative word—notice that the above is an exact solution—no decimal approximations are being used.

This brings up an interesting point!  It would seem that the best kind of answer you could have for any problem is an exact one.  However, when an exact answer reaches the scale of the one above, it might actually be a more practical or useful answer if it were in decimal form.  Force the issue!  Apply N to the previous output!

6.   Well now the answers look a little more understandable—two distinct real roots, and a complex conjugate pair.

Let's push the solver a little harder.   Ask Mathematica to solve the equation:

                                   x^5 + 7x^4 + 3x^3 + 2x^2 + 6x - 3 = 0

7.   Looks like it didn't like that one very much!  Mathematica is showing you what function calls it made in an attempt to solve the equation.  The fact that they remain unevaluated tells us that these functions failed to solve the equation.  This is not surprising!  You may have learned that in general,  higher order poynomials are impossible to solve by algebraic methods.  i.e. the variables cannot usually be isolated.

Remember, however, that all Mathematica is saying is that it can't find the answer exactly.  You're probably aware of the fact that there are quite a few techniques available for solving equations approximately—numerical techniques, usually implemented via computers.  Of course, Mathematica has the ability to access some of these algorithms.  The command we need for this problem  is NSolve.  Reissue your previous command (problem 6) but replace the Solve with an NSolve.

8.   That was pretty quick!  Three real solutions, and a complex conjugate pair.  Keep the NSolve command in mind—it it frequently more useful than the Solve command, and even though the answers it gives are approximations, they are extremely accurate approximations.

An important note:  don't confuse the NSolve[...] command with the combination N[Solve[...]].  The second of these would still calculate the exact answer (if it could), then take the decimal value of that.

Moving on, we will now see that there are even limits to the abilities of the NSolve command.  For instance, try using NSolve on this equation:

                                             arctan x + e^x - cos x + 0.3 = 0.

9.   So even NSolve balks at such a nasty equation...there is another option, however.  Ask Mathematica for the syntax of the command FindRoot

10.   So apparently there's a third method for solving equations, but this one has a couple of twists.  First, notice that the description talks about "solution", singular not plural.  Apparently FindRoot is only capable of finding one solution at a time, not the whole set of solutions like the previous two commands.

Also, notice the last part of the description—"starting with x == x0."  This means that in order to use this particular method it is necessary to have a "starting value" or initial guess for the solution you are seeking to the equation.  The closer your initial guess is to the actual solution you seek, the better are the chances of the numerical method FindRoot uses  "hitting" the solution you are after.

Let's get down to the business of solving the equation from problem 8.  Since we'll be using FindRoot, we'll need an initial guess.  Unfortunately, the equation is so messy it's hard to imagine anything even close to a solution.  Where should we begin?  At times like this a graph is worth a thousand guesses.  We've never actually used Mathematica's graphing features yet, so I'll give you the command you'll need:

Plot[ArcTan[x]+E^x-Cos[x]+0.3,{x,-11,2}]

In a later notebook we'll discuss the details of this, and other graphing commands.  For now, just enter the command, and evaluate it.

11.   Well it looks like there are actually three solutions.  First we'll try to get the one closest to the origin by using an initial guess of x=0.  The command would be:

FindRoot[ArcTan[x]+E^x-Cos[x]+0.3==0,{x,0}]

Enter and evaluate it.

12.   You should have gotten an answer of {x -> -0.163763}.  Now find the other two solutions by using initial guesses of -2 and -4.  (You'll need two separate commands, of course.)

13.   Well that about covers Mathematica's abilities with basic equation solving, i.e. solving equations containing a single variable.  We haven't yet exhausted the abilities of the Solve command, however.

Let's have Mathematica solve the following equation:

                                                F=(G m1 m2) / r^2
                                                
for G.  The command, (looking back at the syntax in problem 1), would be:

Solve[f==(g*m1*m2)/r^2,g]

Notice that I'm obeying the convention of using lower-case letters for user-defined variables.  Enter the command and evaluate it.

14.   Now you solve the same equation, but this time for r.

15.   Let's move up a notch.  What about solving systems of equations?  Let's solve the system:

                                                   5x + 3y = 7,   9x - 11y = 4.
                                                   
The command would be:

Solve[{5x+3y==7, 9x-11y==4}]

Enter and evaluate it.

16.   How about a non-linear system of equations?  Solve the system:

                                                   5x^2 + 3y = 6,   9x^2 - 11y^2 = 3.

17.   Oooh!  Four very ugly ordered pairs.  Notice that Solve continues to give exact answers, even when working with systems of equations.  Now try the system:

                                                      5x^4 + 3y = 6,   9x^3 - 11y^5 = 3.

18.   After some hesitation, Mathematica bails out like in problem 6.  Let's try to fix the problem in the same way.  i.e. try using NSolve now instead.

19.   No wonder it took so long!  It got twenty answers, eighteen of them complex!

Let's now try an even larger system.  Have Mathematica solve the 4x4 system:

            3a + 2b + 7c - e = 5,  5a - 2b + 8c +6e = 3,  9a - 2b = 6,  5b + 12c + c + e = 11

Final Note:  Mathematica's equation solving skills are imspressive.  They aren't perfect, however.  It is still possible to present Mathematica with equations that it can't handle, regardless of which method of solution you choose.  These equations often fit into a class of equations referred to as "poorly conditioned."

Remember learning that some equations have no solution(s)?  Well poorly conditioned equations almost have no solution(s).  They are notoriously hard to solve.  I have it on good authority that Wolfram Research is currently working on some new algorithms to tackle certain classes of poorly conditioned equations more effectively.  Hopefully they'll be ready in time to be included in the next release of the software.

If you'd like a real challenge then use the techniques we've covered in this notebook to solve the following (relatively) poorly conditioned 2x2 system of equations:

               5.31 = (a*E^(10*a))/(0.2544529262086513995*a - b + b*E^(10*a))
             226.5 = (a*E^(190*a))/(0.2544529262086513995*a - b + b*E^(190*a))
             
(where a and b are the variables, and E represents the number e=2.71828182845904523536...)
             
Incidentally, these equations were not just the result of some anti-Intel mathematician's attempts to find a flaw in the Pentium chip, but a very real problem dealing with United States population dynamics.  They came from an analysis of actual US population data that we carried out one semester in my Differential Equations class.