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.
Verification of the contents of variables x1, x2 :
Example 2. We solve polynomial equation and store the result in variable 'z'. Then we extract some of the roots from the list of results.
Here are their numerical values with 12 decimal digits:
N[x3,12]
N[x5,12]
Example 3. We tray to solve exactly equation , 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.
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]
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}]
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]]
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}]
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.
The numerical values of the roots are:
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.
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]
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]]
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 ]]
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.
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:
If we give the variables numerical values, however, things look differently:
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[%]
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}]
Example 17. Expressing one value by using the other.
f1=Reduce[ur1,y]
f2=Reduce[ur2,y]
Created by Mathematica (December 21, 2007)