Sunday, May 31, 2009

How to calculate spring force online

I found a very useful web site about computation. Try it and you'll love it.

Wolfram|Alpha's long-term goal is to make all systematic knowledge immediately computable and accessible to everyone. We aim to collect and curate all objective data; implement every known model, method, and algorithm; and make it possible to compute whatever can be computed about anything. Our goal is to build on the achievements of science and other systematization of knowledge to provide a single source that can be relied on by everyone for definitive answers to factual queries.

Wolfram|Alpha aims to bring expert-level knowledge and capabilities to the broadest possible range of people—spanning all professions and education levels. Our goal is to accept completely free-form input, and to serve as a knowledge engine that generates powerful results and presents them with maximum clarity.

Wolfram|Alpha is an ambitious, long-term intellectual endeavor that we intend will deliver increasing capabilities over the years and decades to come. With a world-class team and participation from top outside experts in countless fields, our goal is to create something that will stand as a major milestone of 21st century intellectual achievement.

Today's Wolfram|Alpha is the first step in an ambitious, long-term project to make all systematic knowledge immediately computable by anyone. You enter your question or calculation, and Wolfram|Alpha uses its built-in algorithms and growing collection of data to compute the answer. Based on a new kind of knowledge-based computing.

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

Saturday, May 2, 2009

Numerical Methods - First derivative using Excel VBA code

In the previous post, Numerical Methods - First derivative using Excel formula, we know how to calculate the first derivative of functions using formula in Microsoft Excel. In this post, let's see how we can simplify it by using Excel VBA code.

The VBA is just like this.

' ================================================
' Created by Suparerg Suksai
' Mechanical Design Handbook
' http://mechanical-design-handbook.blogspot.com
'
' First Derivative - Numerical Methods
' ================================================

Function f(x As Double) As Double
 
f = 5 * (x - 3) ^ 3 - 4 * x ^ 2 - Sin(2 * x)
End Function


Function fDeriv(x As Double) As Double
Const
h = 0.00001 ' Step size
fDeriv = (-f(x + 2 * h) + 8 * f(x + h) - 8 * f(x - h) + f(x - 2 * h)) / 12 / h
End Function


The highlighted (yellow) text is the function that we want to calculate for the first derivative. We can simply replace it with new function. The remaining code still remains the same.

We have to put this code into the Microsoft Visual Basic Editor in Microsoft Excel (Alt+F11). Now we're going to test this Derivative code. Because the first derivative of the function in this example can be determined by direct method. Then we can use it to recheck the calculation result.

The first derivative of 5(x-3)3-4x2-sin(2x) is 15(x-3)2-8x-2cos(2x). Therefore the first derivative of f(x) where x = 7 must be 15(7-3)2-8(7)-2cos(2(7)) = 183.7265256

Here how to use

In excel spreadsheet, enter =fDeriv(7) and see the result.



We'll use this First Derivative Function in the Newton-Raphson Root Finding Method later.

password: mechanical-design-handbook.blogspot.com
FREE DOWNLOAD EXAMPLE EXCEL FILE