SCCC Mathematica Tutorial, © 2007-2020, Seattle Central Community College Math Dept., contact: Greg.Langkamp@seattlecolleges.eduVersion 1.9/ March 2020
Important!
Click "Make your own Copy" in the black ribbon above to make this document interactive. You will also need to be logged into Wolfram Cloud
Click "Make your own Copy" in the black ribbon above to make this document interactive. You will also need to be logged into Wolfram Cloud
Lesson 6 Solving Equations
Lesson 6 Solving Equations
Mathematica has a number of commands which it uses to solve equations. We will consider three commands that will help you solve almost any equation you are likely to encounter. When it is mathematically possible to solve an equation exactly, we use Mathematica's Solve command. In the great many cases where no algorithm exists to find an exact answer, Mathematica's NSolve and FindRoot commands will allow us to find numerical approximations of the solution(s) to any degree of precision. We will consider each of these commands in detail in the course of this lesson. Here is a brief overview of when these three commands are used.
1) The Solve command is used to find exact solutions for general equations when possible.
2) The NSolve command finds approximate solutions to polynomial equations. It produces all of the solutions in one step.
3) The FindRoot command is used to find approximate solutions for more general types of equations (non-polynomial). This command finds one solution at a time.
2) The NSolve command finds approximate solutions to polynomial equations. It produces all of the solutions in one step.
3) The FindRoot command is used to find approximate solutions for more general types of equations (non-polynomial). This command finds one solution at a time.
The table below summarizes the scope of each command.
Command | Equationtypes | Answertype |
Solve | general,incl.polynomialsuptofourthdegree | exact |
NSolve | allpolynomials | approximate |
FindRoot | general | approximate |
6.1 Entering an equation. -- The double equal sign rule
6.1 Entering an equation. -- The double equal sign rule
Clear[x]
There is one very important rule that you need to remember when working with equations in Mathematica. Here it is: Mathematica requires a double equal sign " == " when you enter an equation.Example: To enter the equation -3x=17, you must enter it as -3x==17.
2
x
2
x
Caution : The double equal sign rule takes some getting used to, since its obviously different from our usual way of writing equations. If you forget, and use just one equal sign, Mathematica will issue an error message. Execute the next line to see what Mathematica does when you forget to use the double equal sign.
x^2-3x=17
◼
Enter the equation -5x+6=0.
2
x
x^2-5x+6==0
Note that the equation is displayed using StandardForm. As before, we can use the TraditionalForm command to display it using TraditionalForm.
x^2-5x+6==0//TraditionalForm
Why does Mathematica have the double equal sign rule?
The reason for the rule stems from the need for absolute precision when communicating math between human and computer. If you write on a piece of paper you probably mean "assign the value 5 to the variable " . However, in another context, you might write to represent the equation of a vertical line. In the first case you are using the equal sign to make an assignment. In the latter, you are using it as part of an equation. This is not a problem for us, because we understand the meaning of things in context. But when you interact with a computer, it is not able to appreciate context, hence we have to be very precise when we give it a mathematical object to work on. The easiest way to distinguish between "assignments" and "equations" is to use a different notation: one equal sign for an assignment and two equal signs for an equation.
x=5
x
x=5
6.2 Using the Solve command to find exact solutions.
6.2 Using the Solve command to find exact solutions.
The Solve command is an all purpose command for finding solutions to a wide range of equations.The basic syntax is: Solve[equation, variable], for example Solve[ -5x+60,x.
2
x
We first consider polynomial equations. Algorithms exist for calculating the exact solutions for polynomial equations up to . Mathematica's Solve command implements these algorithms.
degree4
◼
Solve the equation -5x+6=0. (Don't forget the double equal sign).
2
x
Solve[x^2-5x+6==0,x]
Note the form in which Mathematica reports the solutions. A list of two assignment statements.
◼
Solve the equation -5x-7=0. (Don't forget the double equal sign).
2
x
Solve[x^2-5x-7==0,x]
◼
Solve the equation +5++125x=600. (Don't forget the double equal sign).
4
x
3
x
2
x
Solve[x^4+5x^3+x^2+125x==600,x]
Here Mathematica has found all of the exact solutions of this equation: the two real solutions -8 and 3, as well as the two "imaginary" solutions -5 and 5 . By the way, the shortcut for entering the imaginary number is ii.
=
-1
The Solve command can also be used to find exact solutions for some non-polynomial equations. Two simple examples are given below.
◼
Solve the equation . Recall that this equation has an infinite number of solutions. From trigonometry we know the complete list of solutions is: , where n is an integer. Let's see what Mathematica gives you as an answer.
Sin[x]=
1
2
x=+2πn,x=+2πn
π
6
5π
6
Solve[Sin[x]==1/2,x]
This Mathematica result looks a little scary, but is really not too bad. The first part tells us that some solutions are of the form +2πC[1], where C[1] is an integer. This is another way of writing +2πn, with n an integer. Mathematica calls this a “condition expression” because we don’t have a specific value for the constant C[1]. The second part of the solution is also what we expected -- that the other solutions to the trig equation take the form +2πn.
π
6
π
6
5π
6
◼
Solve the equation . (Don't forget the double equal sign).
5=43
x
4
Solve[5^(x/4)==43,x]
Once again Mathematica is giving us a conditional expression, with C[1]representing all possible integers. It may first appear that Mathematica gives us only imaginary solutions involving the number . But if we let C[1]=0, we get the real solution The reason why Mathematica includes imaginary solutions is a bit beyond the scope of this course. If we use the Solve command with the Reals condition we get only the real solution we are looking for (although written in a different format!)
x=4Log.
43
5
Solve[5^(x/4)==43,x,Reals]
The Solve command can be useful in some cases, particularly when working with polynomial equations. But if an equation is at all complicated, for example combining exponential, polynomial and trigonometric expressions, then an exact solution will typically not be available. In those cases the NSolve command, discussed in the next section, will be the appropriate choice.
Exercise 6.2 A
Solve the equation .
sin(2x)+sin(x)=0
Answer to Exercise 6.2A
Solve[Sin[2x]+Sin[x]==0,x]
The first and last sets of solutions can be combined into . Therefore all solutions to this equation include the following: , and , where n is any integer.
x=nπ
x=nπ
x=+2nπ
-2π
3
x=+2nπ
2π
3
Exercise 6.2 B
Solve the equation -5+6=0. You may want to do this by hand first and compare your answer to Mathematica's. [Hint: let and rewrite equation in terms of . Solve for , then for .]
2x
x
u=
x
u
u
x
Answer to Exercise 6.2B
Solve[^(2x)-5^x+6==0,x,Reals]
So the two solutions are and .
x=ln(2)
x=ln(3)
6.3 Using the NSolve command to approximate the roots of polynomial equations
6.3 Using the NSolve command to approximate the roots of polynomial equations
The NSolve command gives a complete set of numerical solutions to any polynomial equation. The basic syntax is: NSolve[equation, variable], for example NSolve[ -5x+60,x.
2
x
◼
Find a numerical approximation for the roots of the equation -5x-7=0. (Don't forget the double equal sign).
2
x
NSolve[x^2-5x-7==0,x]
You can specify any degree of precision. If you enter NSolve[equation, variable, n], then NSolve will calculate the roots with n-digit precision.
◼
Find a numerical approximation for the roots of the equation -5x-7=0. Use 25 digit precision.
2
x
NSolve[x^2-5x-7==0,x,25]
◼
Find a numerical approximation for the roots of the equation -5x-7=0.
8
x
NSolve[x^8-5x-7==0,x]
One final note on this example. Since this is an eighth degree polynomial equation, there is no mathematical formula for calculating its exact roots. So the Solve command will not be able to handle this equation. Execute the next line to see what happens when we give the Solve command a problem it cannot solve.
Not a very helpful answer! When you get an answer like this from the Solve command it should be a signal to you that it is time to turn to the NSolve command.
Exercise 6.3 A
Answer to Exercise 6.3A
The largest, real-valued solution is approximately 0.136333.
The FindRoot command is our all-purpose numerical solver for any equation. It finds one solution at a time. You give it a starting value (or seed value) in the general vicinity of where you expect a solution and it then works to find that particular solution.
The basic syntax is: FindRoot[equation, {variable, starting value}]
The basic syntax is: FindRoot[equation, {variable, starting value}]
In order to apply the FindRoot command we will need to first come up with a starting value for each solution.
Our first step will be to plot a graph of the left-hand side of the equation.
Our first step will be to plot a graph of the left-hand side of the equation.
The graph shows four x-intercepts. We estimate these x-intercepts from the graph as: -.75, 0, 1.5 and 4.5.
Next we use the FindRoot command with these four starting values.
Next we use the FindRoot command with these four starting values.
We now consider a second example.
If we now graph the left hand side of this equation we once again find solutions at each of the x-intercepts.
From the graph it appears that there is a solution close to 1. So we will use that as our starting value.
Are we done? Have we found all of the solutions? If you think about the global behavior for this function you will realize that as x gets large y should approach infinity. That suggests that the graph will turn back up at some point and cross the x axis again. Let's expand the plot domain.
Not far enough. Lets go further to the right.
There's the turn back up. Let's go further.
Looks like there is indeed another x-intercept at around 200. We now use 200 as our second starting value.
Now we are done.
Exercise 6.4 A
Answer to Exercise 6.4A
We have three solutions, each quite complicated looking and note that each involves the imaginary number .
Note: These solutions may be hard to see in Mathematica Online!
You might be tempted to conclude that all three solutions are therefore not real numbers. But if you think about this for a moment you will realize that this cannot be true, since we know that every cubic equation must have at least one real solution. In fact this cubic equation has three real solutions and no imaginary solutions at all! Just look at the plot below to verify this.
Note: These solutions may be hard to see in Mathematica Online!
You might be tempted to conclude that all three solutions are therefore not real numbers. But if you think about this for a moment you will realize that this cannot be true, since we know that every cubic equation must have at least one real solution. In fact this cubic equation has three real solutions and no imaginary solutions at all! Just look at the plot below to verify this.
The problem here is that the three solutions produced by the Solve command are in unsimplified form. When properly simplified, all of the imaginary units cancel and we discover that the three solutions are in fact three real numbers.
We can estimate these real solutions most readily by using the NSolve command.
We can estimate these real solutions most readily by using the NSolve command.
Compare these values to the x-intercepts in the previous graph. As you can see from this example exact isn't always better. You will have to use your judgment and experiment a bit to find what is most useful.
The other purpose of this example was to illustrate the need to be cautious in jumping to too hasty conclusions based on appearances. Our first look at the exact solutions gave us the wrong impression of the basic nature of the solutions to this equation. Keep this example in mind as you consider the answers Mathematica produces.
The other purpose of this example was to illustrate the need to be cautious in jumping to too hasty conclusions based on appearances. Our first look at the exact solutions gave us the wrong impression of the basic nature of the solutions to this equation. Keep this example in mind as you consider the answers Mathematica produces.
The Solve command can be used to solve a system of linear equations. Here are two examples.
And here is a plot of the two lines along with the solution point. As expected, this point is the intersection of the two graphs.
Exercise 6.6 A
Answer to Exercise 6.6A
Exercise 6.6 B
Answer to Exercise 6.6B
A solution doesn't exist when the lines do not intersect (i.e., they are parallel). Here is the graph.