Sie sind auf Seite 1von 47

KWAME NKRUMAH UNIVERSITY OF SCIENCE AND

TECHNOLOGY, KUMASI

VBA Mini Project

MATH 376

GROUP TWO

Group Members

• Amui Judith Diana Obieley - 1068113

• Awaworyi Wise Dotsey - 1069313

• Antwi Daniel - 1068513

• Amenuku Raymond Mawunyo - 1067813

• Marley Robert - 1072013

• Seble Patience Velon - 1075413

MAY, 2015
Acknowledgements

We first acknowledge Mr. E. Owusu Ansah (Onassis) for his guidelines through his lectures and

also from the course note (Beginning Programming With VBA) and we also thank all the group

members for their various contributions in making this project successful. And to the other groups

who also gave us some ideas.

i
Abstract

Solving numerical analysis problem is involving and therefore takes a lot of time when computing

the solutions manually Excel has being widely used by many Ghanaians to perform tasks, many

people are end users of Microsoft Excel, they do not really make Excel work for them. In this

project we are using the Visual Basic for Application embedded in Microsoft Excel to develop an

excel programme that would solve numerical problems. We are using VBA to create a software

based in excel with the help of the Visual Basic Editor VBE. In this project we find a way to

solve numerical integration using excel with a suitable and interactive user interface.

ii
Table of Contents

Page

Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i

Table of Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iii

List of Figures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v

Chapter

1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.2 Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.3 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

1.4 How to Achieve . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

2 Brief History and Exposition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

2.0.1 Trapezoidal Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6


1
2.0.2 Simpson’s Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3
3
2.0.3 Simple Simpson’s Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
8
2.0.4 Simple Boole’s Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

3 Methodology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

3.2 Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

4 Result and Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

5 Conclusion and Recommendations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

iii
5.1 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

5.2 Recommendation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

6 Appendix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

6.1 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

6.1.1 Variable Deceleration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

6.1.2 Error Handling 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

6.1.3 Trapezoidal Simple code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

1
6.1.4 Simpson’s 3 Simple code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

3
6.1.5 Simpson’s 8 Simple code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

6.1.6 Boole’s Simple code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

6.1.7 Trapezoidal Composite code . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

1
6.1.8 Simpsons 3 Composite code . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

3
6.1.9 Simpsons 8 Composite code . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

6.1.10 Boole Composite code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

6.1.11 Error handling code 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

6.1.12 Codes for buttons in the Userform . . . . . . . . . . . . . . . . . . . . . . . 34

6.1.13 Display control code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

6.1.14 Supporting codes and Functions . . . . . . . . . . . . . . . . . . . . . . . . 39

iv
List of Figures

4.1 Userform 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

4.2 Userform 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

4.3 Function Error 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

4.4 Lower limit Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

4.5 Segment Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

4.6 Help bottom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

4.7 Help 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

4.8 Final Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

v
Chapter 1

Introduction

1.1 Introduction

The basic problem in Numerical integration is to compute an approximate solution to definite


Rb
integral a f (x)dx to a given degree of accuracy.

There are many methods for approximating the integral to the desired precision. Some of these

methods are programmed in macro using VBA in Excel.

Excel VBA implies Excel Visual Basic for Applications. Excel VBA is the name of the

programming language in Excel;

• where one can create a macro by automating tasks in Excel;

• to automate computing operations and extend Excel functionality for easy use by any end-user.

1.2 Problem Statement

• Most scholars waste much time in the process of manual computation in each of the many

existing methods in Numerical integration, due to the fact they are long and complex.

• Fear for much error occurrence and uncertainty in a result after manual calculation.

• In addressing these situations, we developed a VBA program for a few Numerical integration
1 3
methods like Trapezoidal rule, Simpson’s , Simpson’s and the Boole’s rule.
3 8

1
1.3 Objectives

Is to develop an application programme that will make computation of Numerical integration

simple and easy for any end user who aims at using any of the numerical integration method

approaches.

• help in improving productivity and efficiency in the user’s field of work.

• Also, make it able to apply various numerical integration techniques in Numerical analysis.

• To understand and do calculations about various functions that can occur using the Numerical

integration methods.

