The Newton Raphson method is a fast way to solve equations that cannot be rearranged by hand, and it is one of the most useful numerical tools for engineers. In this guide you will learn the Newton Raphson method in three easy steps, write it in Python, and use it on a real pipe-flow problem: finding the friction factor from the Colebrook equation. The full code is below, and you can run it as it is. For the theory behind it, see Newton’s method.

What the Newton Raphson Method Does
Many engineering equations are implicit. The friction factor in a pipe, the roots of a polynomial, the temperature at which two heat flows balance and the angle in a mechanism all appear on both sides of an equation. The Newton Raphson method solves such an equation by starting from a guess and improving it again and again. At each step it draws a tangent to the curve and moves to the point where the tangent crosses the axis. That point is a better guess, as the diagram above shows.
3 Easy Steps of the Newton Raphson Method
- Write the equation as f(x) = 0 and find its derivative f'(x).
- Pick a starting guess x0 close to the answer, using physical sense or a quick plot.
- Repeat x(n+1) = x(n) – f(x(n)) / f'(x(n)) until the change between two steps is smaller than your tolerance.
When the guess is good, the number of correct digits roughly doubles with each step. That is why the Newton Raphson method usually needs only three to six iterations.
Newton Raphson Method in Python: Basic Code
This short function works for any equation. You give it f, its derivative, a starting guess and a tolerance.
import math
def newton_raphson(f, df, x0, tol=1e-8, max_iter=20):
x = x0
for i in range(1, max_iter + 1):
x_new = x - f(x) / df(x)
print(f"iter {i}: x = {x_new:.8f}, error = {abs(x_new - x):.2e}")
if abs(x_new - x) < tol:
return x_new
x = x_new
raise RuntimeError("No convergence: try another starting guess")Run it for a simple test, such as finding the square root of 10 with f(x) = x*x – 10 and f'(x) = 2*x, starting at 3. The answer 3.16227766 appears after four iterations. Our NumPy tutorial shows how to extend such functions to arrays.
Engineering Example: Friction Factor with the Newton Raphson Method
In pipe flow, the Darcy friction factor f depends on the Reynolds number and on the pipe roughness through the Colebrook equation. The equation has f on both sides, so it needs an iterative solution. Take water in a pipe with Reynolds number 100,000 and relative roughness 0.0005. First check the flow regime with the Reynolds number formula or the Reynolds number calculator. The result is turbulent, so Colebrook applies.
Let x = 1/√f. The equation becomes g(x) = x + 2·log10(ε/(3.7D) + 2.51·x/Re) = 0, and its derivative is easy to write. This is the code.
import math
def colebrook_newton(Re, rel_rough, tol=1e-8, max_iter=20):
# unknown x = 1/sqrt(f); g(x) = x + 2*log10(rel_rough/3.7 + 2.51*x/Re)
x = 1 / math.sqrt(0.02) # initial guess f = 0.02
for i in range(1, max_iter + 1):
arg = rel_rough / 3.7 + 2.51 * x / Re
g = x + 2 * math.log10(arg)
dg = 1 + 2 * (2.51 / Re) / (arg * math.log(10))
x_new = x - g / dg
print(f"iter {i}: f = {1 / x_new**2:.6f}, error = {abs(x_new - x):.2e}")
if abs(x_new - x) < tol:
return 1 / x_new**2
x = x_new
f = colebrook_newton(Re=1e5, rel_rough=0.0005)
print("friction factor f =", round(f, 5))Output:
iter 1: f = 0.020327, error = 5.71e-02
iter 2: f = 0.020327, error = 8.56e-06
iter 3: f = 0.020327, error = 1.94e-13
friction factor f = 0.02033The method converges in three iterations to f = 0.02033. A quick check with the Swamee-Jain explicit formula gives 0.02041, which is within 0.4 percent, so the result is reliable.
Using the result for pressure drop
For a pipe of diameter 0.05 m and length 20 m carrying water (density 998 kg/m³, viscosity 1.0 mPa·s) at Re = 100,000, the velocity is about 2.0 m/s. The pressure drop is f·(L/D)·ρ·v²/2, which gives about 16.3 kPa. You can combine this with the Bernoulli equation to size a pump for the line.
Newton Raphson Method vs Other Root-Finding Methods
| Method | Needs derivative | Speed | Safety |
|---|---|---|---|
| Newton Raphson | Yes | Very fast (quadratic) | Can fail if the guess is poor |
| Bisection | No | Slow but steady | Always converges if the root is bracketed |
| Secant | No | Fast | Can fail like Newton Raphson |
| SciPy fsolve or brentq | No | Fast | Robust, ready-made functions |
Use the Newton Raphson method when you can write the derivative and have a good guess. Use bisection when you only need a safe answer and speed does not matter. In real projects, many engineers check their own code against SciPy functions.
Common Mistakes with the Newton Raphson Method
- Starting too far from the root. The iteration may jump to another root or diverge.
- Derivative equal to zero. Division by zero stops the method, so check f'(x) before dividing.
- No iteration limit. Always set a maximum number of iterations.
- Wrong derivative. Test it numerically with a small step before you trust it.
- Ignoring units. Keep all quantities in SI units in the function.
Where Students Usually Get Stuck
The method is simple, but applying it to your own problem is harder: writing the equation in the right form, finding a good starting guess and checking that the answer is physical. These depend on your topic, such as a heat exchanger, a cam profile or a thermodynamic cycle. If you send us your equation and your Python attempt, we can review it with you, explain what went wrong and teach you the next step, so you finish the work yourself. Message us on WhatsApp or see our Python for mechanical engineers page.
A note on the code in this guide. It is a small learning demo that shows how the method works, using generated data. For a full project with your own data, complete analysis, code and report, we offer paid project support, where we plan the work with you and explain every step. Contact us through the Python for mechanical engineers page to discuss your topic and the cost.
Frequently Asked Questions
What is the Newton Raphson method used for in engineering?
It solves nonlinear equations, such as friction factor in pipes, equilibrium temperatures, mechanism angles and implicit design equations, where a direct formula does not exist.
How many iterations does the Newton Raphson method need?
With a good starting guess, three to six iterations are usually enough for engineering accuracy, because the error shrinks very quickly.
Why does the Newton Raphson method fail sometimes?
It fails when the starting guess is far from the root, when the derivative is zero or very small, or when the function has several roots close together. Plot the function first and add an iteration limit.
Can I use SciPy instead of writing my own code?
Yes. SciPy offers fsolve, newton and brentq. Writing the method yourself once helps you understand the result and explain it in a viva.
Is Newton Raphson better than bisection?
It is faster, but bisection is safer. Many engineers use bisection to get close and Newton Raphson to finish.
Related guides: beam deflection in Python, heat exchanger design in Python, plot CFD and FEA results with Python and types of valves.
Interested in this project? Get the details, files and guidance from our team.
Enquire on WhatsApp