Solving Nonlinear Algebraic Equations

  • Svein Linge
  • Hans Petter Langtangen
Open Access
Part of the Texts in Computational Science and Engineering book series (TCSE, volume 15)


As a reader of this book, you might be well into mathematics and often “accused” of being particularly good at solving equations (a typical comment at family dinners!). How true is it, however, that you can solve many types of equations with pen and paper alone? Restricting our attention to algebraic equations in one unknownx, you can certainly do linear equations: ax + b = 0, and quadratic ones: ax2 + bx + c = 0. You may also know that there are formulas for the roots of cubic and quartic equations too. Maybe you can do the special trigonometric equation \(\sin x + \cos x = 1\) as well, but there it (probably?) stops. Equations that are not reducible to one of those mentioned, cannot be solved by general analytical techniques, which means that most algebraic equations arising in applications cannot be treated with pen and paper!

Open image in new window

As a reader of this book, you might be well into mathematics and often “accused” of being particularly good at solving equations (a typical comment at family dinners!). How true is it, however, that you can solve many types of equations with pen and paper alone? Restricting our attention to algebraic equations in one unknownx, you can certainly do linear equations: ax + b = 0, and quadratic ones: ax2 + bx + c = 0. You may also know that there are formulas for the roots of cubic and quartic equations too. Maybe you can do the special trigonometric equation \(\sin x + \cos x = 1\) as well, but there it (probably?) stops. Equations that are not reducible to one of those mentioned, cannot be solved by general analytical techniques, which means that most algebraic equations arising in applications cannot be treated with pen and paper!

If we exchange the traditional idea of finding exact solutions to equations with the idea of rather finding approximate solutions, a whole new world of possibilities opens up. With such an approach, we can in principle solve any algebraic equation.

Let us start by introducing a common generic form for any algebraic equation:
$$\displaystyle \begin{aligned} f(x) = 0\, .\end{aligned} $$
Here, f(x) is some prescribed formula involving x. For example, the equation
$$\displaystyle \begin{aligned} e^{-x}\sin x = \cos x\end{aligned} $$
$$\displaystyle \begin{aligned} f(x)= e^{-x}\sin x - \cos x\, .\end{aligned} $$
Just move all terms to the left-hand side and then the formula to the left of the equality sign is f(x).

So, when do we really need to solve algebraic equations beyond the simplest types we can treat with pen and paper? There are two major application areas. One is when using implicit numerical methods for ordinary differential equations. These give rise to one or a system of algebraic equations. The other major application type is optimization, i.e., finding the maxima or minima of a function. These maxima and minima are normally found by solving the algebraic equation F′(x) = 0 if F(x) is the function to be optimized. Differential equations are very much used throughout science and engineering, and actually most engineering problems are optimization problems in the end, because one wants a design that maximizes performance and minimizes cost.

We first consider one algebraic equation in one variable, for which we present some fundamental solution algorithms that any reader should get to know. Our focus will, as usual, be placed on the programming of the algorithms. Systems of nonlinear algebraic equations with many variables arise from implicit methods for ordinary and partial differential equations as well as in multivariate optimization. Our attention will be restricted to Newton’s method for such systems of nonlinear algebraic equations.

Root Finding

When solving algebraic equations f(x) = 0, we often say that the solution x is a root of the equation. The solution process itself is thus often called root finding.

7.1 Brute Force Methods

The representation of a mathematical function f(x) on a computer takes two forms. One is a Python function returning the function value given the argument, while the other is a collection of points (x, f(x)) along the function curve. The latter is the representation we use for plotting, together with an assumption of linear variation between the points. This representation is also very well suited for equation solving: we simply go through all points and see if the function crosses the x axis, or for optimization: we test for local maximum or minimum points. Because there is a lot of work to examine a huge number of points, and also because the idea is extremely simple, such approaches are often referred to as brute force methods.

7.1.1 Brute Force Root Finding

Assume that we have a set of points along the curve of a continuous function f(x):

We want to solve f(x) = 0, i.e., find the points x where f crosses the x axis. A brute force algorithm is to run through all points on the curve and check if one point is below the x axis and if the next point is above the x axis, or the other way around. If this is found to be the case, we know that, when f is continuous, it has to cross the x axis at least once between these two x values. In other words, f is zero at least once on that sub-interval.

Note that, in the following algorithm, we refer to “the” root on a sub-interval, even if there may be more than one root in principle. Whether there are more than one root on a sub-interval will of course depend on the function, as well as on the size and location of the sub-interval. For simplicity, we will just assume there is at most one root on a sub-interval (or that it is sufficiently precise to talk about one root, even if there could be more).

Numerical Algorithm

More precisely, we have a set of n + 1 points (xi, yi), yi = f(xi), i = 0, …, n, where x0 < … < xn. We check if yi < 0 and yi+1 > 0 (or the other way around). A compact expression for this check is to perform the test yiyi+1 < 0. If so, the root of f(x) = 0 is in [xi, xi+1].

