Wednesday, May 27, 2009

Numerical Methods - The Newton-Raphson Method to Solve Mechanical Design Problems Part II

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 Newton-Raphson Method.

As we know that Newton-Raphson Method is the most widely used of all root-locating formulas. The Newton-Raphson method uses the slope (first derivative) of the function to find the root. That means, in the VBA code, we have to find the first derivative of the function in order to find the root. We already have the VBA code to find the first derivative and we're going to use it again in Newton-Raphson Method.


The Newton-Raphson Method uses 2 terms of taylor series to approximate delta x as shown below.

f(x) = f(x0) + (xN - x0)f'(x0) = 0
(xN - x0)f'(x0) = -f(x0)
xN - x0 = -f(x0)/f'(x0)

delta x = -f(x0)/f'(x0)

The followings are the procedure to find the root using Newton-Raphson Method.
  1. Guess the initial value of the root --> select x0
  2. Calculate delta x using the formula as shown above
  3. Calculate xN using xN = x0 + delta x
  4. Check whether the error of xN is within the allowable tolerance or not --> abs(xN - x0) < tolerance
Then the VBA code will be like this.

' ================================================
' Created by Suparerg Suksai
' Mechanical Design Handbook
' http://mechanical-design-handbook.blogspot.com
'
' The Newton-Raphson Method - Numerical Methods
' ================================================

Function f(x As Double) As Double
f=Exp(-x)-x ' This is the function we want to find the root
End Function

Function fDeriv(x As Double) As Double
...
...
End Function

Function RootNewtonRaphson(x0 As Double)
...
...
...
End Function

To calculate the root of e-x - x = 0 we then type the following formula in the Microsoft Excel spreadsheet,

In any cell, type "=RootNewtonRaphson(1)", where 1 is the guessing value of the root.
The program will then show the result of 0.567143290409784.

We can recheck the calculation result by entering the following formula into excel,

=exp(-0.567143290409784)-0.567143290409784

Then the result is 0. That means one of the roots of of e-x - x = 0 is 0.567143290409784


FREE DOWNLOAD AN EXCEL FILE WITH VBA CODE OF NEWTON-RAPHSON ROOT FINDING METHOD

Extract the zip file with password: mechanical-design-handbook.blogspot.com

No comments: