Solving Equations

An expression like x^2+2x-7==0 represents an equation in the Wolfram Language. 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 (x^2)+2x-7==0. The solutions are given as replacements for x:

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

Here are the numerical values of the solutions:

N[%]

You can get a list of the actual solutions for x by applying the rules generated by (RefLink[Solve, paclet:ref/Solve]) to x using the replacement operator:

x/.%

You can equally well apply the rules to any other expression involving x:

x^2+3 x/.%%

Finding and using solutions to equations.

(RefLink[Solve, paclet:ref/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 the Wolfram Language 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.

The Wolfram Language can always solve algebraic equations in one variable when the highest power is less than five:

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

It can solve some equations that involve higher powers:

Solve[x^6==1,x]

There are some equations, however, for which it is mathematically impossible to find explicit formulas for the solutions. The Wolfram Language uses (RefLink[Root, paclet:ref/Root]) objects to represent the solutions in this case:

Solve[2-4 x+x^5==0,x]

Even though you cannot get explicit formulas, you can still evaluate the solutions numerically:

N[%]

In addition to being able to solve purely algebraic equations, the Wolfram Language can also solve some equations involving other functions.

After printing a warning, the Wolfram Language returns one solution to this equation:

Solve[Sin[x]==a,x]

It is important to realize that an equation such as sin(x)==a actually has an infinite number of possible solutions, in this case differing by multiples of 2π. However, (RefLink[Solve, paclet:ref/Solve]) by default returns just one solution, but prints a message telling you that other solutions may exist. You can use (RefLink[Reduce, paclet:ref/Reduce]) to get more information.

There is no explicit "closed form" solution for a transcendental equation like this:

Solve[Cos[x]==x,x]

You can find an approximate numerical solution using (RefLink[FindRoot, paclet:ref/FindRoot]), and giving a starting value for x:

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

(RefLink[Solve, paclet:ref/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.

The Wolfram Language returns a result in terms of the formal inverse function of f:

Solve[f[x^2]==a,x]

Solving sets of simultaneous equations.

You can also use the Wolfram Language 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:

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

Here are some more complicated simultaneous equations. The two solutions are given as two lists of replacements for x and y:

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

This uses the solutions to evaluate the expression x+y:

x+y/.%

The Wolfram Language 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 y between the two equations, giving a single equation for x:

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

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 the Wolfram Language returns {}, indicating that the set of solutions is empty:

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

There is also no consistent solution to these equations for almost all values of a:

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

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. (RefLink[Solve, paclet:ref/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 (RefLink[Reduce, paclet:ref/Reduce]) instead of (RefLink[Solve, paclet:ref/Solve]), the Wolfram Language 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 (RefLink[True, paclet:ref/True]):

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

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:

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

This gives a more complicated combination of equations:

Reduce[a x^2-b==0,x]

This gives a symbolic representation of all solutions:

Reduce[Sin[x]==a,x]

Functions for solving and manipulating equations.

(RefLink[Reduce, paclet:ref/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 x and y are complex:

Reduce[x^2+y^2==1,y]

This includes the conditions for x and y to be real:

Reduce[x^2+y^2==1,y,Reals]

This gives only the integer solutions:

Reduce[x^2+y^2==1,y,Integers]