Mathematica Reference Guide
1.5 Symbolic Mathematics
1.5.7 Solving Equations
An expression like x^2 + 2 x - 7 == 0 represents an equation in Mathematica. You will often need to solve equations like this, to find out for what values of x they are true.
This gives the two solutions to the quadratic equation x2+ 2 x - 7 = 0. The solutions are given as replacements for x.
In[1]:= Solve[x^2 + 2x - 7 == 0, x]
Out[1]= 99x → −1 − 2 è!!!2=, 9x → −1 + 2 è!!!2==
Here are the numerical values of the solutions.
In[2]:= N[ % ]
Out[2]= 88x → −3.82843<, 8x → 1.82843<<
You can get a list of the actual solutions for x by applying the rules generated by Solve to x using the replacement operator.
In[3]:= x /. %
Out[3]= 8−3.82843, 1.82843<
You can equally well apply the rules to any other expression involving x.
In[4]:= x^2 + 3 x /. %%
Out[4]= 83.17157, 8.82843<
Solve@ lhs == rhs, x D solve an equation, giving a list of rules for x x ê. solution use the list of rules to get values for x
expr ê. solution use the list of rules to get values for an expression
Finding and using solutions to equations.
Solve always tries to give you explicit formulas for the solutions to equations. However, it is a basic mathematical result that, for sufficiently complicated equations, explicit algebraic formulas cannot be given. If you have an algebraic
Mathematica can always solve algebraic equations in one variable when the highest power is less than five.
It can solve some equations that involve higher powers.
In[6]:= Solve[x^6 == 1, x]
Out[6]= 88x → −1<, 8x → 1<, 8x → −H−1L1ê3<, 8x → H−1L1ê3<, 8x → −H−1L2ê3<, 8x → H−1L2ê3<<
There are some equations, however, for which it is mathematically impossible to find explicit formulas for the solutions. Mathemat-ica uses Root objects to represent the solutions in this case.
In[7]:= Solve[2 - 4 x + x^5 == 0, x]
Out[7]= 88x → Root@2 − 4 #1 + #15&, 1D<,
8x → Root@2 − 4 #1 + #15&, 2D<, 8x → Root@2 − 4 #1 + #15&, 3D<, 8x → Root@2 − 4 #1 + #15&, 4D<, 8x → Root@2 − 4 #1 + #15&, 5D<<
Even though you cannot get explicit formulas, you can still find the solutions numerically.
In[8]:= N[ % ]
Out[8]= 88x → −1.51851<, 8x → 0.508499<, 8x → 1.2436<,
8x → −0.116792 − 1.43845 <, 8x → −0.116792 + 1.43845 <<
In addition to being able to solve purely algebraic equations, Mathematica can also solve some equations involving other functions.
After printing a warning, Mathematica returns one solution to this equation.
In[9]:= Solve[ Sin[x] == a, x ]
Solve::ifun : Inverse functions are being used by Solve, so some solutions may not be found; use Reduce for complete solution information.
Out[9]= 88x → ArcSin@aD<<
It is important to realize that an equation such as sin HxL = a actually has an infinite number of possible solutions, in this case differing by multiples of 2 p. However, Solve by default returns just one solution, but prints a message telling you that other solutions may exist. You can use Reduce to get more information.
There is no explicit “closed form” solution for a transcendental equation like this.
In[10]:= Solve[ Cos[x] == x, x ]
Solve::tdep : The equations appear to involve the
variables to be solved for in an essentially non−algebraic way.
Out[10]= Solve@Cos@xD x, xD
You can find an approximate numerical solution using FindRoot, and giving a starting value for x.
In[11]:= FindRoot[ Cos[x] == x, {x, 0} ] Out[11]= 8x → 0.739085<
Solve can also handle equations involving symbolic functions. In such cases, it again prints a warning, then gives results in terms of formal inverse functions.
Mathematica returns a result in terms of the formal inverse function of f.
In[12]:= Solve[ f[x^2] == a, x ] InverseFunction::ifun :
Inverse functions are being used. Values may be lost for multivalued inverses.
Out[12]= 99x → −è!!!!!!!!!!!!!!!!!!!fH−1L@aD =, 9x → è!!!!!!!!!!!!!!!!!!!fH−1L@aD ==
Solve@ 8 lhs1 == rhs1, lhs2
== rhs2, … <, 8 x, y, … < D
solve a set of simultaneous equations for x , y , …
Solving sets of simultaneous equations.
You can also use Mathematica to solve sets of simultaneous equations. You simply give the list of equations, and specify the list of variables to solve for.
Here is a list of two simultaneous equations, to be solved for the variables x and y. In[13]:= Solve[{a x + y == 0, 2 x + (1-a) y == 1}, {x, y}]
Out[13]= 99x → − 1
−2 + a − a2, y → − a 2 − a + a2==
Here are some more complicated simultaneous equations. The two solutions are given as two lists of replacements for x and y.
In[14]:= Solve[{x^2 + y^2 == 1, x + 3 y == 0}, {x, y}]
Out[14]= 99x → − 3è!!!!!!10 , y → 1
è!!!!!!10 =, 9x → 3
è!!!!!!10 , y → − 1 è!!!!!!10 ==
This uses the solutions to evaluate the expression x + y.
In[15]:= x + y /. % Out[15]= 9−$%%%%%%2
5 , $%%%%%%2 5 =
Mathematica can solve any set of simultaneous linear equations. It can also solve a large class of simultaneous polyno-mial equations. Even when it does not manage to solve the equations explicitly, Mathematica will still usually reduce them to a much simpler form.
When you are working with sets of equations in several variables, it is often convenient to reorganize the equations by
This eliminates y between the two equations, giving a single equation for x.
In[16]:= Eliminate[{a x + y == 0, 2 x + (1-a) y == 1}, y]
Out[16]= H2 − a + a2L x 1
If you have several equations, there is no guarantee that there exists any consistent solution for a particular variable.
There is no consistent solution to these equations, so Mathematica returns {}, indicating that the set of solutions is empty.
In[17]:= Solve[{x==1, x==2}, x]
Out[17]= 8<
There is also no consistent solution to these equations for almost all values of a.
In[18]:= Solve[{x==1, x==a}, x]
Out[18]= 8<
The general question of whether a set of equations has any consistent solution is quite a subtle one. For example, for most values of a, the equations {x==1, x==a} are inconsistent, so there is no possible solution for x. However, if a is equal to 1, then the equations do have a solution. Solve is set up to give you generic solutions to equations. It discards any solutions that exist only when special constraints between parameters are satisfied.
If you use Reduce instead of Solve, Mathematica will however keep all the possible solutions to a set of equations, including those that require special conditions on parameters.
This shows that the equations have a solution only when a==1. The notation a==1 && x==1 represents the requirement that both a==1 and x==1 should be True.
In[19]:= Reduce[{x==a, x==1}, x]
Out[19]= a 1 && x 1
This gives the complete set of possible solutions to the equation. The answer is stated in terms of a combination of simpler equations. && indicates equations that must simultaneously be true; || indicates alternatives.
In[20]:= Reduce[a x - b == 0, x]
Out[20]= b 0 && a 0»» a ≠ 0 && x b a
This gives a more complicated combination of equations.
In[21]:= Reduce[a x^2 - b == 0, x]
This gives a symbolic representation of all solutions.
In[22]:= Reduce[Sin[x] == a, x]
Out[22]= C@1D ∈ Integers && Hx π − ArcSin@aD + 2 π C@1D »» x ArcSin@aD + 2 π C@1DL
Solve@ lhs == rhs, x D solve an equation for x Solve@ 8 lhs1 == rhs1, lhs2
== rhs2, … <, 8 x, y, … < D
solve a set of simultaneous equations for x , y , … Eliminate@ 8 lhs1 == rhs1,
lhs2 == rhs2, … <, 8 x, … < D
eliminate x , … in a set of simultaneous equations Reduce@ 8 lhs1 == rhs1, lhs2
== rhs2, … <, 8 x, y, … < D
give a set of simplified equations, including all possible solutions
Functions for solving and manipulating equations.
Reduce also has powerful capabilities for handling equations specifically over real numbers or integers. Section 3.4.9 discusses this in more detail.
This reduces the equation assuming x and y are complex.
In[23]:= Reduce[x^2 + y^2 == 1, y]
Out[23]= y −è!!!!!!!!!!!!!1 − x2 »» y è!!!!!!!!!!!!!1 − x2
This includes the conditions for x and y to be real.
In[24]:= Reduce[x^2 + y^2 == 1, y, Reals]
Out[24]= −1 ≤ x ≤ 1 &&Iy −è!!!!!!!!!!!!!1 − x2 »» y è!!!!!!!!!!!!!1 − x2M
This gives only the integer solutions.
In[25]:= Reduce[x^2 + y^2 == 1, y, Integers]
Out[25]= x −1 && y 0»» x 0 && y −1 »» x 0 && y 1 »» x 1 && y 0
1.5.8 Inequalities
Reduce@ ineqs, 8 x, y, … < D reduce a collection of inequalities FindInstance@ ineqs, 8 x, y, … < D find an instance that satisfies the ineqs
Handling inequalities.
This finds a reduced form for the inequalities.
In[1]:= Reduce[x + y < 1 && y > x > 0, {x, y}]
Out[1]= 0 < x < 1
2 && x < y < 1 − x
These inequalities can never be satisfied.
In[2]:= Reduce[x + y < 1 && y > x > 1, {x, y}]
It is easy to end up with rather complicated results.
Equations can often be solved to give definite values of variables. But inequalities typically just define regions that can only be specified by other inequalities. You can use FindInstance to find definite values of variables that satisfy a particular set of inequalities.
This finds a point in the region specified by the inequalities.
In[4]:= FindInstance[x + y < 1 && y^2 > x > 0, {x, y}]
Out[4]= 99x → 72, y → −3==
Minimize@ 8 expr, ineq <, 8 x, y, … < D
minimize expr while satisfying ineqs Maximize@ 8 expr,
ineq <, 8 x, y, … < D
maximize expr while satisfying ineqs
Constrained minimization and maximization.
This gives the maximum, together with where it occurs.
In[5]:= Maximize[{x^2 + y, x^2 + y^2 <= 1}, {x, y}]
Solving an ordinary differential equation.
Here is the solution to the differential equation y£HxL = a y HxL + 1. C[1] is a coefficient which must be determined from boundary conditions.
In[1]:= DSolve[ y'[x] == a y[x] + 1, y[x], x ] Out[1]= 99y@xD → − 1a + a xC@1D==
If you include an appropriate initial condition, there are no undetermined coefficients in the solution.
In[2]:= DSolve[ {y'[x] == a y[x] + 1, y[0] == 0}, y[x], x ] Out[2]= 99y@xD → −1 +a a x==