Skip to main content

Featured Post

Laser Rust Removal: Engineering Science & Best Workshop Tools

There is something inherently satisfying about watching a layer of heavy oxidation vanish instantly under a beam of light. But for engineering design managers and automation specialists, this isn't magic—it is Laser Ablation . In the field of industrial maintenance, non-contact surface cleaning is becoming the gold standard for restoring precision parts without altering the substrate tolerance. Advertisement The Physics: How Laser Ablation Works Laser cleaning operates on the principle of sublimation . The process relies on the differential absorption coefficients of the materials involved. When the high-energy pulse hits the surface, two things happen: The Oxide Layer (Rust): Has a high absorption rate. It absorbs the energy, heats up rapidly, and transitions directly from a solid state to a gas (plasma) phase. The Substrate (Steel): Has a high reflection rate. Once the rust is removed, the laser reflects off the shiny metal, eff...
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...

Watt Straight-Line Linkage: Analysis and Automotive Uses

Figure 1: Watt's linkage example geometry and path generation. Introduction to Watt's Linkage The Watt's linkage (also known as the parallel motion linkage) is a cornerstone in the history of mechanical engineering. It is a type of four-bar linkage originally invented by James Watt in the late 18th century to solve a critical problem in steam engine design: constraining the piston rod to move in a straight line without using high-friction guideways. Before this invention, engines used chains to connect the piston to the beam, which meant they could only pull, not push. Watt's rigid linkage allowed for double-acting engines (pushing and pulling), doubling the power output. He was immensely proud of this kinematic solution, describing it in a 1784 letter to his partner Matthew Boulton: "I have got a glimpse of a method of causing a piston rod to move up and down perpendicularly by only fixing it to a piece of iron u...

Dowel Pins & Locating Pins: The Basics of Fixture Design

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-wear applications in CNC fixtures . Stainless Steel: For corrosion resistance in medical or food processing machinery. Plastic (Thermoplastic): For lightweight, non-conductive, low-load alignment. The ...