Tutorial 8:  Solving Equations

by R. Dover, IMSA

Basics

"Solve" is a good place to start.  Check out the following.  Note the double "=" sign.

Solve[(x^2) -  x  -  6  ==  0]

Solve[(x^2) -  x  +  6  ==  0]

Solve[2  Sin[x]^2  -  Sin[x]  -  1 == 0]

That last one was interesting.  Often, to avoid such problems and potential problems, we let Mathematica know what we are trying to solve for.  Try it again, with a new ending.

Solve[2  Sin[x]^2  -  Sin[x]  -  1 == 0,  x]

This still has some problems.  We'll come back to that later.  Indicating the variable at the end is generally a good habit, and it can also allow us to deal with several variables.

Solve[(x^2)  +  x y  +  2  (y^2)  ==  4,  x]

Edit the line above to solve for y instead of x.

Some work...and some don't

Solve[(x^3) -  6  x  +  2  ==  0,  x]

In a new cell, under the "Input" menu, choose "Copy Input from Above."  Edit the line to change the (x^3)to (x^4.) It's interesting.  Edit the problem once again to use (x^5.)  Unfortunately, Mathematica can't do everything.  On the other hand, solve (x^6) =  64.

Generally, "Solve" can handle polynomial equations through the 4th degree - and a few other things here and there.  For algebraic equations, "NSolve" will help a great deal.

NSolve[(x^5) -  6  x  +  2  ==  0,  x]

NSolve[(x^4) -  (Sqrt[2  x]) +  3  x  ==  6,  x]

Try some others.

But this won't take care of everything.

NSolve[x  -  Cos[x]  ==  0,  x]

Checking results

First we'll define a function f.

f[x_] := 
      x^4  + 12  x^3  -  305  x^2  -  1056  x  +  3220

Let's call our solution set "solns."

solns  =  Solve[f[x]  ==  0,  x]

Use TableForm to display the output.  Here, this really isn't necessary because the numbers are quite nice.  Sometimes, however, the answers are much easier to read in this format.

Check these values by replacing them into the function.

f[x]  /.  solns

This shows one of the reasons that Mathematica shows the solution set in this format - to make substitution (into any equation) quite easy.  Then clear both f and solns.

Clear[f,  solns]

Repeat the same steps above using "NSolve" with the function f(x) = (x^4) - 6  (x^3) +  3  x  +  8. When you are done, clear your function and the name of your solution set.  (Alternatively, one can simply refer to the previous output rather than naming the solution set.)

Out of order?

Did you make a mistake somewhere so that "%" or even "%%"  just won't work? See if this scenario sounds familiar:  You made a spelling error in an input line with a "%".  You correct the spelling only to try to execute the corrected command on the faulty output.  When this occurs, replace the "%" with Out[ ],   putting the number of the proper output line in the square brackets.

Another method

The command "FindRoot" uses an iterative process called Newton's Method, and it requires an initial input which is fairly close to the actual root being sought.  Let's deal with the equation  x - Cos[x] == 0.

FindRoot[x  -  Cos[x]  ==  0,  {x,  1}]

As another example, consider the cubic f(x) = (x^3) + 5  (x^2) - 7. First, we'll define the function, calling it f.  (How clever!)

f[x_] := (x^3) + 5  (x^2) - 7

Plot the function.  Choose an appropriate domain so that you can see the three roots.

Since the smallest root is near x = -5, we'll use FindRoot and Newton's Method with this as an initial guess.

FindRoot[f[x]  ==  0,  {x,  -5}]

Based on your plot above, choose nice x-values as initial estimates to find the other two roots.  Note that bad choices for initial inputs (too far away, past a max or min, for example) can lead to some unexpected results.

Clear[f]

Systems

Here, we will solve a system of two equations, y = 2x - 3 and (x^2) - 5  x + 2  xy + (y^2) = 6. First, we will graph the system to check for approximate solutions.  We will use a new method to do this. Before this can be done, use "Help" and load the package "ImplicitPlot."  Execute the cells to name and plot each of the graphs separately.

gr1 = 
      ImplicitPlot[
        (x^2) -  5  x  +  2  x y  +  (y^2) ==  6,  {x,  -4,  
          5}]

gr2  =  Plot[2  x  -  3,  {x,  -3,  4}]

The interesting part is to combine the two graphs.  Use the command "Show."  Note that the graph shown is on the intersection of the two domains given.

Show[gr1,  gr2]

To solve, we need a list of equations and a list of variables for which we wish to solve.

NSolve[{(x^2) - 5  x  +  2  x y  +  (y^2) == 6,  
        y  ==  2  x  - 3},  {x,  y}]

Create some other systems, implicit or not, and solve them.

One more example

In the last section, you looked at the graph and solved the system.  Here, we'll go a little further in putting several pieces together.
	Let's consider two polynomials: f(x) = (x^4) - 6  (x^2) + 2  x + 5 and g(x) = (x^3) - 3  x + 1.  First, define both functions.

Plot both functions together, finding an appropriate domain. Add options to show both graphs in different colors to make it easier to distinguish them.

Now try to estimate the points of intersection.  To do this, click anywhere on the graph to select it.  A box should appear around the graph.  Hold the command key (⌘) down (Macintosh) or Alt key (Windows) and move the cursor around the graph.  The coordinates of the cursor should appear in the lower left corner.  (This does not trace, but it will give decent approximations.) Now try to solve the equation with "Solve."

Whoa!  You may wish to scroll back through this and check it out.  That was a list of four solutions.  Let's work on approximations instead.  Edit the command to use "NSolve."

Substitute this list of values into either f or g to find the corresponding y-values.  Do the ordered pairs seem to agree with your graphical approximations?