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

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...

Taper-Lock Bushing Failures: The Physics of Wedge Angles

The Failure Scenario: A technician is installing a heavy 20-inch cast-iron V-belt sheave onto a motor shaft using a standard Taper-Lock® bushing. To make future removal easier, they apply a generous coat of anti-seize lubricant to the tapered surface. They tighten the set screws with an impact wrench. Instantly, a loud "CRACK" echoes through the shop. The massive cast-iron hub has split perfectly in half. The Cause: The technician misunderstood the physics of mechanical wedges and friction. By lubricating the taper and using unregulated torque, they generated a radial expansion force that exceeded the Ultimate Tensile Strength of the cast iron, causing a catastrophic hoop stress failure. Tapered bushings (including Taper-Lock and QD styles) are the industrial standard for mounting sprockets, gears, and pulleys to shafts. They rely on the mechanical advantage of a wedge to create massive clamping friction. This guide explains the physics of wedge mechanics, the de...

High-Speed Automation: The Mechanics of Sankyo SANDEX Indexing

In my design work for high-speed automation, I've frequently relied on indexing boxes from the Japanese manufacturer Sankyo Seisakusho . The SANDEX series is a generic name for their cam units that utilize the superior Roller Gear Cam mechanism . Sankyo's core technology combines a precision-machined globoidal cam with a follower turret fitted with high-capacity needle bearings. To visualize how this robust mechanism works, consider the conceptual model below. Figure 1: A conceptual model illustrating the interaction between the input roller gear cam (bottom) and the output follower turret (top). Advertisement The Superior Mechanics of the Roller Gear Cam As depicted conceptually in Figure 1 , the SANDEX unit uses a continuous rib on the input shaft to drive the output turret. This is a significant upgrade over traditional Geneva mechanisms . Geneva drives suffer from high impact shock at the start and stop of motion due to inhere...