Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

10 - Newton’s Method Example Exercise

Non-linear solution with Newton’s method

Newton’s method is a numerical method for solving non-linear equations. It is an iterative method that uses the derivative of the function to find the root. The derivative of the function is used to find the tangent line to the function at the current point. The tangent line is then used to find the next point where the tangent line crosses the x-axis. This process is repeated until the solution is found.

Derivation of Newton’s method

First use the Taylor series expansion of the function f(x)f(x) about the point x0x_0:

f(x)=f(x0)+f′(x0)(x−x0)+f′′(x0)2!(x−x0)2+f′′′(x0)3!(x−x0)3+⋯f(x) = f(x_0) + f'(x_0)(x-x_0) + \frac{f''(x_0)}{2!}(x-x_0)^2 + \frac{f'''(x_0)}{3!}(x-x_0)^3 + \cdots

where f′f' is the 1st derivative of x and f′′f'' is the second derivative and so on. If we neglect the higher order terms, we can write:

f(x)≈f(x0)+f′(x0)(x−x0)f(x) \approx f(x_0) + f'(x_0)(x-x_0)

If we set f(x)=0f(x) = 0 and solve for xx, we get:

x=x0−f(x0)f′(x0)x = x_0 - \frac{f(x_0)}{f'(x_0)}

This is the Newton’s method equation for finding the root of a function. x0x_0 is the initial guess and xx is the new guess. Iterations are completed until the value of x is close enough to zero. The derivative of the function is used as an estimate for the next guess according to the above. This is a first order approximation as a the first derivative is used. There are other numerical methods for root finding.

<Figure size 640x480 with 1 Axes>

We need to find the roots of the above equation.

-5.25
-3.1011904761904763
-2.356691551112544
-2.2391703436571913
-2.2360698972755695
-2.236067978533095
-2.23606797749979

Lets plot our progression from our initial guess of x0=−10x_0=-10

<Figure size 640x480 with 1 Axes>

Now lets try a more complex equation (still everywhere differentiable)

(-100.0, 300.0)
<Figure size 640x480 with 1 Axes>
4.870887130362349
4.424011563368115
4.106934926714884
3.836682141049953
1.9062611179657791
4.752313888527454
4.339199843776698

Let’s plot our points again:

(-100.0, 300.0)
<Figure size 640x480 with 1 Axes>

You can see that a proper guess value is important in finding the root with derivative based root finding methods.

Instead of x’s and y’s, lets us an application example: the Colbrook equation.

(-10.0, 10.0)
<Figure size 640x480 with 1 Axes>

Now lets setup our Newton’s method to find the friction factor at those conditions.

There are several ways to get that Colbrook_prime function in the above code snippet. It’s the derivative with respect to the friction factor of the Colbrook equation. You can do it by hand and then type in the equation. You could also use the symbolic derivative function in sympy to get the derivative. Or you can use the derivative function in scipy. Several methods are shown below.

Loading...
-0.09022367308759105
/var/folders/6d/1jr2w1qx1rnd2nkndlq4hc700000gn/T/ipykernel_57416/3040087786.py:2: RuntimeWarning: invalid value encountered in sqrt
  return 1/np.sqrt(f) + 2*np.log10(2.51/(Rey*np.sqrt(f))+ep/(3.71*Dia))
/var/folders/6d/1jr2w1qx1rnd2nkndlq4hc700000gn/T/ipykernel_57416/837614916.py:2: RuntimeWarning: invalid value encountered in scalar divide
  return -1/(2*f**(3/2)) - 2.51/(np.log(10)*Re*f**(3/2)*(0.2695*epsil/D+2.51/(Re*np.sqrt(f))))
(nan+nanj)
0.05585303773902183
0.06563953943684912
0.0670681189201254
0.06709175234208192
0.06709175772521607

Isn’t there a better way of doing this than having to type this in over and over. Let’s modify the Newton function to do the interation for us.

<Figure size 640x480 with 1 Axes>
-9.349525358359756e-07
0.06709410233779416

Check answer against fsolve

array([0.06709176])

Below is another example of using Newton’s method to solve a non-linear system of equations where the derivatives of each equation with respect to each variable are known and defined in the Jacobian matrix. A more in-depth class on numerical mehods would explain more.

Solution:[1.76746906 0.87605312]