Solvers

Solve[expr, vars] - attempts to solve the system expr of equations or inequalities for the variables vars.

The solution returned by Solve[ ...] is a list of lists of transformation rules.

Solving equations

Clear[x]

Mathematica can solve various kinds of equations. For example set

eqn= (x^2)+ 2 a x + b==0

Note that the Set operator (=) has lower precedence than the Equal operator (==). To solve this equation for x you use the command

sol=Solve[eqn,x]

Note that the result is given as a list with two lists of rules. Each sublist represents a distinct solution.

Transformation rules

The association x->-a-(Sqrt[(a^2)-b]) is called a transformation rule. 
How can we extract the useful value “-a-(Sqrt[(a^2)-b])” from the transformation rule?

"Naive Idea"

sol[[1]]

sol[[1]][[1]]

sol[[1]][[1]][[2]]

"Better Idea"

Values[sol]

Keys[sol]

We can get rid of one set of curly brackets by using the command Flatten[ ...]

Flatten[Values[sol]]

However, we lost the connection between the name of the variables and the numeric solutions obtained.

To verify that this is indeed a solution of the quadratic equation, we can substitute the solution back into the equation and simplify the result.

eqn /. sol

Simplify[eqn /. sol]

You can now also use the solutions for other computations. Suppose you want to evaluate some function f1 on the solution set:

f1[x] /. sol

Short exercise: We know that sol contains the roots of the quadratic polynomial (given as a list of transformation rules) and we want to check (using transformation rules!) that the sum of the two roots is -2a and the product of the two roots is b.

Transformation rules & Replacement Operator “/.”

expr /. rule lhs->rhs     Applies a transformation rule lhs->rhs to expression expr
The replacement operator  (pronounced “slash-dot”) applies rules to expressions.

Sin[x] /. x->4

If you give a list of rules, you get a list of results. Each rule will be tried once on each part of the expression.

Sin[x+y] /. {x->4, y->3}

Sin[x+y] /. {x->Pi, y->Pi/2}

x+x^2+y*x+x^3 /. {x^3->x, y->x, x->20}

If you give a list of lists of rules, you get a list of results.

Sin[x] /. {{x->0}, {x->1}, {x->2}, {x->3}, {x->4}}

Sin[x+y] /. {{x->Pi, y->Pi/2},{x->4, y->3}}

Short exercise : Use the transformation rule to change the 0 - rows of a matrix like {{1,2,3}, {0,0,0}, {0,1,0}, {0,0,0}, {0,2,3}} with 1 - rows.

Back to the quadratic equation

To verify that sol is indeed a solution of the quadratic equation eqn, we can substitute the solution back into the equation and simplify the result.

eqn /. sol

Simplify[eqn /. sol]

You can now also use the solutions for other computations. Suppose you want to evaluate some function f1 on the solution set:

f1[x_]:=10x
f1[x] /. sol

Short exercise: We know that sol contains the roots of the quadratic polynomial (given as a list of transformation rules) and we want to check (using transformation rules!) that the sum of the two roots is -2a and the product of the two roots is b.

Numerical Solvers

NSolve[expr, vars] -- attempts to find numerical approximations to the solutions of the system expr of equations or inequalities for the variables vars.
NSolve[expr,vars,Reals] -- finds solutions over the domain of real numbers.

Example: Find the solutions of a polynomial equation of degree 5

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

NSolve[x^5-2 x+3==0,x]

NSolve[x^5-2 x+3==0,x,Reals]

Newton' s Method

Numeric solvers that are used for finding real roots usually implement Newton' s Method.

Newton' s Method is a method for finding successively better approximations to the roots (or zeroes) of a real - valued differentiable function f : [a,b] -> R. Suppose that f has a single root in the interval [a,b]. The idea of the method is as follows: one starts with an initial guess which is reasonably close to the true root, then the function is approximated by its tangent line (which can be computed using the tools of calculus), and one computes the x-intercept of this tangent line (which is easily done with elementary algebra). This x-intercept will typically be a better approximation to the function’s root than the original guess, and the method can be iterated.

Suppose we have some current approximation (Subscript[x, n]). The equation of the tangent line to the curve y = ƒ(x) at the point  ((Subscript[x, n]),f((Subscript[x, n]))) is 
y = f ’((Subscript[x, n]))(x-(Subscript[x, n])) + f((Subscript[x, n])). 
To find the intersection point of the tangent line with the x-axis we set y=0 and solve for x. We get 
 (Subscript[x, n+1])=(Subscript[x, n])-((f((Subscript[x, n])))/(f '((Subscript[x, n])))). We get a sequence of successive approximations (Subscript[x, 1]),... (Subscript[x, n]), (Subscript[x, n+1, ... ]) of the root of the function f, in the interval [a,b].
 The method will usually converge, provided this initial guess  (Subscript[x, 1]) is close enough to the unknown zero, and that ƒ’((Subscript[x, 1])) ≠ 0  (i.e. the tangent line is not horizontal).

Functions with no name (pure functions #)

#^5&

When solving equations where solutions do not have any closed form, you may see these kind of functions returned by Mathematica. These are functions with no name, or pure functions.

#    represents the first variable (first argument) in a pure function 
#n   represents the n^th variable (argument) in a pure function
&   the ampersand marks the end of the pure function

The name of a function is irrelevant if you do not intend to refer to the function again, so the Wolfram language lets you define functions with no names. Pure functions in the Wolfram Language can take any number of arguments. The Wolfram Language allows you to avoid using explicit names for the arguments of pure functions, and instead to specify the arguments by giving “slot numbers” . In a Wolfram Language pure function, #n  stands for the nth argument you supply. # stands for the first argument.

#^5 &   is simply the function that takes any number and raises it to power 5. We can use this function to compute 2^5 for example.  
#1+#2 & is a function that computes the sum of two numbers.

#^5&[2]

#1+#2 &[1,2]

Example : Sort the list L = {{1, 2}, {2, 5}, {1, 1}, {4, 8}, {3, 4}, {9,0}, {7,6}} in ascending order, according to the following criterion : {a, b} < {c, d} iff  b < d

L = {{1, 2}, {2, 5}, {1, 1}, {4, 8}, {3, 4},{9,0},{7,6}};
Sort[L]
Sort[L,Greater]
Sort[L, #1[[2]]<#2[[2]]&]

Short Exercise : Sort a list of the form  S = {{1, 1 -> 2}, {2, 2 -> 5}, {1, 1 -> 1}, {4, 4 -> 8}, {3, 3 -> 4}, {9, 9 -> 0}, {7, 7-> 6}} according to the following criterion: {a, a->b} < {c, c->d} iff b<d