Assuming a linear variation of f between xi and xi+1, we have the approximation
$$\displaystyle \begin{aligned} f(x)\approx \frac{f(x_{i+1})-f(x_i)}{x_{i+1}-x_i}(x-x_i) + f(x_i) = \frac{y_{i+1}-y_i}{x_{i+1}-x_i}(x-x_i) + y_i, \end{aligned} $$
which, when set equal to zero, gives the root
$$\displaystyle \begin{aligned} x = x_i - \frac{x_{i+1}-x_i}{y_{i+1}-y_i}y_i\, .\end{aligned} $$


Given some Python implementation f( x) of our mathematical function, a straightforward implementation of the above algorithm that quits after finding one root, looks like

(See the file

Note the nice use of setting root to None: we can simply test if root is None to see if we found a root and overwrote the None value, or if we did not find any root among the tested points.

Running this program with some function, say \(f(x)=e^{-x^2}\cos {}(4x)\) (which has a solution at \(x = \frac {\pi }{8}\)), gives the root 0.39269910538048097, which has an error of 2.4 × 10−8. Increasing the number of points with a factor of ten gives a root with an error of 2.9 × 10−10.

After such a quick “flat” implementation of an algorithm, we should always try to offer the algorithm as a Python function, applicable to as wide a problem domain as possible. The function should take f and an associated interval [a, b] as input, as well as a number of points (n), and return a list of all the roots in [a, b]. Here is our candidate for a good implementation of the brute force root finding algorithm:

(See the file

This time we use another elegant technique to indicate if roots were found or not: roots is an empty list if the root finding was unsuccessful, otherwise it contains all the roots. Application of the function to the previous example can be coded as

Note that if roots evaluates to True if roots is non-empty. This is a general test in Python: if X evaluates to True if X is non-empty or has a nonzero value.

Running the program gives the output

7.1.2 Brute Force Optimization

Numerical Algorithm

We realize that xi corresponds to a maximum point if yi−1 < yi > yi+1. Similarly, xi corresponds to a minimum if yi−1 > yi < yi+1. We can do this test for all “inner” points i = 1, …, n − 1 to find all local minima and maxima. In addition, we need to add an end point, i = 0 or i = n, if the corresponding yi is a global maximum or minimum.


The algorithm above can be translated to the following Python function (file

The max and min functions are standard Python functions for finding the maximum and minimum element of a list or an object that one can iterate over with a for loop.

An application to \(f(x)=e^{-x^2}\cos {}(4x)\) looks like

Running the program gives

7.1.3 Model Problem for Algebraic Equations

We shall consider the very simple problem of finding the square root of 9. That is, we want to solve x2 = 9, but will (for simplicity) seek only the positive solution. Knowing the solution beforehand, allows us to easily investigate how the numerical method (and the implementation of it) performs in the search for a solution. The f(x) function that corresponds to the equation x2 = 9 is
$$\displaystyle \begin{aligned} f(x) = x^2 - 9\, .\end{aligned} $$
Our interval of interest for solutions will be [0, 1000] (the upper limit here is chosen somewhat arbitrarily).

In the following, we will present several efficient and accurate methods for solving nonlinear algebraic equations, both single equation and systems of equations. The methods all have in common that they search for approximate solutions. The methods differ, however, in the way they perform the search for solutions. The idea for the search influences the efficiency of the search and the reliability of actually finding a solution. For example, Newton’s method is very fast, but not reliable, while the bisection method is the slowest, but absolutely reliable. No method is best at all problems, so we need different methods for different problems.

What Is the Difference Between Linear and Nonlinear Equations?

You know how to solve linear equations ax + b = 0: x = −ba. All other types of equations f(x) = 0, i.e., when f(x) is not a linear function of x, are called nonlinear. A typical way of recognizing a nonlinear equation is to observe that x is “not alone” as in ax, but involved in a product with itself, such as in x3 + 2x2 − 9 = 0. We say that x3 and 2x2 are nonlinear terms. An equation like \(\sin x + e^x\cos x=0\) is also nonlinear although x is not explicitly multiplied by itself, but the Taylor series of \(\sin x\), ex, and \(\cos x\) all involve polynomials of x where x is multiplied by itself.

7.2 Newton’s Method

Newton’s method, also known as Newton-Raphson’s method, is a very famous and widely used method for solving nonlinear algebraic equations.1 Compared to the other methods presented in this chapter, i.e., secant and bisection, it is generally the fastest one (although computational speed rarely is an issue with a single equation on modern laptops). However, it does not guarantee that an existing solution will be found.

A fundamental idea of numerical methods for nonlinear equations is to construct a series of linear equations (since we know how to solve linear equations) and hope that the solutions of these linear equations bring us closer and closer to the solution of the nonlinear equation. The idea will be clearer when we present Newton’s method and the secant method.

7.2.1 Deriving and Implementing Newton’s Method

Figure 7.1 shows the f(x) function in our model equation x2 − 9 = 0. Numerical methods for algebraic equations require us to guess at a solution first. Here, this guess is called x0. The fundamental idea of Newton’s method is to approximate the original function f(x) by a straight line, i.e., a linear function, since it is straightforward to solve linear equations. There are infinitely many choices of how to approximate f(x) by a straight line. Newton’s method applies the tangent of f(x) at x0, see the rightmost tangent in Fig. 7.1. This linear tangent function crosses the x axis at a point we call x1. This is (hopefully) a better approximation to the solution of f(x) = 0 than x0. The next fundamental idea is to repeat this process. We find the tangent of f at x1, compute where it crosses the x axis, at a point called x2, and repeat the process again. Figure 7.1 shows that the process brings us closer and closer to the left. It remains, however, to see if we hit x = 3 or come sufficiently close to this solution.
Fig. 7.1

Illustrates the idea of Newton’s method with f(x) = x2 − 9, repeatedly solving for crossing of tangent lines with the x axis

How do we compute the tangent of a function f(x) at a point x0? The tangent function, here called \(\tilde f(x)\), is linear and has two properties:
  1. 1.

    the slope equals to f′(x0)

  2. 2.

    the tangent touches the f(x) curve at x0

So, if we write the tangent function as \(\tilde f(x)=ax+b\), we must require \(\tilde f'(x_0)=f'(x_0)\) and \(\tilde f(x_0)=f(x_0)\), resulting in
$$\displaystyle \begin{aligned} \tilde f(x) = f(x_0) + f'(x_0)(x - x_0)\, . \end{aligned} $$
The key step in Newton’s method is to find where the tangent crosses the x axis, which means solving \(\tilde f(x)=0\):
$$\displaystyle \begin{aligned} \tilde f(x)=0\quad \Rightarrow\quad x = x_0 - \frac{f(x_0)}{f'(x_0)} \, .\end{aligned} $$
This is our new candidate point, which we call x1:
$$\displaystyle \begin{aligned} x_1 = x_0 - \frac{f(x_0)}{f'(x_0)}\, . \end{aligned} $$
With x0 = 1000, we get x1 ≈ 500, which is in accordance with the graph in Fig. 7.1. Repeating the process, we get
$$\displaystyle \begin{aligned} x_2 = x_1 - \frac{f(x_1)}{f'(x_1)}\approx 250\, . \end{aligned} $$
The general scheme2 of Newton’s method may be written as
$$\displaystyle \begin{aligned} x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)},\quad n=0,1,2,\ldots {} \end{aligned} $$
The computation in (7.1) is repeated until \(f\left (x_n\right )\) is close enough to zero. More precisely, we test if |f(xn)| < 𝜖, with 𝜖 being a small number.

We moved from 1000 to 250 in two iterations, so it is exciting to see how fast we can approach the solution x = 3. A computer program can automate the calculations. Our first try at implementing Newton’s method is in a function naive_Newton (found in

The argument x is the starting value, called x0 in our previous mathematical description.

To solve the problem x2 = 9 we also need to implement

which in is included by use of an extra function and a test block.

Why Not Use an Array for the x Approximations?

Newton’s method is normally formulated with an iteration indexn,
$$\displaystyle \begin{aligned} x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)}\, .\end{aligned} $$
Seeing such an index, many would implement this as

Such an array is fine, but requires storage of all the approximations. In large industrial applications, where Newton’s method solves millions of equations at once, one cannot afford to store all the intermediate approximations in memory, so then it is important to understand that the algorithm in Newton’s method has no more need for xn when xn+1 is computed. Therefore, we can work with one variable x and overwrite the previous value:

Running naive_Newton(f, dfdx, 1000, eps=0.001) results in the approximate solution 3.000027639. A smaller value of eps will produce a more accurate solution. Unfortunately, the plain naive_Newton function does not return how many iterations it used, nor does it print out all the approximations x0, x1, x2, …, which would indeed be a nice feature. If we insert such a printout (print( x) in the while loop), a rerun results in

We clearly see that the iterations approach the solution quickly. This speed of the search for the solution is the primary strength of Newton’s method compared to other methods.

7.2.2 Making a More Efficient and Robust Implementation

The naive_Newton function works fine for the example we are considering here. However, for more general use, there are some pitfalls that should be fixed in an improved version of the code. An example may illustrate what the problem is.

Let us use naive_Newton to solve \(\tanh (x)=0\), which has solution x = 0 (interactively, you may define f(x) = tanh(x) and f′(x) = 1 − tanh2(x) as Python functions and import the naive_Newton function from With |x0|≤ 1.08 everything works fine. For example, x0 = 1.08 leads to six iterations if 𝜖 = 0.001:

Adjusting x0 slightly to 1.09 gives division by zero! The approximations computed by Newton’s method become

The division by zero is caused by x7 = −1.26055913647 × 1011, because \(\tanh (x_7)\) is 1.0 to machine precision, and then \(f'(x)=1 - \tanh (x)^2\) becomes zero in the denominator in Newton’s method.

The underlying problem, leading to the division by zero in the above example, is that Newton’s method diverges: the approximations move further and further away from x = 0. If it had not been for the division by zero, the condition in the while loop would always be true and the loop would run forever. Divergence of Newton’s method occasionally happens, and the remedy is to abort the method when a maximum number of iterations is reached.

Another disadvantage of the naive_Newton function is that it calls the f(x) function twice as many times as necessary. This extra work is of no concern when f(x) is fast to evaluate, but in large-scale industrial software, one call to f(x) might take hours or days, and then removing unnecessary calls is important. The solution in our function is to store the call f( x) in a variable (f_value) and reuse the value instead of making a new call f( x) .

To summarize, we want to write an improved function for implementing Newton’s method where we
  • handle division by zero properly

  • allow a maximum number of iterations

  • avoid the extra evaluation of f(x)

A more robust and efficient version of the function, inserted in a complete program ( for solving x2 − 9 = 0, is listed below.

Handling of the potential division by zero is done by a try-except construction.3

The division by zero will always be detected and the program will be stopped. The main purpose of our way of treating the division by zero is to give the user a more informative error message and stop the program in a gentler way.

Calling sys.exit with an argument different from zero (here 1) signifies that the program stopped because of an error. It is a good habit to supply the value 1, because tools in the operating system can then be used by other programs to detect that our program failed.

To prevent an infinite loop because of divergent iterations, we have introduced the integer variable iteration_counter to count the number of iterations in Newton’s method. With iteration_counter we can easily extend the condition in the while loop such that no more iterations take place when the number of iterations reaches 100. We could easily let this limit be an argument to the function rather than a fixed constant.

The Newton function returns the approximate solution and the number of iterations. The latter equals − 1 if the convergence criterion |f(x)| < 𝜖 was not reached within the maximum number of iterations. In the calling code, we print out the solution and the number of function calls. The main cost of a method for solving f(x) = 0 equations is usually the evaluation of f(x) and f′(x), so the total number of calls to these functions is an interesting measure of the computational work. Note that in function Newton there is an initial call to f(x) and then one call to f and one to f′ in each iteration.

Running, we get the following printout on the screen:

The Newton scheme will work better if the starting value is close to the solution. A good starting value may often make the difference as to whether the code actually finds a solution or not. Because of its speed (and when speed matters), Newton’s method is often the method of first choice for solving nonlinear algebraic equations, even if the scheme is not guaranteed to work. In cases where the initial guess may be far from the solution, a good strategy is to run a few iterations with the bisection method (see Sect. 7.4) to narrow down the region where f is close to zero and then switch to Newton’s method for fast convergence to the solution.

Using sympy to Find the Derivative

Newton’s method requires the analytical expression for the derivative f′(x). Derivation of f′(x) is not always a reliable process by hand if f(x) is a complicated function. However, Python has the symbolic package SymPy, which we may use to create the required dfdx function. With our sample problem, we get:

The nice feature of this code snippet is that dfdx_expr is the exact analytical expression for the derivative, 2*x (seen if you print it out). This is a symbolic expression, so we cannot do numerical computing with it. However, with lambdify, such symbolic expression are turned into callable Python functions, as seen here with f and dfdx.

The next method is the secant method, which is usually slower than Newton’s method, but it does not require an expression for f′(x), and it has only one function call per iteration.

7.3 The Secant Method

When finding the derivative f′(x) in Newton’s method is problematic, or when function evaluations take too long; we may adjust the method slightly. Instead of using tangent lines to the graph we may use secants.4 The approach is referred to as the secant method, and the idea is illustrated graphically in Fig. 7.2 for our example problem x2 − 9 = 0.
Fig. 7.2

Illustrates the use of secants in the secant method when solving x2 − 9 = 0, x ∈ [0, 1000]. From two chosen starting values, x0 = 1000 and x1 = 700 the crossing x2 of the corresponding secant with the x axis is computed, followed by a similar computation of x3 from x1 and x2

The idea of the secant method is to think as in Newton’s method, but instead of using f′(xn), we approximate this derivative by a finite difference or the secant, i.e., the slope of the straight line that goes through the points (xn, f(xn)) and (xn−1, f(xn−1)) on the graph, given by the two most recent approximations xn and xn−1. This slope reads
$$\displaystyle \begin{aligned} \frac{f(x_n)-f(x_{n-1})}{x_n - x_{n-1}}\, . {} \end{aligned} $$
Inserting this expression for f′(xn) in Newton’s method simply gives us the secant method:
$$\displaystyle \begin{aligned} x_{n+1} = x_n - \frac{f(x_n)}{\frac{f(x_n)-f(x_{n-1})}{x_n - x_{n-1}}},\end{aligned} $$
$$\displaystyle \begin{aligned} x_{n+1} = x_n - f(x_n) \frac{x_n - x_{n-1}}{f(x_n)-f(x_{n-1})} \, . {} \end{aligned} $$
Comparing (7.3) to the graph in Fig. 7.2, we see how two chosen starting points (x0 = 1000, x1 = 700, and corresponding function values) are used to compute x2. Once we have x2, we similarly use x1 and x2 to compute x3. As with Newton’s method, the procedure is repeated until f(xn) is below some chosen limit value, or some limit on the number of iterations has been reached. We use an iteration counter here too, based on the same thinking as in the implementation of Newton’s method.

We can store the approximations xn in an array, but as in Newton’s method, we notice that the computation of xn+1 only needs knowledge of xn and xn−1, not “older” approximations. Therefore, we can make use of only three variables: x for xn+1, x1 for xn, and x0 for xn−1. Note that x0 and x1 must be given (guessed) for the algorithm to start.

A program that solves our example problem may be written as:

The number of function calls is now related to no_iterations, i.e., the number of iterations, as 2 + no_iterations, since we need two function calls before entering the while loop, and then one function call per loop iteration. Note that, even though we need two points on the graph to compute each updated estimate, only a single function call (f( x1) ) is required in each iteration since f( x0) becomes the “old” f( x1) and may simply be copied as f_x0 = f_x1 (the exception is the very first iteration where two function evaluations are needed).

Running, gives the following printout on the screen:

7.4 The Bisection Method

Neither Newton’s method nor the secant method can guarantee that an existing solution will be found (see Exercises 7.1 and 7.2). The bisection method, however, does that. However, if there are several solutions present, it finds only one of them, just as Newton’s method and the secant method. The bisection method is slower than the other two methods, so reliability comes with a cost of speed (but, again, for a single equation that is rarely an issue with laptops of today).

To solve x2 − 9 = 0, \(x \in \left [0, 1000\right ]\), with the bisection method, we reason as follows. The first key idea is that if f(x) = x2 − 9 is continuous on the interval and the function values for the interval endpoints (xL = 0, xR = 1000) have opposite signs, f(x) must cross the x axis at least once on the interval. That is, we know there is at least one solution.

The second key idea comes from dividing the interval in two equal parts, one to the left and one to the right of the midpoint xM = 500. By evaluating the sign of f(xM), we will immediately know whether a solution must exist to the left or right of xM. This is so, since if f(xM) ≥ 0, we know that f(x) has to cross the x axis between xL and xM at least once (using the same argument as for the original interval). Likewise, if instead f(xM) ≤ 0, we know that f(x) has to cross the x axis between xM and xR at least once.

In any case, we may proceed with half the interval only. The exception is if f(xM) ≈ 0, in which case a solution is found. Such interval halving can be continued until a solution is found. A “solution” in this case, is when |f(xM)| is sufficiently close to zero, more precisely (as before): |f(xM)| < 𝜖, where 𝜖 is a small number specified by the user.

The sketched strategy seems reasonable, so let us write a reusable function that can solve a general algebraic equation f(x) = 0 (

Note that we first check if f changes sign in [a, b], because that is a requirement for the algorithm to work. The algorithm also relies on a continuous f(x) function, but this is very challenging for a computer code to check.

We get the following printout to the screen when is run:

We notice that the number of function calls is much higher than with the previous methods.

Required Work in the Bisection Method

If the starting interval of the bisection method is bounded by a and b, and the solution at step n is taken to be the middle value, the error is bounded as
$$\displaystyle \begin{aligned} \frac{|b-a|}{2^n}, {} \end{aligned} $$
because the initial interval has been halved n times. Therefore, to meet a tolerance 𝜖, we need n iterations such that the length of the current interval equals 𝜖:
$$\displaystyle \begin{aligned} \frac{|b-a|}{2^n}=\epsilon\quad \Rightarrow\quad n = \frac{\ln ((b-a)/\epsilon)}{\ln 2}\, . \end{aligned} $$
This is a great advantage of the bisection method: we know beforehand how many iterations n it takes to meet a certain accuracy 𝜖 in the solution.

7.5 Rate of Convergence

With the methods above, we noticed that the number of iterations or function calls could differ quite substantially. The number of iterations needed to find a solution is closely related to the rate of convergence, which dictates the speed of error reduction as we approach the root. More precisely, we introduce the error in iteration n as en = |x − xn|, and define the convergence rateq as
$$\displaystyle \begin{aligned} e_{n+1} = Ce_n^q, {} \end{aligned} $$
where C is a constant. The exponent q measures how fast the error is reduced from one iteration to the next. The larger q is, the faster the error goes to zero (when en < 1), and the fewer iterations we need to meet the stopping criterion |f(x)| < 𝜖.

Convergence Rate and Iterations

When we previously addressed numerical integration (Chap.  6), the approximation error E was related to the size h of the sub-intervals and the convergence rate r as E = Khr, K being some constant.

Observe that (7.5) gives a different definition of convergence rate. This makes sense, since numerical integration is based on a partitioning of the original integration interval into n sub-intervals, which is very different from the iterative procedures used here for solving nonlinear algebraic equations.

A single q in (7.5) is defined in the limit n →. For finite n, and especially smaller n, q will vary with n. To estimate q, we can compute all the errors en and set up (7.5) for three consecutive experiments n − 1, n, and n + 1:
$$\displaystyle \begin{aligned} e_{n} &= Ce_{n-1}^q,\\ e_{n+1} &= Ce_n^q\, . \end{aligned} $$
Dividing, e.g., the latter equation by the former, and solving with respect to q, we get that
$$\displaystyle \begin{aligned} q = \frac{\ln (e_{n+1}/e_n)}{\ln(e_n/e_{n-1})}\, .\end{aligned} $$
Since this q will vary somewhat with n, we call it qn. As n grows, we expect qn to approach a limit (qn → q).

Modifying Our Functions to Return All Approximations

To compute all the qn values, we need all the xn approximations. However, our previous implementations of Newton’s method, the secant method, and the bisection method returned just the final approximation.

Therefore, we have modified our solvers5 accordingly, and placed them in A user can choose whether the final value or the whole history of solutions is to be returned. Each of the extended implementations now takes an extra parameter return_x_list. This parameter is a boolean, set to True if the function is supposed to return all the root approximations, or False, if the function should only return the final approximation.

As an example, let us take a closer look at Newton:

We can now make a call

and get a list x returned. With knowledge of the exact solution x of f(x) = 0 we can compute all the errors en and all the associated qn values with the compact function (also found in

The error model (7.5) works well for Newton’s method and the secant method. For the bisection method, however, it works well in the beginning, but not when the solution is approached.

We can compute the rates qn and print them nicely (,

The result for print_rates('Newton', x, 3) is

indicating that q = 2 is the rate for Newton’s method. A similar computation using the secant method, gives the rates

Here it seems that q ≈ 1.6 is the limit.


If we in the bisection method think of the length of the current interval containing the solution as the error en, then (7.5) works perfectly since \(e_{n+1}=\frac {1}{2}e_n\), i.e., q = 1 and \(C=\frac {1}{2}\), but if en is the true error |x − xn|, it is easily seen from a sketch that this error can oscillate between the current interval length and a potentially very small value as we approach the exact solution. The corresponding rates qn fluctuate widely and are of no interest.

7.6 Solving Multiple Nonlinear Algebraic Equations

So far in this chapter, we have considered a single nonlinear algebraic equation. However, systems of such equations arise in a number of applications, foremost nonlinear ordinary and partial differential equations. Of the previous algorithms, only Newton’s method is suitable for extension to systems of nonlinear equations.

7.6.1 Abstract Notation

Suppose we have n nonlinear equations, written in the following abstract form:
$$\displaystyle \begin{aligned} F_0(x_0,x_1,\ldots,x_n) &= 0, \end{aligned} $$
$$\displaystyle \begin{aligned} F_1(x_0,x_1,\ldots,x_n) &= 0, \end{aligned} $$
$$\displaystyle \begin{aligned} \vdots &= \vdots \end{aligned} $$
$$\displaystyle \begin{aligned} F_n(x_0,x_1,\ldots,x_n) &= 0\, . \end{aligned} $$
It will be convenient to introduce a vector notation
$$\displaystyle \begin{aligned} \boldsymbol{F} = (F_0,\ldots,F_1),\quad \boldsymbol{x} = (x_0,\ldots,x_n)\, .\end{aligned} $$
The system can now be written as F(x) = 0.
As a specific example on the notation above, the system
$$\displaystyle \begin{aligned} x^2 &= y - x\cos{}(\pi x) {} \end{aligned} $$
$$\displaystyle \begin{aligned} yx + e^{-y} &= x^{-1} {} \end{aligned} $$
can be written in our abstract form by introducing x0 = x and x1 = y. Then
$$\displaystyle \begin{aligned} F_0(x_0,x_1) &= x^2 - y + x\cos{}(\pi x) = 0,\\ F_1(x_0,x_1) &= yx + e^{-y} - x^{-1} = 0\, . \end{aligned} $$

7.6.2 Taylor Expansions for Multi-Variable Functions

We follow the ideas of Newton’s method for one equation in one variable: approximate the nonlinear f by a linear function and find the root of that function. When n variables are involved, we need to approximate a vector functionF(x) by some linear function \(\tilde {\boldsymbol {F}} = \boldsymbol {J}\boldsymbol {x} + \boldsymbol {c}\), where J is an n × n matrix and c is some vector of length n.

The technique for approximating F by a linear function is to use the first two terms in a Taylor series expansion. Given the value of F and its partial derivatives with respect to x at some point xi, we can approximate the value at some point xi+1 by the two first term in a Taylor series expansion around xi:
$$\displaystyle \begin{aligned} \boldsymbol{F}(\boldsymbol{x}_{i+1}) \approx \boldsymbol{F}(\boldsymbol{x}_i) + \nabla\boldsymbol{F}(\boldsymbol{x}_i)(\boldsymbol{x}_{i+1}-\boldsymbol{x}_i)\, .\end{aligned} $$
The next terms in the expansions are omitted here and of size ||xi+1 −xi||2, which are assumed to be small compared with the two terms above.
The expression ∇F is the matrix of all the partial derivatives of F. Component (i, j) in ∇F is
$$\displaystyle \begin{aligned} \frac{\partial F_i}{\partial x_j}\, .\end{aligned} $$
For example, in our 2 × 2 system (7.10) and (7.11) we can use SymPy to compute the Jacobian:
We can then write
$$\displaystyle \begin{aligned} \nabla\boldsymbol{F} = \left(\begin{array}{ll} \frac{\partial F_0}{\partial x_0} & \frac{\partial F_0}{\partial x_1}\\ \frac{\partial F_1}{\partial x_0} & \frac{\partial F_1}{\partial x_1} \end{array}\right) = \left(\begin{array}{ll} 2x_0 + \cos{}(\pi x_0) - \pi x_0\sin{}(\pi x_0) & -1 \\ x_1 + x_0^{-2} & x_0 - e^{-x_1} \end{array}\right) \end{aligned} $$

The matrix ∇F is called the Jacobian of F and often denoted by J.

7.6.3 Newton’s Method

The idea of Newton’s method is that we have some approximation xi to the root and seek a new (and hopefully better) approximation xi+1 by approximating F(xi+1) by a linear function and solve the corresponding linear system of algebraic equations. We approximate the nonlinear problem F(xi+1) = 0 by the linear problem
$$\displaystyle \begin{aligned} \boldsymbol{F}(\boldsymbol{x}_i) + \boldsymbol{J}(\boldsymbol{x}_i)(\boldsymbol{x}_{i+1}-\boldsymbol{x}_i) = \boldsymbol{0}, {} \end{aligned} $$
where J(xi) is just another notation for ∇F(xi). The Eq. (7.12) is a linear system with coefficient matrix J and right-hand side vector F(xi). We therefore write this system in the more familiar form
$$\displaystyle \begin{aligned} \boldsymbol{J}(\boldsymbol{x}_i)\boldsymbol{\delta} = -\boldsymbol{F}(\boldsymbol{x}_i),\end{aligned} $$
where we have introduced a symbol δ for the unknown vector xi+1 −xi that multiplies the Jacobian J.
The i-th iteration of Newton’s method for systems of algebraic equations consists of two steps:
  1. 1.

    Solve the linear system J(xi)δ = −F(xi) with respect to δ.

  2. 2.

    Set xi+1 = xi + δ.

Solving systems of linear equations must make use of appropriate software. Gaussian elimination is the most common, and in general the most robust, method for this purpose. Python’s numpy package has a module linalg that interfaces the well-known LAPACK package with high-quality and very well tested subroutines for linear algebra. The statement x = numpy.linalg.solve( A, b) solves a system Ax = b with a LAPACK method based on Gaussian elimination.

When nonlinear systems of algebraic equations arise from discretization of partial differential equations, the Jacobian is very often sparse, i.e., most of its elements are zero. In such cases it is important to use algorithms that can take advantage of the many zeros. Gaussian elimination is then a slow method, and (much) faster methods are based on iterative techniques.

7.6.4 Implementation

Here is a very simple implementation of Newton’s method for systems of nonlinear algebraic equations:

We can test the function Newton_system with the 2 × 2 system (7.10) and (7.11):

Here, the testing is based on the L2 norm6 of the error vector. Alternatively, we could test against the values of x that the algorithm finds, with appropriate tolerances. For example, as chosen for the error norm, if eps=0.0001, a tolerance of 10−4 can be used for x[0] and x[1].

7.7 Exercises

Exercise 7.1: Understand Why Newton’s Method Can Fail

The purpose of this exercise is to understand when Newton’s method works and fails. To this end, solve \(\tanh x=0\) by Newton’s method and study the intermediate details of the algorithm. Start with x0 = 1.08. Plot the tangent in each iteration of Newton’s method. Then repeat the calculations and the plotting when x0 = 1.09. Explain what you observe.

Filename: Newton_failure.∗.

Exercise 7.2: See If the Secant Method Fails

Does the secant method behave better than Newton’s method in the problem described in Exercise 7.1? Try the initial guesses
  1. 1.

    x0 = 1.08 and x1 = 1.09

  2. 2.

    x0 = 1.09 and x1 = 1.1

  3. 3.

    x0 = 1 and x1 = 2.3

  4. 4.

    x0 = 1 and x1 = 2.4

Filename: secant_failure.∗.

Exercise 7.3: Understand Why the Bisection Method Cannot Fail

Solve the same problem as in Exercise 7.1, using the bisection method, but let the initial interval be [−5, 3]. Report how the interval containing the solution evolves during the iterations.

Filename: bisection_nonfailure.∗.

Exercise 7.4: Combine the Bisection Method with Newton’s Method

An attractive idea is to combine the reliability of the bisection method with the speed of Newton’s method. Such a combination is implemented by running the bisection method until we have a narrow interval, and then switch to Newton’s method for speed.

Write a function that implements this idea. Start with an interval [a, b] and switch to Newton’s method when the current interval in the bisection method is a fraction s of the initial interval (i.e., when the interval has length s(b − a)). Potential divergence of Newton’s method is still an issue, so if the approximate root jumps out of the narrowed interval (where the solution is known to lie), one can switch back to the bisection method. The value of s must be given as an argument to the function, but it may have a default value of 0.1.

Try the new method on \(\tanh (x)=0\) with an initial interval [−10, 15].


Exercise 7.5: Write a Test Function for Newton’s Method

The purpose of this function is to verify the implementation of Newton’s method in the Newton function in the file Construct an algebraic equation and perform two iterations of Newton’s method by hand or with the aid of SymPy. Find the corresponding size of |f(x)| and use this as value for eps when calling Newton. The function should then also perform two iterations and return the same approximation to the root as you calculated manually. Implement this idea for a unit test as a test function test_Newton().


Exercise 7.6: Halley’s Method and the Decimal Module

A nonlinear algebraic equation f(x) = 0 may also be solved by Halley’s method,7 given as:
$$\displaystyle \begin{aligned} x_{n+1} = x_n - \frac{2f(x_n)f'(x_n)}{2f'(x_n)^2 - f(x_n)f''(x_n)},\quad n=0,1,\ldots\, ,\end{aligned} $$
with some starting value x0.
  1. a)

    Implement Halley’s method as a function Halley. Place the function in a module that has a test block, and test the function by solving x2 − 9 = 0, using x0 = 1000 as your initial guess.

  2. b)

    Compared to Newton’s method, more computations per iteration are needed with Halley’s method, but a convergence rate of 3 may be achieved close to the root. You are now supposed to extend your module with a function compute_rates_decimal, which computes the convergence rates achieved with your implementation of Halley (for the given problem).

    The implementation of compute_rates_decimal should involve the decimal module (you search for the right documentation!), to better handle very small errors that may enter the rate computations. For comparison, you should also compute the rates without using the decimal module. Test and compare with several parameter values.



The logarithms in the rate calculation might require some extra consideration when you use the decimal module.


Exercise 7.7: Fixed Point Iteration

A nonlinear algebraic equation f(x) = 0 may be solved in many different ways, and we have met some of these in this chapter. Another, very useful, solution approach is to first re-write the equation into x = ϕ(x) (this re-write is not unique), and then formulate the iteration
$$\displaystyle \begin{aligned} x_{n+1} = \phi(x_n),\quad n=0,1,\ldots\, ,\end{aligned} $$
with some starting value x0. If ϕ(x) is continuous, and if ϕ(xn) approaches α as xn approaches α (i.e., we get α = ϕ(α) as n →), the iteration is called a fixed point iteration and α is referred to as a fixed point of the mapping x → ϕ(x). Clearly, if a fixed point α is found, α will also be a solution to the original equation f(x) = 0.
In this exercise, we will briefly explore the fixed point iteration method by solving
$$\displaystyle \begin{aligned} x^3 + 2x = e^{-x},\quad x \in \left[-2, 2\right]\, .\end{aligned} $$

For comparison, however, you will first be asked to solve the equation by Newton’s method (which, in fact, can be seen as fixed point iteration8).

  1. a)

    Write a program that solves this equation by Newton’s method. Use x = 1 as your starting value. To better judge the printed answer found by Newton’s method, let the code also plot the relevant function on the given interval.

  2. b)

    The given equation may be rewritten as \(x = \frac {e^{-x} - x^3}{2}\). Extend your program with a function fixed_point_iteration, which takes appropriate parameters, and uses fixed point iteration to find and return a solution (if found), as well as the number of iterations required. Use x = 1 as starting value.

    When the program is executed, the original equation should be solved both with Newton’s method and via fixed point iterations (as just described). Compare the output from the two methods.


Exercise 7.8: Solve Nonlinear Equation for a Vibrating Beam

An important engineering problem that arises in a lot of applications is the vibrations of a clamped beam where the other end is free. This problem can be analyzed analytically, but the calculations boil down to solving the following nonlinear algebraic equation:
$$\displaystyle \begin{aligned} \cosh\beta \cos\beta = -1,\end{aligned} $$
where β is related to important beam parameters through
$$\displaystyle \begin{aligned} \beta^4 = \omega^2 \frac{\varrho A}{EI},\end{aligned} $$
where ϱ is the density of the beam, A is the area of the cross section, E is Young’s modulus, and I is the moment of the inertia of the cross section. The most important parameter of interest is ω, which is the frequency of the beam. We want to compute the frequencies of a vibrating steel beam with a rectangular cross section having width b = 25 mm and height h = 8 mm. The density of steel is 7850 kg/m3, and E = 2 × 1011 Pa. The moment of inertia of a rectangular cross section is I = bh3∕12.
  1. a)

    Plot the equation to be solved so that one can inspect where the zero crossings occur.



When writing the equation as f(β) = 0, the f function increases its amplitude dramatically with β. It is therefore wise to look at an equation with damped amplitude, g(β) = eβf(β) = 0. Plot g instead.

  1. b)

    Compute the first three frequencies.



  1. 1.

    Read more about Newton’s method, e.g., on

  2. 2.

    The term scheme is often used as a synonym for method or computational recipe.

  3. 3.

    Professional programmers would avoid calling sys.exit inside a function. Instead, they would raise a new exception with an informative error message, and let the calling code have another try-except construction to stop the program.

  4. 4.
  5. 5.

    An implemented numerical solution algorithm is often called a solver.

  6. 6.
  7. 7.
  8. 8.

Copyright information

© The Authors 2020

Open Access This chapter is licensed under the terms of the Creative Commons Attribution 4.0 International License (, which permits use, sharing, adaptation, distribution and reproduction in any medium or format, as long as you give appropriate credit to the original author(s) and the source, provide a link to the Creative Commons license and indicate if changes were made.

The images or other third party material in this chapter are included in the chapter's Creative Commons license, unless indicated otherwise in a credit line to the material. If material is not included in the chapter's Creative Commons license and your intended use is not permitted by statutory regulation or exceeds the permitted use, you will need to obtain permission directly from the copyright holder.

Authors and Affiliations

  • Svein Linge
    • 1
  • Hans Petter Langtangen
    • 2
  1. 1.Fac of Tech, Natural Sci & Maritime SciUniversity of South-Eastern NorwayPorsgrunnNorway
  2. 2.Simula Research Laboratory BioCompLysakerNorway

Personalised recommendations