• Be able to write programs in Visual Basic that can be used as a user defined functions, UDF.

1.4 How to Achieve

? Make sure an Excel application is launched with a developer TAB;

? Obtain the accurate formula for each of the Numerical integration computation methods with

their required conditions;

? After the appropriate formula, an algorithm is carefully design and from which our code will

be written in a macro using the VBE tool of the Excel;

? User forms are created with the various operational buttons;

? Each user form created is hence assign to a macro with a command button on the Excel sheet;

? Finally, one has to click the command button for the user form to emerge and hence

demanding the user’s input for computation.

2
Chapter 2

Brief History and Exposition

EXPOSITION ON NUMERICAL INTEGRATION AND COMPUATIONS

Historically, Numerical Methods and Computation otherwise called Numerical Analysis predates

he invention of modern computers by many centuries. Linear interpolation was already in use

more than 2000 years ago. Many great Mathematicians of the past were preoccupied by

numerical analysis, as is obvious from the names of important algorithms like Newtons method,

Lagrange interpolation polynomial, Gaussian elimination, or Eulers method.

Numerical Methods are methods designed for the constructive solution of mathematical

problems requiring particular numerical results, usually on a computer. It is a complete and

unambiguous set of procedures for the solution of a problem, together with computable error

estimates. The study and implementations of such methods is the province of Numerical

Integration.

Numerical analysis is the area of mathematics that creates, analyzes, and implements algorithms

for obtaining numerical solutions to problems involving continuous variables. Also, It is the

study of algorithmsthat use numerical approximation(as opposed to general symbolic) for the

problems of mathematical(as distinguished from discrete).

When to use Numerical Methods:

It is important to note the limitations of numerical methods so they can be used effectively.

3
Numerical methods can only deliver approximate solutions to problems over a defined interval

such as time or distance. The error present in the solution is dependent on how the problem is

solved. Analytical methods provide solution valid at all points. The amount of error in a

solution when using Numerical methods is dependent upon the series of methods used and the

order in which they are used. Hence it is important to determine how accurate your solution

needs to be and o track the error in your solution.

It is important to differentiate between two key concepts of error: Accuracy and Precision.

Accuracy is how close a computed or measured value comes to the theoretical true value.

Precision is how repeatable a value is.

Numerical methods are employed with large systems involving much iteration. They are used

when evaluating empirical information such as experimental data. Here are some properties in

solving Numerical methods,

• The solution is only valid for a defined interval (time, distance).

• A close approximation is good enough

• The solutions can applied to many systems

• Large systems with many iterations

• Evaluating experimental data

• Short time or little resources available to construct a solution

Numerical methods and Computation is in two folds; Direct and Iterative Methods.

Direct methods compute the solution to a problem in a finite number of steps. These methods

give the precise answer when performed in infinite precision arithmetic. Examples include;

1. Gaussian elimination

4
2. QR factorization method for solving Systems of linear equations

3. Simplex method or Linear programming

4. Numerical integration e.g. Trapezoidal rule, Simpsons 13 and 3


8 rule and Boole,s rule

5. Numerical differentiation e.g. Euler method, Runge-kutta

Contradictorily, Iterative methods are not expected to terminate in a finite number of steps.

Starting from an initial guess, hey form successive approximations that converge to the exact

solution only in the limit. A convergence test often involving the residual, is specified in order

ton decide when a sufficiently accurate solution has been found. Examples include;

1. Newtons method

2. Bisection method

3. Jacobi method

4. Secant method

5. Newton-Raphsons method

Numerical integration for single integral comes in two general flavors. You either work with an

array of data points or with analytical functions. This application focuses on the latter case. Let

me start by quickly going over some of the basic numerical integration algorithms that we want

to include.

Here are the Simple methods we are working with;

Trapezoidal Rule

5
Simpson’s 31 Rule

Simpson’s 38 Rule

Boole’s Rule

Here are the Composite methods we are working with;

Trapezoidal Composite Rule

Simpson’s 13 Composite Rule

Simpson’s 13 Composite Rule

Boole’s Composite Rule

2.0.1 Trapezoidal Rule

In numerical, the trapezoidal rule(also known as thetrapezoid ruleortrapezium rule) is a


