Solving Equations and Finding Roots

Index

This index gives a list of keywords that are associated with various cells in this Notebook. If you Command-Double-click one of the keywords below,  Mathematica  will find the cell associated with that keyword, select it, and display it in the center of  your window.

Assignment  
Eliminate  
Equation  
Equation`symbolic  
FindRoot        
Inequation  
Logical`And  
Logical`Or  
LogicalExpand  
NSolve        
Reduce      
Roots  
Solve      
ToRules

Basic Equation Solving

Syntax (see pages 95 and 606)

An Assignment

x =y  assigns x  to have value y .

A Symbolic Equation

lhs ==rhs  represents a symbolic equation.

Solve

Solve[lhs ==rhs , x ] solves an equation, giving a list of rules for x .

x  /. solution    uses the list of rules to get values for x .

expr  /. solution   uses the list of rules to get values for an expression.

Solve[{lhs1  ==rhs1 , lhs2   ==rhs2  , ... }, {x  , y , ...} ] solves a set of simultaneous equations.

NSolve

NSolve[lhs == rhs  , x  ]  gives an approximate numerical solution to a polynomial equation.

FindRoot

FindRoot[lhs == rhs  , {x  , x0 }]  searches for a numerical solution to the equation starting with x  = x0 .

Reduce

Reduce[{lhs1  == rhs1, lhs2   == rhs2 , ... }, {x, y, ...]    gives a set of simplified equations, including all possible solutions.

A Quadratic Equation

Solve[x^2 + 2x - 7 == 0, x]

Getting an Approximate Numerical Solution

Solve[x^2 + 2x - 7 == 0, x]

N[%]

Getting Values for x

Solve[x^2 - 2x - 3 == 0, x]

x /. %

Naming and Using the Solutions

soln = Solve[x^2 - 2x - 3 == 0, x]

x^ 2 + 3 x /. soln

A Simultaneous Symbolic Equation

Solve[ {a x + b y == 0, x + y == c}, {x,y}]

A Fifth Order Equation

Note that N[%] can be used to get a numerical approximation or  NSolve may be used directly.

Solve[  x^5 + 2x + 1 == 0, x]

N[%]

NSolve[ x^5 + 2x + 1 == 0, x]

A Note on Obtaining Numerical Solutions

There are many equations for which it is mathematically impossible to find explicit formulas for the solutions. Mathematica  uses the function Roots to represent the solutions in this case. Even though you cannot get explicit formulas, you can still find the solutions numerically by applying N. If you ultimately need only the numerical solution, it is usually a lot faster to use NSolve to ask for numerical results from the outset. If there are approximate  numbers in your original equation, Solve will immediately get a numerical solution.

Numerical Solution of Simultaneous Trigonometric Equations

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

Finding All Possible Solutions

Reduce[a x + b == 0, x]

A Note on Reduce and Solve

The most important difference between Reduce and Solve is that Reduce gives all  the possible solutions to a set of equations, while Solve gives only generic  ones. Solutions are considered "generic" if they involve conditions only on the variables that you explicitly solve for, and not on other parameters in the equations.  Reduce and Solve also differ in that Reduce always returns combinations of equations using logical connectives like || and  && , while Solve gives results in the form of transformation rules.

Equations in One Variable

Syntax (see page 607)

Solve

Solve[lhs  == rhs , x ]   solves an equation, giving a list of rules for x .

x   /.  solution     uses the list of rules to get values for x .

expr  /. solution     uses the list of rules to get values for an expression.

NSolve

NSolve[lhs  == rhs , x ]   gives an approximate numerical solution to a polynomial equation.

FindRoot

FindRoot[lhs   == rhs , {x ,  x0  }]   searches for a numerical solution to the equation starting with x  = x0 .

A Quadratic Equation

Solve[x^2 + 2x - 7 == 0, x]

Getting a Numerical Solution

When there are approximate numbers in the equations,  Solve immediately gets a numerical solution.

Solve[x^2 + 2x - 7 == 0, x]

N[%]

Solve[x^2 + 2.0 x - 7 == 0, x]

Getting Values for x

Solve[x^2 - 2x - 3 == 0 , x]

x /. %

Naming and Using the Solutions

soln = Solve[x^2 - 2x - 3 == 0, x]

x^ 2 + 3 x /. soln

Naming an Equation

p = x^2 - 3 x + 9

Solve[ p == 0, x]

NSolve[ p == 0, x]

Displaying a Single Solution

The semicolon at the end of the first input suppresses the printing of its output.

solution = Solve[ x^3 + 34 x + 1 == 0, x] ;

solution[[2]]

A Fifth Order Equation

Note that N[%] can be used to get a numerical approximation or  NSolve may be used directly.

Solve[  x^5 + 2x + 1 == 0, x]

N[%]

NSolve[ x^5 + 2x + 1 == 0, x]

A Note on Higher Order Polynomial Equations

Mathematica  can always find exact solutions to polynomial equations of degree four or less. For cubic and quadratic equations, however, the results can be extremely complicated. If the parameters in equations like these are symbolic, there can also be some subtlety in what the solutions mean (see page 608). If the highest power in the equation is 5 or more, it may be mathematically impossible to give explicit algebraic formulas for the solutions. Mathematica  uses the function  Roots to represent the solutions in this case. Even though you cannot get explicit formulas, you can still find the solutions numerically by applying N. If you ultimately need only the numerical solution, it is usually a lot faster to use NSolve to ask for numerical results from the outset. If there are approximate numbers in your original equation,  Solve  will immediately get a numerical solution.

A Trigonometric Equation

Solve[ Sqrt[Cos[x]] == a, x]

x /.%/. a -> .5

Solving Nonpolynomial Equations

Mathematica   can always give you numerical approximations to the solutions of a polynomial equation. Finding solutions to more general equations, involving, say, transcendental functions, can be much more difficult. FindRoot gives you a way to search for numerical solution to an arbitrary equation or set of equations. Note that you must give  FindRoot  a starting point and that FindRoot always returns the first solution it finds. For more information, see the section Numerical Equation Solving below.

Numerical Root Finding

FindRoot[ x Sin[x] - 1/2 == 0, {x, 1}]

Simultaneous Equations

Syntax (see page 610)

Solve

Solve[{lhs1  == rhs1 , lhs2  == rhs2 ,...},{x, y, ...}] solves a set of simultaneous equations.

Solve[{lhs1  == rhs1 ,  lhs2  ==  rhs2 ,...},vars , elims  ]  finds solutions for vars , eliminating the variables elims .

NSolve

NSolve[{lhs1  ==  rhs1 ,  lhs2  ==  rhs2 , ...}, {x , y , ...}]  gives an approximate numerical solution to simultaneous polynomial equations.

FindRoot

FindRoot[{eqn1 , eqn2 , ...},{x ,  x0 }, {y,  y0 },...]   searches for a numerical solution to the simultaneous equations starting with x  = x0   and  y  = y0 .

Reduce

Reduce[{lhs1  ==  rhs1 ,  lhs2   ==  rhs2 , ...}, {x , y , ...}] gives a set of simplified equations, including all possible solutions

Eliminate

Eliminate[{lhs1  == rhs1 , lhs2   == rhs2, ...}, {x,  y, ...}]  eliminates x ,  y , ...  in a set of simultaneous equations.

Simultaneous Polynomial Equations

Solve[ {x ^ 2 + y ^ 2 == 1, x + y == a},
                        {x,y}]

Using the List of Solutions

The semicolon after the first input  suppresses the printing of its output.

Solve[ {x ^ 2 + y ^ 2 == 1, x + y == a},
 {x,y}] ;

x^3 + y^3 /. % /. a -> 0.7

Getting a Numerical Approximation

Solve[{ x^3 + y^3 == x y, x + y + x y == 1},
                             {x, y}]

N[%]

NSolve[{ x^3 + y^3 == x y, x + y + x y == 1},
                             {x, y}]

A Note on Obtaining Numerical Solutions

There are many equations for which it is mathematically impossible to find explicit formulas for the solutions. Mathematica  uses the function Roots  to represent the solutions in this case. Even though you cannot get explicit formulas, you can still find the solutions numerically by applying N. If you ultimately need only the numerical solution, it is usually a lot faster to use NSolve to ask for numerical results from the outset. If there are approximate numbers in your original equation, Solve will immediately get a numerical solution.

Using a Table of Equations

eqn = Table[ 2 a[i] + a[i-1] == a[i+1], {i,3}]

Solve[ eqn, {a[1], a[2], a[3]}]

Equations between Lists

Solve[{3 x + y, 2 x - 5y} == {7, 8}, {x, y}]

Finding Roots of Simultaneous Trigonometric Equations

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

Eliminating Variables

Eliminate[{a x + y == 0, 2 x + (1 - a) y == 1}, y]

Solving for One Variable in Terms of Another

Solve gives solutions as replacement rules.

eqn = {x == 1 + 2 a, y == 9 + 2x}

Solve[ eqn, {x, y}]

Solve[ eqn, x , y]

Eliminating Variables

Eliminate constructs equations in which variables have been eliminated.

eqn = {x == 1 + 2 a, y == 9 + 2x}

Eliminate[ eqn, a]

Writing a Polynomial in Terms of Specified Forms

This gives  x  ^ 5 + y  ^ 5 in terms of the "symmetric
polynomials"  x  + y   and  x y .

Eliminate[ {f == x^5 + y^5, a == x + y,
                 b == x y}, {x, y}]

Using a Subsidiary Condition

The equation named sincos in the first line is used as a
"subsidiary condition"  in the solution of the second two examples.

sincos = Sin[x]^2 + Cos[x]^2 == 1

Solve[ { Sin[x] == Cos[x], sincos },
                   {Sin[x], Cos[x]}]

Solve[ { Sin[x] + 2 Cos[x] == 1, sincos},
                    {Sin[x], Cos[x]}]

Parameter Values That Always Give Solutions

SolveAlways[ a x + b == 0, x]

Finding All Possible Solutions

eqn = {x y == a, x^2 y^2 == b}

Solve[ eqn, {x, y}]

Reduce[ eqn, {x, y}]

Equations as Logical Statements

Syntax (see page 618)

An Equation

lhs   == rhs    gives an equation asserting that two quantities are equal.

An Inequation

lhs   != rhs    gives an inequation asserting that two quantities are not equal.

Logical And

eqn1   && eqn2     gives equations that must simultaneously be satisfied.

Logical Or

eqn1 || eqn2     gives alternative equations,  only one of which need be satisfied.

Reduce

Reduce[eqns ,vars ]  simplifies the equations eqns , attempting to solve for the variables vars .

Roots

Roots[lhs  == rhs ,vars ]  produces a logical statement representing the roots of a polynomial equation.

LogicalExpand

LogicalExpand[expr ]   expands out expressions using logical connectives such as &&  and ||.

ToRules

ToRules[eqns ]  takes logical combinations of equations and converts them to a list of rules.

Solving Equations Given as Logical Statements

Solve[ {x + y == 1 && x - y == 2}, {x, y}]

Solve[ {x + y == 1 || x - y == 2}, {x, y}]

Reduce[ {x + y == 1 || x - y == 2}, {x, y}]

Using Solutions Given as Logical Statements

The final output gives solutions to  x  ^ 5  ==  x                                                       that are not  solutions to x  ^ 2  == x .

logicsoln = Reduce[ x^2 == x, x]

Reduce[ x^5 == x && !logicsoln, x]

Finding All Solutions

Solve[{x ==1, x==a}, x]

Reduce[{x ==1, x==a}, x]

A Note on Reduce and Solve

The most important difference between Reduce and Solve is that Reduce gives all  the possible solutions to a set of equations, while Solve gives only generic  ones. Solutions are considered "generic" if they involve conditions only on the variables that you explicitly solve for, and not on other parameters in the equations. Reduce and Solve also differ in that Reduce always returns combinations of equations using logical connectives like || and &&, while Solve gives results in the form of transformation rules.

Roots as Logical Statements

Roots[ x^2 + 3x == 2, x]

Converting Logical Statements into Transformation Rules

{ToRules[ x==1 || x==2]}

x^2 + 2 /. %

Using Roots in an Algebraic Replacement

Roots[ x^2 + 3x == 2, x]

{ToRules[%]}

x^2 + b x /. %

Converting an Equation of Lists to a Logical Statement

LogicalExpand[{3 x + y, 2 x - 5y} == {7, 8}]

Numerical Equation Solving

Syntax (see page 107)

NSolve

NSolve[eqns , vars ]  gives a list of numerical approximations to the roots of a polynomial equation.

NSolve[eqns ,vars , n ]  gives results to n- digit  precision.

FindRoot

FindRoot[lhs   == rhs , {x , x0 }]  searches for a numerical solution to the equation starting with x  = x0 .

FindRoot[lhs   == rhs , {x , {x0 , x1 }}]   searches for a solution to the equation using x0  and x1  as the first two values of x   (this form must be used if the symbolic derivatives of the equation cannot be found).

FindRoot[lhs   == rhs , {x , xstart , xmin , xmax  }]   searches for a solution, stopping the search if   x   ever gets outside the range  xmin   to xmax  .

FindRoot[{eqn1 ,eqn2 , ...}, {x,  x0 }, {y , y0 }, ...]    searches for a numerical solution to the simultaneous equations.

Option for FindRoot:

AccuracyGoal -> n    specifies the accuracy in the value of the function at the root. The default value is Automatic.

Numerical Solution

NSolve[ x^5 + 7x + 1 == 0, x]

Arbitrary-Precision Numerical Solution

NSolve[ x^5 + 7x + 1 == 0, x, 25]

Arbitrary-Precision Numerical Solution Using N

Note that this is typically much slower than
using NSolve directly.

Solve[ x^5 + 7x + 1 == 0, x]

N[%%, 25]

Simultaneous Polynomial Equations

NSolve[{ x^3 + y^3 == x y, x + y + x y == 1},
                             {x, y}]

A Transcendental Equation

FindRoot[ 3 Cos[x] == Log[x], {x, 1}]

FindRoot[ 3 Cos[x] == Log[x], {x, 10}]

A Note on Using FindRoot

If your equations involve only linear functions or polynomials, then you can use NSolve to get numerical approximations to all the solutions. However, when your equations involve more complicated functions, there is in general no systematic procedure for finding all solutions, even numerically. In such cases, you can use FindRoot to search for solutions.

In trying to find a solution to your equation, FindRoot starts at the point you specify, and then progressively tries to get closer and closer to a solution. Even if your equation has several solutions, FindRoot always returns the first solution it finds. Which solution this is will depend on what starting point you chose. So long as you start sufficiently close to a particular solution, FindRoot will always return that solution. In choosing starting points, the best policy is to avoid points that have special properties with respect to the equations you are trying to solve.

Finding Complex Roots

If you want to search for complex roots, then you must give a complex starting value.

FindRoot[Sin[x] == 2, {x, I}]

Searching for Roots in a Specified Interval

FindRoot[ Sin[x] == 0, {x, 1, 0.5, 1.5}]

Simultaneous Trigonometric Equations

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

Equations without Symbolic Derivatives

If Mathematica  cannot get an explicit formula for the derivative of a function that appears in your equation, you have to specify the first two values to try.

FindRoot[Zeta[ 1/2 + I t] == 0, { t, {12, 13}}]

Specifying the Accuracy Goal

This specifies that the zeta function needs to be zero to 10-digit accuracy at the solution.

FindRoot[Zeta[ 1/2 + I t] == 0, { t, {12, 13}},
                           AccuracyGoal -> 10]

Chop[%]