Skip to main content

Featured Post

Why I Wrote The Sheet Mechanic (And Why Calculations Aren’t Enough)

For engineers who already know the math—but still lose projects. For the last few years, I’ve been sharing technical guides here on Mechanical Design Handbook —how to size a motor, how to calculate fits, and (as you recently read) how to choose between timing belts and ball screws. But after 25 years in industrial automation, I realized something uncomfortable: Projects rarely fail because the math was wrong. They fail because: The client changed the scope three times in one week. A critical vendor lied about a shipping date (and no one verified it). The installation technician couldn’t fit a wrench into the gap we designed. University taught us the physics. It didn’t teach us the reality. That gap is why I wrote my new book, The Sheet Mechanic . This is not a textbook. It is a field manual for the messy, political, and chaotic space between the CAD model and the factory floor. It captures the systems I’ve used to survive industrial projec...
NEW RELEASE: Stop trying to be a Hero. Start being a Mechanic. Get "The Sheet Mechanic" on Amazon »
Disclosure: As an Amazon Associate, I earn from qualifying purchases.

Numerical Methods - The Newton-Raphson Method (Part 2)

In the previous post, we talked about several root finding techniques. In this post, we're going to see how we can use Microsoft Excel VBA to find the roots using the Newton-Raphson Method.

Advertisement
Abstract visualization of Newton-Raphson algorithm convergence
Figure 1: The Newton-Raphson method rapidly converges on a solution using the function's derivative.

The Logic

As we know, the Newton-Raphson Method is the most widely used of all root-locating formulas. It uses the slope (first derivative) of the function to find the root. That means, in the VBA code, we have to calculate the first derivative of the function.

We already discussed how to find the first derivative using numerical methods, and we will incorporate that logic here.

Graph showing the tangent line projection of the Newton-Raphson method
Figure 2: Geometric interpretation: The tangent line points to the next approximate root.
The Iterative Formula:

The method uses the Taylor series to approximate the next position (xN) based on the current position (x0):

xN = x0 - f(x0) / f'(x0)

The Algorithm

  1. Guess the initial value of the root → select x0.
  2. Calculate the next guess (xN) using the formula above.
  3. Check convergence: Is the error abs(xN - x0) within the allowable tolerance?
  4. If yes, stop. If no, set x0 = xN and repeat.
Advertisement

The VBA Code

Copy and paste the following code into a Module in your Excel VBA Editor (Alt+F11). This code includes the function to solve, the numerical derivative calculator, and the Newton-Raphson loop.

' ================================================
' Created by Suparerg Suksai
' Mechanical Design Handbook
' The Newton-Raphson Method - Numerical Methods
' ================================================

' 1. Define the function you want to solve here
' Example: e^(-x) - x = 0
Function f(x As Double) As Double
    f = Exp(-x) - x
End Function

' 2. Calculate Numerical Derivative (Central Difference)
Function fDeriv(x As Double) As Double
    Dim h As Double
    h = 0.000001 ' Small step size
    fDeriv = (f(x + h) - f(x - h)) / (2 * h)
End Function

' 3. Main Newton-Raphson Algorithm
Function RootNewtonRaphson(x0 As Double) As Double
    Dim x_new As Double
    Dim x_old As Double
    Dim tolerance As Double
    Dim error_val As Double
    Dim max_iter As Integer
    Dim i As Integer
    
    x_old = x0
    tolerance = 0.000001
    max_iter = 100 ' Prevent infinite loops
    
    For i = 1 To max_iter
        ' Apply formula: x_new = x_old - f(x)/f'(x)
        x_new = x_old - (f(x_old) / fDeriv(x_old))
        
        ' Check if we found the root
        error_val = Abs(x_new - x_old)
        If error_val < tolerance Then
            RootNewtonRaphson = x_new
            Exit Function
        End If
        
        ' Update for next iteration
        x_old = x_new
    Next i
    
    RootNewtonRaphson = x_new
End Function
Screenshot of the VBA code in the Excel Editor window
Figure 3: Paste the code into a new Module in the VBA Editor (Alt+F11).

How to Use It

To calculate the root of e-x - x = 0:

  1. Go to any cell in your Excel spreadsheet.
  2. Type =RootNewtonRaphson(1). (Here, 1 is your initial guess).
  3. The program will calculate and return the result: 0.567143290409784.

Verification

We can recheck the calculation result by plugging the answer back into the original formula in Excel:

=EXP(-0.567143...) - 0.567143...

The result is 0. This confirms that 0.567143... is indeed the root of the equation.

This method is incredibly powerful for engineering calculations where analytical solutions are impossible to find.

Comments

Popular posts from this blog

Hoeken's Linkage: Kinematics and Walking Robot Design

Figure 1: Animated simulation of the Hoeken’s Linkage showing the characteristic "tear-drop" coupler curve. 🚀 New Design Guide Available Don't just read about it—build it. Check out our new tutorial: How to Design a Hoeken’s Linkage in Excel (with Free VBA Simulator) » Introduction to the Hoekens Linkage The Hoekens linkage is a specialized four-bar mechanism designed to convert rotational motion into an approximate straight-line motion. While it serves a similar purpose to other straight-line generators, its unique coupler curve—a "tear-drop" shape—makes it exceptionally useful for intermittent motion and walking machines. One of the most fascinating aspects of kinematic theory is the concept of "Cognates." The Hoekens linkage is actually a cognate linkage of the Chebyshev Straight-line Mechanism . This means that while the physical structure and link lengths differ, they can generate...

Dowel Pins & Locating Pins: The Basics of Fixture Design

Dowel pins are precision cylindrical pins used for accurate part alignment in assemblies. They control position, not clamping force. This guide explains tolerances, fits, sizing rules, and design best practices. Figure 1: A typical fixture setup. Notice how dowel pins (silver) provide precise location, while bolts (not shown here) provide the clamping force. In the world of Precision Engineering , the difference between a high-quality product and a scrap part often comes down to microns. While bolts hold parts together, they are terrible at positioning them. This is where Dowel Pins and Locating Pins become essential components in industrial tooling . Advertisement What is a Dowel Pin? Dowel pins are precision-ground fasteners used to secure the relative position of two parts. They are typically machined to extremely tight tolerances (often within 0.0001 inches) and are available in materials like: Hardened Steel: For high-wea...

Conveyor Belt Tension Calculation: T1, T2 & Take-Up Design

In any friction-driven conveyor system, the most fundamental concept is the relationship between the Tight Side Tension (T 1 ) and the Slack Side Tension (T 2 ) . If you get this ratio wrong, your drive pulley will slip, your belt will wear out prematurely, or your take-up counterweight will be too light to maintain traction. In this guide, we will use CEMA standard calculations to determine the correct tensions and take-up weight. Table of Contents 1. The Basics: T1 vs T2 2. Euler’s Equation (The Grip Formula) 3. Worked Example: Calculating Tensions 4. Take-Up Units: Gravity vs Screw 5. Common Failure Modes Advertisement 1. The Basics: T1 vs T2 Imagine a conveyor belt running over a drive pulley. The motor pulls the belt, creating a tension differential: T 1 (Tight Side): The tension pulling the loaded belt toward the drive pulley. This is the highest tension point in the system. ...