How Do I Solve Equations with Mathematica?
Author
Wolfram Research, Inc.
Title
How Do I Solve Equations with Mathematica?
Description
Also available in Japanese http://library.wolfram.com/howtos/equations/index.ja.html
Category
Educational Materials
Keywords
URL
http://www.notebookarchive.org/2018-10-10r855j/
DOI
https://notebookarchive.org/2018-10-10r855j
Date Added
2018-10-02
Date Last Modified
2018-10-02
File Size
68.2 kilobytes
Supplements
Rights
Redistribution rights reserved




How Do I Solve Equations with Mathematica?
How Do I Solve Equations with Mathematica?
Polynomial Equations
Polynomial Equations
Mathematica provides two main functions for solving polynomial equations. They are Solve and NSolve. Solve is used to algebraically solve an equation or set of equations. However, it is not always possible to solve an equation algebraically and there are times when a numerical solution is desired instead. In either case, NSolve can be used to find a numeric solution to an equation or set of equations.
■
Algebraic Solutions
Algebraic Solutions
Solve is the Mathematica function used for symbolically solving a polynomial equation or set of equations. Its syntax is Solve[eqns, vars], where eqns is your equation or set of equations and vars are the variable(s) in the equation(s). The equations are written in the form of lefthandside == righthandside. For example, the equation would be entered as
a+bx+c=0
2
x
a+bx+c0
2
x
Be sure to use the double equal sign when entering equations in Mathematica. For a more detailed explanation on using = versus ==, please see section of the Mathematica book.
1.5.5
◼
Single Equation
Single Equation
Let's solve the previously mentioned equation for x.
a+bx+c=0
2
x
Solve[a+bx+c0,x]
2
x
x-b-(-4ac)(2a),x-b+(-4ac)(2a)
2
b
2
b
◼
Special Topic: Algebraic Numbers
Special Topic: Algebraic Numbers
The following sections give a high level introduction to the way Mathematica handles algebraic numbers. It is not essential to understanding the Mathematica equation solvers and is just included for interested readers.
The text is adapted from a talk presented by Robert Knapp at the Japanese Society for Synmbolic and Algebraic Computing.
The text is adapted from a talk presented by Robert Knapp at the Japanese Society for Synmbolic and Algebraic Computing.
For higher order polynomials, it is not always possible to represent the exact solution in closed form. Currently, the highest polynomial that a closed form solution has been found for is the Quinoid or 5th order polynomial. However, the expression represents an exact mathematical quantity, which is based on the abstraction of algebraic numbers, represented in Mathematica as Root objects.
The method Mathematica uses internally to calculate roots of polynomials is the well established Jenkins-Traub method. It works by finding successive roots with what is essentially a numerically stable variant of the Newton-Raphson method. Then the polynomial is deflated by dividing out by a factor involving each root that is found. This is the engine behind algebraic numbers. What makes it possible to have an implicit representation which is reasonably efficient are mathematical theorems which allow roots to be isolated and Mathematica's significance arithmetic.
Lets take a closer look at the roots of a higher order equation.
Solve[-+σx+1==0,x]
5
x
3
x
{{xRoot[1+σ#1-+&,1]},{xRoot[1+σ#1-+&,2]},{xRoot[1+σ#1-+&,3]},{xRoot[1+σ#1-+&,4]},{xRoot[1+σ#1-+&,5]}}
3
#1
5
#1
3
#1
5
#1
3
#1
5
#1
3
#1
5
#1
3
#1
5
#1
gives the solution to the equation. This is given in terms of replacement rules so that you can see how the variables relate to the solution. In this case it indicates that there are five possible solutions. In cases where you just want to work with the roots, you can extract them into a list.
polyroots=x/.%
{Root[1+σ#1-+&,1],Root[1+σ#1-+&,2],Root[1+σ#1-+&,3],Root[1+σ#1-+&,4],Root[1+σ#1-+&,5]}
3
#1
5
#1
3
#1
5
#1
3
#1
5
#1
3
#1
5
#1
3
#1
5
#1
A plot of the root positions in the complex plane as a function of the parameter value helps to visualize the numbers implicitly represented here. First, an auxiliary package is loaded in order to highlight the roots with different colors.
Needs["Graphics`Colors`"];
ParametricPlot[Evaluate[{Re[#],Im[#]}&/@roots],{σ,-2,2},PlotStyle->{Red,Blue,Green,Black,Purple}];
It is interesting to note that two roots come together on the real axis, at the point σ = -1. The lines going across the plot are an artifact of the canonical order Mathematica gives to the roots. You may be asking: how does Mathematica distinguish between and order the roots? The process is an elegant example of numerical and symbolic mathematics working together. We will work with the last root to illustrate this and to make things a bit more convenient we define the root as a function of the parameter:
lastroot[σ_]=Last[roots];
When the parameter σ is given a numerical value, Mathematica does the computations necessary to determine an approximate root which is precise enough to be isolated from the other roots of the polynomial.
lastroot[0]
Root[1-+&,5]
3
#1
5
#1
First, roots approximations at machine precision are found using the Jenkins-Traub algorithm. The isolation is possible because for a given root approximation it is possible to find an enclosing circle in which an exact root of the polynomial is guaranteed to lie. Isolation amounts to making sure that none of the circles around the approximate values overlap. Optionally Mathematica allows you to use an exact root isolation algorithm which is more complicated and time consuming. See [Strzebonski, 1996] for details on both methods of isolation. When σ = 0, the roots are reasonably far apart from each other, so that 16 digits is sufficient to ensure isolation. Once the roots are isolated, they are ordered based on a canonical ordering. Real roots are ordered first along the real line, then complex ones based on complex absolute value. This is why you see the lines across the plot above at σ = -1. The pairs of roots switch to maintain the canonical ordering.
When σ = -1, the polynomial is reducible in terms of radicals, and Mathematica recognizes this, so
lastroot[-1]
Root[-1++&,4]
3
#1
4
#1
is given in its simplest from. However, for σ sightly greater than -1, the polynomial cannot be reduced, and so
lastroot[-1+]
-30
10
Root[1000000000000000000000000000000-999999999999999999999999999999#1-1000000000000000000000000000000+1000000000000000000000000000000&,5]
3
#1
5
#1
is given in terms of a root object. Notice that the polynomial is recalculated to be the minimal polynomial with integer coefficients. Internally Mathematica recognizes that the two roots are very close together and adapts to using higher precision (in this case 32 digits) to do the root isolation for these. Then, when you request a numerical value,
N[%]
-0.219447+0.914474
the positive imaginary part is indicated correctly.
Hopefully this example has given you a rough idea of how the fusion of implicit representations with numerical approximations using adaptive precision, yields a powerful computational tool. Mathematica provides a framework where objects like algebraic numbers fit naturally. You can similarly use Mathematica to set up implicit representations of your own objects by defining numeric and symbolic rules which apply to them. Implicit representations can be thought of in some sense as a transparent interface to a combination of powerful numerical algorithms and mathematical information without overburdening a user with the implementation details.
⌚
Special Topic: Finding Special Solutions
Special Topic: Finding Special Solutions
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. However, the function Reduce will keep all the possible solutions to a set of equations, including those that require special conditions on parameters.
Reduce[a x^2 - b == 0, x]
a==0&&b==0||x==-b(√a)&&a≠0||x==b(√a)&&a≠0
In this case the output means the possible solutions are(1) a = 0 and b=0or(2) x = - and a is not equal to 0or(3) x = and a is not equal to 0
b
a
b
a
◼
Simultaneous Equations
Simultaneous Equations
Solve is also used in algebraically solving sets of simultaneous equations. Let's solve the two equations +ax+=0 and for x and y.
2
x
2
cy
y+x=5
Solve[{+ax+c0,y+x5},{x,y}]
2
x
2
y
x5-5(1+c)-a(2(1+c))-(-100c-20ac)(2(1+c)),y10+a+(-100c-20ac)(2(1+c)),x5-5(1+c)-a(2(1+c))+(-100c-20ac)(2(1+c)),y10+a-(-100c-20ac)(2(1+c))
2
a
2
a
2
a
2
a
Note that in the syntax above, braces surround both equations and both variables. Another way of entering the equations would be to assign the set of equations a name. For example,
eqns=++==20, x+z==2, y+x==0;
2
x
2
y
2
z
Solve[eqns,{x,y,z}]
{{y-(2/3)-(2√13)/3,z4/3-(2√13)/3,x(2/3)(1+√13)},{y-(2/3)+(2√13)/3,z(2/3)(2+√13),x(2/3)(1-√13)}}
More on .
Solve
■
Numeric Solutions
Numeric Solutions
There is more than one way to get a numeric solution to an equation. For example, to numerically solve the equation +2x-7=0, we could do either of the following.
2
x
First, we solve the equation algebraically and then use N to get a numeric answer.
Solve[+2x-7==0,x]
2
x
{{x-1-2√2},{x-1+2√2}}
N[ % ]
{{x-3.82843},{x1.82843}}
Or, we could use NSolve to numerically solve the equation from the start.
NSolve[+2x-70,x]
2
x
{{x-3.82843},{x1.82843}}
Here's an example of solving a more complicated polynomial equation using NSolve.
poly=3+3x-7-+2+3-3-+;
2
x
3
x
4
x
7
x
8
x
9
x
10
x
NSolve[poly==0,x]
{{x-1.73205},{x-0.868688-0.585282},{x-0.868688+0.585282},{x-0.496292},{x0.0763556-1.14095},{x0.0763556+1.14095},{x1.},{x1.04048-0.56735},{x1.04048+0.56735},{x1.73205}}
NSolve can also be used to numerically solve systems of equations. The syntax is the same as for Solve.
NSolve[{x + y == 2, x - 3 y + z == 3, x - y + z == 0},
{x, y, z}]
{x, y, z}]
{{x3.5,y-1.5,z-5.}}
More on .
NSolve
◼
Special Topic: Large Systems of Polynomial Equations
Special Topic: Large Systems of Polynomial Equations
In many engineering, financial and scientific calculations, researchers have to deal with large systems containing hundreds or thousands of equations. In those cases, linear algebra methods are usually more suited than Solve or NSolve. These methods are covered in through of The Mathematica Book. If you are dealing with sparse systems, please read the documentation for Developer`SparseLinearSolve.
Section 3.7.8
Section 3.7.10
Other Equations
Other Equations
In addition to being able to solve purely algebraic equations, Mathematica can also solve some equations involving other functions. Solve can be used in solving radical equations, equations involving trigonometric or hyperbolic functions and their inverses as well as equations involving exponentials and logarithms. As a reminder its syntax is NSolve[eqns, vars], where eqns is your equation or set of equations and vars are the variable(s) in the equation(s).
■
Algebraic Solutions
Algebraic Solutions
◼
Radical Equations
Radical Equations
Solve1x+1√x==1,x
{{x(1/2)(3+√5)}}
Note that in radical equations, Solve discards parasite solutions. To see all candidate solutions, including parasites, set VerifySolutions to False.
Solve1x+1√x==1,VerifySolutionsFalse
{{x(1/2)(3-√5)},{x(1/2)(3+√5)}}
◼
Equations involving trigonometric or hyperbolic functions and their inverses
Equations involving trigonometric or hyperbolic functions and their inverses
Solve[{Cos[x+α+1/5]==5,Cos[-x+α+2/5]==6},{α,x}]
{{α(1/10)(5(5)+5(6)-3),x(1/10)(5(5)-5(6)+1)}}
-1
cos
-1
cos
-1
cos
-1
cos
◼
Equations involving exponentials and logarithms
Equations involving exponentials and logarithms
Solve[==,x]
a+x
(1+)
3
x
a+x
{x-a},{x-(1-)^(1/3)},{x(-1+)^(1/3)},x(-1+)^(1/3)
2/3
(-1)
More on .
Solve
■
Numeric Solutions
Numeric Solutions
If your equations involve only linear functions or polynomials, then you can use NSolve to get numerical approximations to all the solutions. However, when your equations involve more complicated functions, there is in general no systematic procedure for finding all solutions, even numerically. In such cases, you can use FindRoot to search for solutions. The basic syntax for FindRoot is FindRoot[eqn, {x, }] where eqn is the equation you are solving and is the value around which FindRoot starts its search.
x
0
x
0
FindRoot[ 3 Cos[x] == Log[x], {x, 1} ]
{x1.44726}
More on .
FindRoot
Differential Equations
Differential Equations
Please see the How do I solve differential equations in Mathematica? how to.
More Information
More Information
The Mathematica book and the Built-in Functions section of the Help Browser contain much more information on solving equations in Mathematica. The following are some suggested sections to take a look at if you want to know more about solving equations.
The Mathematica Book Sections
1.5.5
1.5.7
1.5.8
1.6.3
1.6.4
3.4
3.5.10
3.9.5
3.9.6
3.9.7
3.9.8
Built-in Functions in Help Browser
Solve
NSolve
DSolve
NDSolve
FindRoot


Cite this as: Wolfram Research, Inc., "How Do I Solve Equations with Mathematica?" from the Notebook Archive (2004), https://notebookarchive.org/2018-10-10r855j

Download