Rb
technique for approximating the definite a f (x)dx

The trapezoidal rule works by approximating the region under the graph of the functionf (x)as
Rb f (a) + f (b)
atrapezoidand calculating its area. It follows that a f (x)dx ≈ (b − a)
2
For a domain discretized intoNequally spaced panels, orN + 1 grid

pointsa = x1 < x2 < ... < xN + 1 = b, where the grid spacing ish = (b − a)/N , the

approximation to the integral becomes


Rb b−a b−a
[f (a) + 2 n−1
P
a f (x)dx ≈ I = i=1 f (a + i( )) + f (b)]
2n n

1
2.0.2 Simpson’s Rule
3

Inanalysis, Simpsons ruleis a method fornumerical integration, the numerical approximation

ofdefinite integrals. Specifically, it is the following

6
Rb b−a b−a
approximation: a f (x)dx ≈ [f (a) + 4 ∗ f (a + ) + f (b)]
6 2
If the interval of integrationis in some sense ”small”, then Simpson’s rule will provide an

adequate approximation to the exact integral. By small, what we really mean is that the

function being integrated is relatively smooth over the interval [a,b]. For such a function, a

smooth quadratic interpolant like the one used in Simpson’s rule will give good results.

However, it is often the case that the function we are trying to integrate is not smooth over the

interval. Typically, this means that either the function is highly oscillatory, or it lacks

derivatives at certain points. In these cases, Simpson’s rule may give very poor results. One

common way of handling this problem is by breaking up the interval[a,b]into a number of small

subintervals. Simpson’s rule is then applied to each subinterval, with the results being summed

to produce an approximation for the integral over the entire interval. This sort of approach is

termed thecomposite Simpson’s rule.

Suppose that the interval[a,b]is split up insubintervals, withan even number. Then, the

composite Simpson’s rule is given by


Rb b−a
[f (a) + 4 n−2
P Pn−1
a f (x)dx ≈ I = i=1,3,5... f (xi ) + 2 i=2,4,6... f (xi ) + f (b)] The error committed
3n
h4
by the composite Simpson’s rule is bounded (in absolute value) by (b − a)max f 4 (ε)
180

3
2.0.3 Simple Simpson’s Rule
8

Simpson’s 3/8 rule is another method for numerical integration proposed by Thomas Simpson.

It is based upon a cubic interpolation rather than a quadratic interpolation. Simpson’s 3/8 rule
Rb 3(b − a) b−a b−a
is as follows: a f (x)dx ≈ [f (a) + 3f (a + ) + 3f (a + 2 ) + f (b)]
24 3 3
With the composite methods where n is the number of segments

7
Note, we can only use this ifnis a multiple of three.:
3
Composite Simpson’s Rule;
8
Rb 3(b − a)
[f (a) + 3 n−2
P Pn−1 Pn−3
a f (x)dx ≈ i=1,4,7... f (xi ) + 3 i=2,5,8,... f (xi ) + 2 i=3,6,9,... f (xi ) + f (b)]
8n
3 (b − a)4
The error committed by the Simpson’s Rule is given by |f (ε)|
8 6480

2.0.4 Simple Boole’s Rule

Boole’s rule, named afterGeorge Boole, is a method ofnumerical integration. It approximates an

b−a
integral h= 4
Rb h
a f (x)dx ≈ I = [f (a) + 64f (x1 ) + 24f (x2 ) + 64f (x3 ) + 14f (b)]
45
With the composite methods where m is the number of segments:

b−a
Rb
h= 4m let n = 4m a f (x)dx ≈ I =
2h
[7(f (a) + f (b)) + 32 n−1
P Pn−2 Pn−4
i=1,3,5,... f (a + ih) + 12 j=2,6,10,... f (a + jh) + 14 i=4,8,12,... f (a + kh)]
45

8
Chapter 3

Methodology

3.1 Introduction

This chapter details the step to step process that lead to the achievement of our objectives. Here

we started right from the Excel sheet through VBA, VBE, UDF till the user form is made available

for use in computations.

3.2 Work

Visual Basic for Applications (VBA) is one of the tools or platforms used for carrying out

Numerical Methods and Computation. It is programming software similar to MATLAB.

