Beruflich Dokumente
Kultur Dokumente
TECHNOLOGY, KUMASI
MATH 376
GROUP TWO
Group Members
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
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
List of Figures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v
Chapter
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3 Methodology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.2 Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
iii
5.1 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
5.2 Recommendation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
6 Appendix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
6.1 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1
6.1.4 Simpson’s 3 Simple code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3
6.1.5 Simpson’s 8 Simple code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
1
6.1.8 Simpsons 3 Composite code . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3
6.1.9 Simpsons 8 Composite code . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
iv
List of Figures
4.1 Userform 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.2 Userform 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.7 Help 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
v
Chapter 1
Introduction
1.1 Introduction
There are many methods for approximating the integral to the desired precision. Some of these
Excel VBA implies Excel Visual Basic for Applications. Excel VBA is the name of the
• to automate computing operations and extend Excel functionality for easy use by any end-user.
• 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
simple and easy for any end user who aims at using any of the numerical integration method
approaches.
• 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.
? Obtain the accurate formula for each of the Numerical integration computation methods with
? After the appropriate formula, an algorithm is carefully design and from which our code will
? 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
2
Chapter 2
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,
Numerical Methods are methods designed for the constructive solution of mathematical
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
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
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.
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
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
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.
Trapezoidal Rule
5
Simpson’s 31 Rule
Simpson’s 38 Rule
Boole’s Rule
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
1
2.0.2 Simpson’s Rule
3
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
Suppose that the interval[a,b]is split up insubintervals, withan even number. Then, the
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
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
3.2 Work
Visual Basic for Applications (VBA) is one of the tools or platforms used for carrying out
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.
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
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,
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
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
sequentially,
? The word As
? The first character must be a letter. It can also include numbers and punctuations.
A Data Type is a manner in which a program stores data such as Integers, Real numbers, or
11
X Long uses 4bytes
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.
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.
VBA has two forms of controls, Form and ActiveX control. Below is a description of some of
them.
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
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,
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
15
Figure 4.2: Main User form
16
Figure 4.3: Error when there is no function
17
Figure 4.5: Error massage when there is no number of segments specified
18
Figure 4.7: Second help screen
19
Chapter 5
5.1 Conclusion
5.2 Recommendation
? We recommend this Program to both Lecturers and Students of this particular course to aid in
20
Chapter 6
Appendix
6.1 Code
(Reading of required data and Declaration of variables, Examples: Dim n As Integer, this allow
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
This is a conditional statement that helps pop up an error massage when the user does not prov
tbFunction.SetFocus
Exit Sub
tbVariable.SetFocus
Exit Sub
22
tblwBound.SetFocus
Exit Sub
tbupBound.SetFocus
Exit Sub
End If
This defined the algorithm for solving the Trapezoidal single method, this is activated when t
Range("B3").Value = "Function"
Range("C3").Value = fxn
Range("A4").Value = "Method"
Range("A5").Value = "Trapezoidal"
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
Range("B3").Value = "Function"
Range("C3").Value = fxn
Range("A4").Value = "Method"
’Range("A4").Value = "Trapezoidal"
h = (val(b) - val(a))
c = Solve(fxn, variable, a)
24
d = Solve(fxn, variable, b)
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
Range("B3").Value = "Function"
Range("C3").Value = fxn
Range("A4").Value = "Method"
’Range("A4").Value = "Trapezoidal"
h = (val(b) - val(a))
c = Solve(fxn, variable, a)
d = Solve(fxn, variable, b)
25
sum3 = (h / 8) * (c + 3 * g + 3 * z + d)
Range("B4").Value = "Results"
Range("B7").Value = sum3
’Unload Me
End If
’This defined the algorithm for solving the Boole single method, this is activated when the ch
Range("B3").Value = "Function"
Range("C3").Value = fxn
Range("A4").Value = "Method"
Range("A8").Value = "Boole"
h = ((val(b) - val(a))) / 4
cc = Solve(fxn, variable, a)
dd = Solve(fxn, variable, b)
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
This defined the algorithm for solving the Trapezoidal Composite method, this is activated wh
Range("B3").Value = "Function"
Range("C3").Value = fxn
Range("E4").Value = "Method"
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
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
Exit Sub
End If
28
n = tbNos.Value
Exit Sub
Range("B3").Value = "Function"
Range("C3").Value = fxn
Range("E4").Value = "Method"
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
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
Range("B3").Value = "Function"
Range("C3").Value = fxn
Range("A4").Value = "Method"
Exit Sub
End If
n = tbNos.Value
30
If n Mod 3 <> 0 Then
Exit Sub
Range("B3").Value = "Function"
Range("C3").Value = fxn
Range("E4").Value = "Method"
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
Next p
For r = 2 To n - 1 Step 3
Next r
For t = 3 To n - 3 Step 3
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
’This defined the algorithm for solving the Boole Composite method, this is activated when the
Exit Sub
End If
n = tbNos.Value
Range("B3").Value = "Function"
Range("C3").Value = fxn
Range("E4").Value = "Method"
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
Next ra
For v = 2 To (4 * n) - 2 Step 4
Next v
For w = 4 To (4 * n) - 4 Step 4
Next w
Range("F4").Value = "Results"
Range("F8").Value = sum8
’Unload Me
End If
Exit Sub
ErrorHandling:
MsgBox "Check that all the entries are correct. There was an error"
33
End Sub
Unload Me
End Sub
frmHelp1.Show
End Sub
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
’This part shows what data can be inputted when the check box(es) is(are) checked. When the vi
With frmNumInt
.tbNos.Visible = False
.lbSeg.Visible = False
End With
35
End Sub
With frmNumInt
.tbNos.Visible = True
.lbSeg.Visible = True
End With
End Sub
With frmNumInt
.tbNos.Visible = True
.lbSeg.Visible = True
End With
End Sub
With frmNumInt
36
’.LabelUpperBound.Caption = " Upper Bound"
.tbNos.Visible = True
.lbSeg.Visible = True
End With
End Sub
With frmNumInt
.tbNos.Visible = False
.lbSeg.Visible = False
End With
End Sub
With frmNumInt
.tbNos.Visible = True
.lbSeg.Visible = True
37
End With
End Sub
With frmNumInt
.tbNos.Visible = False
.lbSeg.Visible = False
End With
End Sub
With frmNumInt
.tbNos.Visible = True
.lbSeg.Visible = True
End With
End Sub
38
With frmNumInt
.tbNos.Visible = False
.lbSeg.Visible = False
End With
End Sub
End Function
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
Numerical Solution of Booles Rule in Numerical Integration by Using General Quadrature Formula
10.18052/www.scipress.com/BSMaSS.2.1
41