Solving equations and systems of equations with Mathematica functions
Solve,  Eliminate, Reduce, FindRoot  

It must be noted that the build-in function Solve[ ] always trays to apply a mathematical formulae to solve exactly the equations. If this function does not find the result then we tray the functions NSolve, Eliminate, Reduce, FindRoot, ...

Example 1.  We solve the quadratic equation and extract its roots from the list of solutions.

Solve[x^2  4]              ... e the use of logical equality  instead of =   *) x1 = x/.%[[1]] x2 = x/.%%[[2]]

{{x -2}, {x2}}

-2

2

Verification of the contents of variables x1,  x2 :

x1 x2

-2

2

Example 2.  We solve polynomial equation x^6 - 10  and store the result in variable  'z'. Then we extract some of the roots from the list of results.  

z = Solve[x^6 - 10] x3 = x/.z[[3]] x5 = x/.z[[5]]

{{x -1}, {x1}, {x -(-1)^(1/3)}, {x (-1)^(1/3)}, {x -(-1)^(2/3)}, {x (-1)^(2/3)}}

-(-1)^(1/3)

-(-1)^(2/3)

Here are their numerical values with 12 decimal digits:

N[x3,12]
N[x5,12]

RowBox[{RowBox[{-, 0.500000000000}], -, RowBox[{0.866025403784,  , }]}]

RowBox[{0.500000000000, -, RowBox[{0.866025403784,  , }]}]

Example 3.  We tray to solve exactly equation   x^7 - 5x^3 (x + 1)^(1/2), but only the rules are shown. Then we tray to solve it numerically by the function NSolve[ ]. The equation posses two real roots and six complex roots.

r = Solve[x^7 - 5x^3 (x + 1)^(1/2), x] r = NSolve[x^7 - 5x^3 (x + 1)^(1/2), x]