VBA is a programming language and environment for advanced users of Excel and other

Microsoft Office products to automate computing operations and extend Excel functionalities.

Automating a task by using VBA offers several merits

• Excel executes the task in exactly he same way

• Excel performs task much faster than you do it manually

• Excel always performs task without errors

Basically, actions are performed in VBA by recording code in a module. A VBA module consists

of Sub Procedures and can also have Function Procedures. It manipulates Objects which are

9
arranged in a hierarchy.

VBA is an object-oriented programming language which consists of individual objects with its

own set of features called Properties and uses called Methods. Objects in Excel include Cell,

Tab, Sheet, Workbooks etc. An object has properties like Value, lock, Name, Color etc. it also

has methods like Activate, Refresh, Clear etc.

The Editor for VBA is called Visual Basic Editor (VBE). It is a user friendly development

environment where programmed instructions are maintained in order to make your spreadsheet

application work. It is a separate application where you write and edit VBA macros. It has four

(4) components:

• Project Explorer Window which displays Workbook objects, Worksheets and Module objects.

• Properties Window which displays a list of the properties and their assigned value.

• Immediate Window is used for executing VBA statements directly and for debugging your

code.

• Code Window, where the code for macros and VBA procedures are located.

A Module is located in the Project Window. It is just a container of ones code. It can be in

many forms,

• Standard macros you create.

• Class modules enables you create your own objects.

• Workbook modules maintain code for handling workbook-events.

• Worksheet modules keep VBA code as a procedure.

• Userform modules have costumed Userform Interface.

A VBA code that writes in the Editor is known as a Procedure. There are two common types,

10
Sub and Function.

? A Sub procedure is a group of VBA statements that perform action(s) wih Excel.

? A Function procedure is a group of VBA statements that performs a calculation and returns a

single value or an array.

In a module containing codes in VBA, there is a need for a Variable. VBA stores data in

memory using a variable. A Variable is a name you assign a piece of data to hat is stored in the

computers memory. It needs to be declared before it can function in a code. To do that,

sequentially,

? The Dim statement where Dim means Dimension

? The name of your variable

? The word As

? The type of data being stored

VBA enforces few rules concerning variable names:

? The first character must be a letter. It can also include numbers and punctuations.

? Variables cannot be more than 255 characters.

? Spaces, periods and commas cannot be used in a variable name.

? VBA is not case sensitive

A Data Type is a manner in which a program stores data such as Integers, Real numbers, or

Strings. VBA has some built-in data types which include,

X Byte uses 1byte

X Boolean uses 2bytes

X Integer uses 2bytes

11
X Long uses 4bytes

X Single uses 4bytes

X Double uses 8bytes

A Userform is a type of module. It is useful if your VBA macro needs to pause and get some

information from a user. In creating Userforms, you would have to follow the following

1. Determine how the dialogue box will be used. At what point will it be displayed in your

VBA macro.

2. Activate the VBE and Insert a new Userform object.

3. Add controls to the Userform which include text boxes, buttons, check boxes, and list

boxes.

4. Use the Properties Window to modify the properties for the controls or the Userform itself.

5. Write event-handler procedures for the controls.

6. Write a procedure that displays the dialog box to the user

VBA has two forms of controls, Form and ActiveX control. Below is a description of some of

them.

X Label control shows text

X Textbox allows the user to enter text

X Combo Box - displays a drop-down list

X List Box displays a list of items

X Checkbox useful for on/off or yes/no options

X Frame a container for other controls

12
In Summary, VBA is a Help System to Excel. Together they are platforms used in solving

Numerical methods and Computations. This improves on productivity and efficiency in the

users work.

13
Chapter 4

Result and Discussion

This is the outcome of the software An introduction to our program is the Userform. The form

allows the program to grab information from users. The Userform is created in VBE in excel

and assigned to a button(s) in the Excel Worksheet. That button allows the user to provided

appropriate answers of the form. Basically, the Userform begins the users computations.

Error handling refers to the programming practice of anticipating and coding for error conditions

that may arise when your program runs. Errors in general come in three flavors: compiler errors

such as undeclared variables that prevent your code from compiling; user data entry error such as

