JCDaly

Visual Basic Code Examples

J. C. Daly

Examples follow, David M. Bourg, Excel Scientific and Engineering Cookbook, O'Riley Media, Inc. , 2006,
ISBN 13;978-0-596-00879-6

Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'    Name: y
'    This is a function that calculates the gaussian function, e-x2
'    Author: J. C. Daly
'    Date: 2/3/2011
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Public Function y(x as double) as double

y = exp(-(x^2))
End Function

Trapesoidal Rule

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'    Name: Trapizoidal
'    This is a function that calculates the approximate value of the integral of y(x)
'    Parameters: Minimum x, maximum x, and number of points.
'    Date: 2/3/2011
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Public Function Trapezoidal(xMin as double, xMax as double, n as integer) as double

Dim dx as double       '   Declare variables.
Dim sum as double
Dim z as double

dx = (xMax - xMin)/n       '   calculate the increment in x

sum = (y(xMin) + y(xMax))/2#       '   # is a type declaration character for Double data types.
      '   Initialize the value of the integral with first and last terms of the sum

For k = 1 to (n-1)       '   Loop n - 1 times

z = y(xMin + (dx*k))       '   z is the value of y(xk)
sum = sum + z       '   Add another term to the sum each time the loop is executed
next k       '   Increment the loop counter.

sum = sum*dx       '   Multiply by dx to get the area.

Trapezoidal = sum       '   export the value of the integral

End Function

Looping

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'    Name: IterateTrapezoidal
'    This function repeatedly evaluates the integral. Each time
'    the change in the result is calculated. This continues until the change
'    is less than a tolerance level or until after 100 trys.
'    Date: 2/3/2011
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Public Function IterateTrapezoidal(xMin as double, xMax as double, n as integer, tol as double)

Dim value as double
Dim oldValue as double
Dim difference as double
Dim counter as integer
Dim nNew as integer

oldValue = Trapezoidal(xMin, xMax, n)       '   Remember the value of the integral
difference = 99999      '   Initialize the difference to a large (incorrect) number.
counter = 0       '   Initialize the counter to the correct value of 0.
nNew = n       '   Initialize the number of points.

      '   Continue for 100 times and as long as
      '   the difference is below the tolerance threshold.

While((counter < 100) and (difference > tol))

nNew = nNew*2       '   Double the number of points and try again.
oldValue = value       '   Store the value
value = Trapezoidal(xMin, xMax, nNew)       '   Calculate a new value for the integral.
difference = ABS(value - oldValue)       '   Get the absolute value of the change.
counter = counter + 1       '   Increnent the counter.
Wend       '   End of while loop.

IterateTrapezoidal = value       '   Export the value of the integral.

End Function