{{xRoot[-1 - #1 + 25 #1^6 - 10 #1^10 + #1^14&, 1]}, {xRoot[-1 - #1 + 25 #1 ... 1^6 - 10 #1^10 + #1^14&, 13]}, {xRoot[-1 - #1 + 25 #1^6 - 10 #1^10 + #1^14&, 14]}}

RowBox[{{, RowBox[{RowBox[{{, RowBox[{x, , 1.52763}], }}], ,, RowBox[{{, RowBox[{x, &# ... 71,  , }]}]}], }}], ,, RowBox[{{, RowBox[{x, , RowBox[{-, 0.520013}]}], }}]}], }}]

Example 4.  An attempt to solve exatly two trigonometric equations, but it is not always possible.

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

Solve :: ifun : Inverse functions are being used by Solve, so some solutions may not be found; use Reduce for complete solution information.  More…

{{x -ArcCos[a]}, {xArcCos[a]}}

Solve :: tdep : The equations appear to involve the variables to be solved for in an essentially non-algebraic way.  More…

Solve[Cos[x] 2 x, x]

Example 5. To solve  the second equation in Example 4 we draw the graphics to find the root location and carry out the problem by Newton's method realized by FindRoot[ ] build-in function. The initial guess in this example can be x=0.

Plot[Cos[x]-2x,{x,-5,5}]
FindRoot[Cos[x]== 2x, {x,0}]

[Graphics:HTMLFiles/index_23.gif]

⁃Graphics⁃

RowBox[{{, RowBox[{x, , 0.450184}], }}]

Example 6.  By analogy we solve a system of two equations and extract the first roots in variables  а,  b :

f = 3x + y-5
g =-x + 2y+1
Solve[{f==0,g==0},{x,y}]    (*  In the first curly brackets we indicate the list of equations *)
              (*  and in the second curly brackets the list of unknowns x,y *)
N[%]
a= x/. %[[1]]
b= y/. %%[[1]]

-5 + 3 x + y

1 - x + 2 y

{{x11/7, y2/7}}

RowBox[{{, RowBox[{{, RowBox[{RowBox[{x, , 1.57143}], ,, RowBox[{y, , 0.285714}]}], }}], }}]

1.57143

0.285714

Example 7.  Verification by substitution:

a
b
f /. {x->a,y->b}
g /. {x->a,y->b}

1.57143
0.285714
0.
0.

Example 8.  Solving a parametric system:

Solve[{c*x + y==0, 3x + (1+c)y == 1}, {x,y}]

{{x -1/(-3 + c + c^2), yc/(-3 + c + c^2)}}

Example 9.  We solve a system of two equations. Sometimes the output is very long (the next is about four pages formulae) and may be useless as well.  If you want to hide/unhide it you must make a double click on the right closed blue bracket.  

p = x^2 + 2y 3 ;     q = 3x + y^21 ; Solve[ {p, q }, {x, y}]

The numerical values of the roots are:

%//N

RowBox[{{, RowBox[{RowBox[{{, RowBox[{RowBox[{x, , RowBox[{RowBox[{1.70919, }] ... Box[{RowBox[{x, , RowBox[{-, 0.354983}]}], ,, RowBox[{y, , 1.43699}]}], }}]}], }}]

Example 10. We will solve a system of two equations with two unknowns x, y. For convenience we store the equations in two auxiliary cells ur1 and ur2. Notece the use of the symbol ==  (equality) instead of assingment symbol =.  We try to accurately solve the equation with the function Solve with respect to {x,y}.  It gives as a result the possible EXACT roots and a description of the rest based on the rules the equations are solved - in this case by replacing  ' y '. After that we find the numerical values of the result. In the end we derive the values of the third pair of solutions in separate cells x3 and y3, which would allow us to use them later if necessary. Try to solve other equations the same way to derive the roots.

ur1 = x^3 - 2x y - 10 ur2 = y^2/x + 4y - x -1 Solve[{ur1, ur2}, {x, y}]   ... p;(*This is better to obtain the approximate numerical solutions*) x3 = x/.%[[3]] y3 = y/.%%[[3]]

-1 + x^3 - 2 x y0

-x + 4 y + y^2/x -1

{{y0, x1}, {y1/2 Root[-1 - #1 + 7 #1^2 + 5 #1^3 + 9 #1^4 + #1^5&,  ... ^3 + 9 #1^4 + #1^5&, 5]^4), xRoot[-1 - #1 + 7 #1^2 + 5 #1^3 + 9 #1^4 + #1^5&, 5]}}

RowBox[{{, RowBox[{RowBox[{{, RowBox[{RowBox[{y, , 0.}], ,, RowBox[{x, , 1.}]} ... [{x, , RowBox[{RowBox[{-, 0.265139}], +, RowBox[{0.952031,  , }]}]}]}], }}]}], }}]

RowBox[{-, 0.327031}]

1.58238

Example 11. With complex examples it is worth it to try an eventual simplification by eliminating some unknowns in some equations. Here is the elimination of ' x ' and ' y ' for the system from example 10. As a result we recieve equations of the sixth power for ' y ' and ' x ' respectively.

opr1=Eliminate[{ur1,ur2},x]
opr2=Eliminate[{ur1,ur2},y]

10 y + 43 y^2 + 74 y^3 - 12 y^4 - 36 y^5 + y^6 == 0

-8 x^2 + 2 x^3 - 4 x^4 + 8 x^5 + x^6 == -1

Example 12. Here the equations from the previous example are solved, but we need to couple the values of corresponding unknowns.

rey=N[Solve[opr1,y]]
rex=N[Solve[opr2,x]]

RowBox[{{, RowBox[{RowBox[{{, RowBox[{y, , 0.}], }}], ,, RowBox[{{, RowBox[{y,  ... Box[{y, , RowBox[{RowBox[{-, 0.282294}], +, RowBox[{0.23497,  , }]}]}], }}]}], }}]

RowBox[{{, RowBox[{RowBox[{{, RowBox[{x, , 1.}], }}], ,, RowBox[{{, RowBox[{x,  ... ox[{x, , RowBox[{RowBox[{-, 0.265139}], +, RowBox[{0.952031,  , }]}]}], }}]}], }}]

This problem is solved like this: we separate the second root for x from the upper result (second line) and we replace it in one of the  initial  equations. The derived equation is solved with regard to ' y ' and recieve the corresponding solution. The final solution is:        { x = - 8.5107,  y = 36.2737 }.

x2=x/.rex[[2]]
ur11=ur1 /. x->x2
N[Solve[ur11,y ]]

RowBox[{-, 8.51057}]

RowBox[{RowBox[{RowBox[{-, 617.419}], +, RowBox[{17.0211,  , y}]}], ==, 0}]

RowBox[{{, RowBox[{{, RowBox[{y, , 36.2737}], }}], }}]

Example 13. Mathematica calculates homogeneous equation systems as well. Below is a system of three equations with three unknowns. Notice that here we obtain the EXACT solutions.

Clear[x, y, z] Solve[{x^2 + 2y - 3z0, x - z - y0, -x + 6y + 4z0}, {x, y, z}]

{{y -63/4, z105/4, x21/2}, {y0, z0, x0}}

Example 14. All possible cases with examination can be derived with the function Reduce. It uses presentation of the result with the logical functions || (logical addition) and && (logical multiplication). Here is how a quadratic equation with random parameters a, b, c looks like:

Reduce[a x^2 + b x + c0, x]

x == (-b - (b^2 - 4 a c)^(1/2))/(2 a) &&a≠0 || x == (-b + (b^2 - 4 a c)^(1/2))/( ... ≠0 || a == 0&&b == 0&&c == 0 || a == 0&&x == -c/b&&b≠0

    If we give the variables numerical values, however, things look differently:

a = 5 Reduce[a x^2 + b x + c0, x] b = 6 Reduce[a x^2 + b x + c0, x] c = -3 Reduce[a x^2 + b x + c0, x] N[%]

5

x == 1/5 (-3 - 2 6^(1/2)) || x == 1/5 (-3 + 2 6^(1/2))

6

x == 1/5 (-3 - 2 6^(1/2)) || x == 1/5 (-3 + 2 6^(1/2))

-3

x == 1/5 (-3 - 2 6^(1/2)) || x == 1/5 (-3 + 2 6^(1/2))

RowBox[{RowBox[{x, ==, RowBox[{-, 1.5798}]}], ||, RowBox[{x, ==, 0.379796}]}]

Example 15. Here we show the solution to example 10 with the function Reduce. Since the result is a long and complicated logical expression, we will not show it by writing ; (semicolon) at the end of the operator. Immediately after that the numerical  value of the results follow.

Reduce[{ur1, ur2},{x,y}];
N[%]

RowBox[{RowBox[{RowBox[{x, ==, 1.}], &&, RowBox[{y, ==, 0.}]}], ||, RowBox[{RowBox[{x, ... amp;&, RowBox[{y, ==, RowBox[{RowBox[{-, 0.282294}], +, RowBox[{0.23497,  , }]}]}]}]}]

Example 16. In the end we also give the solution to the same problem with a numerical method, by setting the inital guess for the roots. By setting real guess a real root is found and by setting complex guess an eventual complex root is found. Try with other inital values to find othere roots.

FindRoot[{ur1,ur2},{x,-1},{y,-1}]
FindRoot[{ur1,ur2},{x,-0.1+i},{y,i}]

RowBox[{{, RowBox[{RowBox[{x, , 1.}], ,, RowBox[{y, , 1.88287*10^-19}]}], }}]

RowBox[{{, RowBox[{RowBox[{x, , RowBox[{RowBox[{-, 0.265139}], +, RowBox[{0.952031,  , ... , RowBox[{y, , RowBox[{RowBox[{-, 0.282294}], +, RowBox[{0.23497,  , }]}]}]}], }}]

Example 17. Expressing one value by using the other.

f1=Reduce[ur1,y]
f2=Reduce[ur2,y]

x≠0&&y (-1 + x^3)/(2 x)

x≠0&& (y -2 x - (-x + 5 x^2)^(1/2) || y -2 x + (-x + 5 x^2)^(1/2))


Created by Mathematica  (December 21, 2007)