a user entering a negative value where only a positive number is acceptable; and run time errors,

that occur when VBA cannot correctly execute a program statement.

After clicking the button on Excel, the Userform pops up. Assuming you dont make any inputs,

you will alert to input something before u can proceed. For instance, lets say you enter a function

with the parameter x. Then you cannot use any other variable except x.

Also on the Userform is the Help Button. This button happens to give the user a general and fair

idea about what he or she is about. It introduces briefly to the user how the information required

should be handled.

Final Output:

After providing all information required on the Userform, then the user can press on the command

14
button provided to view the output. The output appears within a particular range on the active

Excel Worksheet that has been specified in the module.

Figure 4.1: Excel sheet user form

15
Figure 4.2: Main User form

16
Figure 4.3: Error when there is no function

Figure 4.4: Error when there is no lower limit

17
Figure 4.5: Error massage when there is no number of segments specified

Figure 4.6: What displays when you click Help

18
Figure 4.7: Second help screen

Figure 4.8: The output when you are done computing

19
Chapter 5

Conclusion and Recommendations

5.1 Conclusion

In conclusion,Numerical integration makes evaluations and computations of an integrand easy in

order to get an approximate value.

5.2 Recommendation

? We recommend this Program to both Lecturers and Students of this particular course to aid in

feasible teaching and learning process.

? We also recommend that an improvement should be done on this program.

20
Chapter 6

Appendix

6.1 Code

6.1.1 Variable Deceleration

Private Sub cbCompute_Click()

