Solving Equations

An expression like represents an equation in Mathematica. You will often need to solve equations like this, to find out for what values of they are true.

This gives the two solutions to the quadratic equation . The solutions are given as replacements for .
In[1]:=
Click for copyable input
Out[1]=
Here are the numerical values of the solutions.
In[2]:=
Click for copyable input
Out[2]=
You can get a list of the actual solutions for by applying the rules generated by Solve to using the replacement operator.
In[3]:=
Click for copyable input
Out[3]=
You can equally well apply the rules to any other expression involving .
In[4]:=
Click for copyable input
Out[4]=
Solve[lhs==rhs,x]solve an equation, giving a list of rules for x
x/.solutionuse the list of rules to get values for x
expr/.solutionuse 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 in terms of radicals cannot be given. If you have an algebraic equation in one variable, and the highest power of the variable is at most four, then Mathematica can always give you formulas for the solutions. However, if the highest power is five or more, it may be mathematically impossible to give explicit algebraic formulas for all the solutions.

Mathematica can always solve algebraic equations in one variable when the highest power is less than five.
In[5]:=
Click for copyable input
Out[5]=
It can solve some equations that involve higher powers.
In[6]:=
Click for copyable input
Out[6]=
There are some equations, however, for which it is mathematically impossible to find explicit formulas for the solutions. Mathematica uses Root objects to represent the solutions in this case.
In[7]:=
Click for copyable input
Out[7]=
Even though you cannot get explicit formulas, you can still evaluate the solutions numerically.
In[8]:=
Click for copyable input
Out[8]=

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]:=
Click for copyable input
Out[9]=

It is important to realize that an equation such as actually has an infinite number of possible solutions, in this case differing by multiples of . 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]:=
Click for copyable input
Out[10]=
You can find an approximate numerical solution using FindRoot, and giving a starting value for .
In[11]:=
Click for copyable input
Out[11]=

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 .
In[12]:=
Click for copyable input
Out[12]=
Solve[{lhs1==rhs1,lhs2==rhs2,...},{x,y,...}]
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 and .
In[13]:=
Click for copyable input
Out[13]=
Here are some more complicated simultaneous equations. The two solutions are given as two lists of replacements for and .
In[14]:=
Click for copyable input
Out[14]=
This uses the solutions to evaluate the expression .
In[15]:=
Click for copyable input
Out[15]=

Mathematica can solve any set of simultaneous linear or polynomial equations.

When you are working with sets of equations in several variables, it is often convenient to reorganize the equations by eliminating some variables between them.

This eliminates between the two equations, giving a single equation for .
In[16]:=
Click for copyable input
Out[16]=

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]:=
Click for copyable input
Out[17]=
There is also no consistent solution to these equations for almost all values of .
In[18]:=
Click for copyable input
Out[18]=

The general question of whether a set of equations has any consistent solution is quite a subtle one. For example, for most values of , the equations are inconsistent, so there is no possible solution for . However, if is equal to , 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 . The notation represents the requirement that both and should be True.
In[19]:=
Click for copyable input
Out[19]=
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]:=
Click for copyable input
Out[20]=
This gives a more complicated combination of equations.
In[21]:=
Click for copyable input
Out[21]=
This gives a symbolic representation of all solutions.
In[22]:=
Click for copyable input
Out[22]=
Solve[lhs==rhs,x]solve an equation for x
Solve[{lhs1==rhs1,lhs2==rhs2,...},{x,y,...}]
solve a set of simultaneous equations for x, y, ...
Eliminate[{lhs1==rhs1,lhs2==rhs2,...},{x,...}]
eliminate x, ... in a set of simultaneous equations
Reduce[{lhs1==rhs1,lhs2==rhs2,...},{x,y,...}]
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. "Equations and Inequalities over Domains" discusses this in more detail.

This reduces the equation assuming and are complex.
In[23]:=
Click for copyable input
Out[23]=
This includes the conditions for and to be real.
In[24]:=
Click for copyable input
Out[24]=
This gives only the integer solutions.
In[25]:=
Click for copyable input
Out[25]=
New to Mathematica? Find your learning path »
Have a question? Ask support »