(Reading of required data and Declaration of variables, Examples: Dim n As Integer, this allow

Dim n As Integer, r As Integer, m As Integer, ra As Integer, w As Integer

Dim a As String, b As String, h As Double, o As Integer, jj As Double

Dim c As Double, d As Double, sum4 As Double, sum5 As Double

Dim variable As String, g As Double, i As Integer, hh As Double

Dim j As Integer, l As Double, k As Integer, ee As Double, sum8 As Double

Dim t As Integer, u As Double, sum7 As Double, gg As Double

Dim fxn As String, sum6 As Double, cc As Double, ff As Double

Dim p As Integer, q As Double, s As Double, dd As Double

Dim sum1 As Double, sum2 As Double, f As Double, v As Integer

Dim z As Double, sum3 As Double, response As String, ii As Double

21
Here we assign values to variables which would be stored in the memory VBA.

fxn = tbFunction.Value

variable = tbVariable.Value

a = tblwBound.Value

b = tbupBound.Value

6.1.2 Error Handling 1

This is a conditional statement that helps pop up an error massage when the user does not prov

the Elseif structure in our conditional statement.

If tbFunction.Value = "" Then

response = MsgBox("Please enter a function", vbInformation, "Numerical Integration")

tbFunction.SetFocus

Exit Sub

ElseIf tbVariable.Value = "" Then

response = MsgBox("Please enter a variable", vbInformation, "Numerical Integration")

tbVariable.SetFocus

Exit Sub

ElseIf tblwBound.Value = "" Then

response = MsgBox("Please enter a lower limit", vbInformation, "Numerical Integration")

22
tblwBound.SetFocus

Exit Sub

ElseIf tbupBound.Value = "" Then

response = MsgBox("Please enter an upper limit", vbInformation, "Numerical Integration")

tbupBound.SetFocus

Exit Sub

End If

’Check that the entries are in the correct format

On Error GoTo ErrorHandling

6.1.3 Trapezoidal Simple code

This defined the algorithm for solving the Trapezoidal single method, this is activated when t

If chckbxTrapezoidal.Value = True Then

Range("B3").Value = "Function"

Range("C3").Value = fxn

Range("A4").Value = "Method"

Range("A5").Value = "Trapezoidal"

’Range("A5").Value = "Simpson’s 1/3"

’Range("A6").Value = "Simpson’s 3/8"

h = (val(b) - val(a))

23
c = Solve(fxn, variable, a)

d = Solve(fxn, variable, b)

sum1 = (h / 2) * (c + d)

Range("B4").Value = "Results"

Range("B5").Value = sum1

’Unload Me

End If

1
6.1.4 Simpson’s 3
Simple code

This defined the algorithm for solving the Simpsons 1/3 single method, this is activated when

If chckbxSimpson13.Value = True Then

Range("B3").Value = "Function"

Range("C3").Value = fxn

Range("A4").Value = "Method"

’Range("A4").Value = "Trapezoidal"

Range("A6").Value = "Simpson’s 1/3"

’Range("A6").Value = "Simpson’s 3/8"

h = (val(b) - val(a))

c = Solve(fxn, variable, a)

24
d = Solve(fxn, variable, b)

f = Solve(fxn, variable, val(a + (h / 2)))

sum2 = (h / 6) * (c + 4 * f + d)

Range("B4").Value = "Results"

Range("B6").Value = sum2

’Unload Me

End If

3
6.1.5 Simpson’s 8
Simple code

This defined the algorithm for solving the Simpsons 3/8 single method, this is activated when

If chckbxSimpson38.Value = True Then

Range("B3").Value = "Function"

Range("C3").Value = fxn

Range("A4").Value = "Method"

’Range("A4").Value = "Trapezoidal"

’Range("A5").Value = "Simpson’s 1/3"

Range("A7").Value = "Simpson’s 3/8"

h = (val(b) - val(a))

c = Solve(fxn, variable, a)

d = Solve(fxn, variable, b)

g = Solve(fxn, variable, val(a + (h / 3)))

z = Solve(fxn, variable, val((a + (h / 3)) + (h / 3)))

25
sum3 = (h / 8) * (c + 3 * g + 3 * z + d)

Range("B4").Value = "Results"

Range("B7").Value = sum3

’Unload Me

End If

6.1.6 Boole’s Simple code

’This defined the algorithm for solving the Boole single method, this is activated when the ch

If chckbxBoole.Value = True Then

Range("B3").Value = "Function"

Range("C3").Value = fxn

Range("A4").Value = "Method"

Range("A8").Value = "Boole"

’Range("A5").Value = "Simpson’s 1/3"

’Range("A6").Value = "Simpson’s 3/8"

h = ((val(b) - val(a))) / 4

cc = Solve(fxn, variable, a)

dd = Solve(fxn, variable, b)

ee = Solve(fxn, variable, val(a + h))

ff = Solve(fxn, variable, val(a + (2 * h)))

gg = Solve(fxn, variable, val(a + (3 * h)))

26
sum7 = (h / 45) * (14 * cc + 64 * ee + 24 * ff + 64 * gg + 14 * dd)

Range("B4").Value = "Results"

Range("B8").Value = sum7

’Unload Me

End If

6.1.7 Trapezoidal Composite code

This defined the algorithm for solving the Trapezoidal Composite method, this is activated wh

If chckbxTrapezoidalComp.Value = True Then

Range("B3").Value = "Function"

Range("C3").Value = fxn

Range("E4").Value = "Method"

Range("E5").Value = "Trapezoidal Composite"

’Range("E5").Value = "Simpson’s 1/3 Composite"

’Range("E6").Value = "Simpson’s 3/8 Composite"

If Trim(tbNos.Value) = "" Then ’Nos~Number of Segments

MsgBox "Please provide the number of segments"

Exit Sub

End If

n = tbNos.Value

o = (val(b) - val(a))

27
h = o / n

c = Solve(fxn, variable, a)

d = Solve(fxn, variable, b)

For k = 1 To n - 1

m = Solve(fxn, variable, val(a + (k * h))) + m

Next k

sum4 = (h / 2) * (c + (2 * m) + d)

Range("F4").Value = "Results"

Range("F5").Value = sum4

’Unload Me

End If

1
6.1.8 Simpsons 3
Composite code

’This defined the algorithm for solving the Simpsons 1/3 Composite method, this is activated w

If chckbxSimpson13Comp.Value = True Then

’Range("E6").Value = "Simpson’s 3/8 Composite"

If Trim(tbNos.Value) = "" Then

MsgBox "Please provide the number of segments"

Exit Sub

End If

28
n = tbNos.Value

If n Mod 2 <> 0 Then

MsgBox "The number of segments should be divisible by 2"

Exit Sub

Range("B3").Value = "Function"

Range("C3").Value = fxn

Range("E4").Value = "Method"

’Range("E4").Value = "Trapezoidal Composite"

Range("E6").Value = "Simpson’s 1/3 Composite"

End If

o = (val(b) - val(a))

h = o / n

c = Solve(fxn, variable, a)

d = Solve(fxn, variable, b)

For i = 1 To n - 1 Step 2

g = Solve(fxn, variable, val(a + (i * h))) + g

Next i

For j = 2 To n - 2 Step 2

29
l = Solve(fxn, variable, val(a + (j * h))) + l

Next j

sum5 = (h / 3) * (c + (4 * g) + (2 * l) + d)

Range("F4").Value = "Results"

Range("F6").Value = sum5

’Unload Me

End If

3
6.1.9 Simpsons 8
Composite code

’This defined the algorithm for solving the Simpson’s Composite method, this is activated when

If chckbxSimpson38Comp.Value = True Then

Range("B3").Value = "Function"

Range("C3").Value = fxn

Range("A4").Value = "Method"

’Range("E4").Value = "Trapezoidal Composite"

’Range("E5").Value = "Simpson’s 1/3 Composite"

Range("E7").Value = "Simpson’s 3/8 Composite"

If Trim(tbNos.Value) = "" Then

MsgBox "Please provide the number of segments"

Exit Sub

End If

n = tbNos.Value

30
If n Mod 3 <> 0 Then

MsgBox "The number of segments should be divisible by 3"

Exit Sub

Range("B3").Value = "Function"

Range("C3").Value = fxn

Range("E4").Value = "Method"

’Range("E4").Value = "Trapezoidal Composite"

Range("E6").Value = "Simpson’s 3/8 Composite"

End If

o = (val(b) - val(a))

h = o / n

c = Solve(fxn, variable, a)

d = Solve(fxn, variable, b)

For p = 1 To n - 2 Step 3

q = Solve(fxn, variable, val(a + (p * h))) + q

Next p

For r = 2 To n - 1 Step 3

s = Solve(fxn, variable, val(a + (r * h))) + s

Next r

For t = 3 To n - 3 Step 3

u = Solve(fxn, variable, val(a + (t * h))) + u

Next t

31
sum6 = h * (3 / 8) * (c + (3 * q) + (3 * s) + (2 * u) + d)

Range("F4").Value = "Results"

Range("F7").Value = sum6

’Unload Me

End If

6.1.10 Boole Composite code

’This defined the algorithm for solving the Boole Composite method, this is activated when the

If chckbxBooleComp.Value = True Then

’Range("E6").Value = "Simpson’s 3/8 Composite"

If Trim(tbNos.Value) = "" Then

MsgBox "Please provide the number of segments"

Exit Sub

End If

n = tbNos.Value

Range("B3").Value = "Function"

Range("C3").Value = fxn

Range("E4").Value = "Method"

’Range("E4").Value = "Trapezoidal Composite"

Range("E8").Value = "Boole’s Composite"

o = (val(b) - val(a))

h = o / (4 * n)

32
c = Solve(fxn, variable, a)

d = Solve(fxn, variable, b)

For ra = 1 To (4 * n) - 1 Step 2

hh = Solve(fxn, variable, val(a + (ra * h))) + hh

Next ra

For v = 2 To (4 * n) - 2 Step 4

ii = Solve(fxn, variable, val(a + (v * h))) + ii

Next v

For w = 4 To (4 * n) - 4 Step 4

jj = Solve(fxn, variable, val(a + (w * h))) + jj

Next w

sum8 = ((2 * h) / 45) * (7 * (c + d) + 32 * hh + 12 * ii + 14 * jj)

Range("F4").Value = "Results"

Range("F8").Value = sum8

’Unload Me

End If

Exit Sub

6.1.11 Error handling code 2

’This displays an error massage when any of the input is invalid

ErrorHandling:

MsgBox "Check that all the entries are correct. There was an error"

33
End Sub

6.1.12 Codes for buttons in the Userform

’This controls what happens when the EXIT button is clicked

Private Sub cbExitNIU_Click()

Unload Me

End Sub

’This controls what happens when the HELP button is clicked

Private Sub cbHelpNIU_Click()

frmHelp1.Show

End Sub

’This controls what happens when the RESET button is clicked

Private Sub cbreset_Click()

Cells.Select

Selection.Clear

Range("a1").Select

tbFunction.Value = ""

tblwBound.Value = ""

34
tbupBound.Value = ""

tbVariable.Value = ""

tbNos.Value = ""

chckbxTrapezoidal.Value = False

chckbxSimpson13.Value = False

chckbxSimpson38.Value = False

chckbxBoole.Value = False

chckbxTrapezoidalComp.Value = False

chckbxSimpson38Comp.Value = False

chckbxSimpson13Comp.Value = False

chckbxBooleComp.Value = False

End Sub

6.1.13 Display control code

’This part shows what data can be inputted when the check box(es) is(are) checked. When the vi

Private Sub chckbxBoole_Click()

With frmNumInt

’.LabelUpperBound.Caption = " Upper Bound"

’.LabelLowerBound.Caption = " Lower Bound"

.tbNos.Visible = False

.lbSeg.Visible = False

End With

35
End Sub

Private Sub chckbxBooleCompo_Click()

With frmNumInt

’.LabelUpperBound.Caption = " Upper Bound"

’.LabelLowerBound.Caption = " Lower Bound"

.tbNos.Visible = True

.lbSeg.Visible = True

End With

End Sub

Private Sub chckbxBooleComp_Click()

With frmNumInt

’.LabelUpperBound.Caption = " Upper Bound"

’.LabelLowerBound.Caption = " Lower Bound"

.tbNos.Visible = True

.lbSeg.Visible = True

End With

End Sub

Private Sub chckbxSimpson13Comp_Click()

With frmNumInt

36
’.LabelUpperBound.Caption = " Upper Bound"

’.LabelLowerBound.Caption = " Lower Bound"

.tbNos.Visible = True

.lbSeg.Visible = True

End With

End Sub

Private Sub chckbxSimpson38_Click()

With frmNumInt

’.LabelUpperBound.Caption = " Upper Bound"

’.LabelLowerBound.Caption = " Lower Bound"

.tbNos.Visible = False

.lbSeg.Visible = False

End With

End Sub

Private Sub chckbxSimpson38Comp_Click()

With frmNumInt

’.LabelUpperBound.Caption = " Upper Bound"

’.LabelLowerBound.Caption = " Lower Bound"

.tbNos.Visible = True

.lbSeg.Visible = True

37
End With

End Sub

Private Sub chckbxTrapezoidal_Click()

With frmNumInt

’.LabelUpperBound.Caption = " Upper Bound"

’.LabelLowerBound.Caption = " Lower Bound"

.tbNos.Visible = False

.lbSeg.Visible = False

End With

End Sub

Private Sub chckbxTrapezoidalComp_Click()

With frmNumInt

’.LabelUpperBound.Caption = " Upper Bound"

’.LabelLowerBound.Caption = " Lower Bound"

.tbNos.Visible = True

.lbSeg.Visible = True

End With

End Sub

Private Sub chckbxSimpson13_Click()

38
With frmNumInt

’.LabelUpperBound.Caption = " Upper Bound"

’.LabelLowerBound.Caption = " Lower Bound"

.tbNos.Visible = False

.lbSeg.Visible = False

End With

End Sub

6.1.14 Supporting codes and Functions

Function Solve(fxn As String, variable As String, val As String)

Solve = Evaluate(Application.WorksheetFunction.Substitute(fxn, variable, val))

End Function

Function e(x As Double)

e = Exp(x)

End Function

Sub showfrmNumericalIntegration()

frmNumericalIntegration.Show

End Sub

Sub ClearAll()

Range("A3:Z1000000").Clear

39
End Sub

Sub showfrmNumInt()

frmNumInt.Sh

End Sub

40
References

Mixing Numerical Integration Rules By Namir Shammas

Numerical Solution of Booles Rule in Numerical Integration by Using General Quadrature Formula

10.18052/www.scipress.com/BSMaSS.2.1

Beginning Programming With VBA by Mr. E. Owusu Ansah (Onassis).

41

Das könnte Ihnen auch gefallen