View
43
Download
0
Category
Preview:
DESCRIPTION
ForTran code to integrat numericaly
Citation preview
1 | P a g e
December 12, 2013
Numerical Integration
and Quadrature Techniques CMPSC 202 Project 3 group component
Dillon Figurelle
Robert Peason
Brian Smith
2 | P a g e
Table of Contents
Abstract.
Fortran Component...
Analysis of Total Approximate Areas..
Analysis of Subinterval Areas...
Conclusion
Page 03
Page 04
Page 14
Page 17
Page 21
3 | P a g e
1. Abstract
People are very efficient at analytically solving mathematical problems. Our mind for diverse and
dynamic computations lend itself well to pulling a large amount of information from minimal details. In
this, we are able to compute the area under a curve using techniques such as integration. However,
unless an equation is of a certain form or relative simplicity, analysis can only go so far.
Numeric techniques, on the other hand, can solve a much wider range of problems. Unfortunately
though, they tend to be very tedious, in some cases they can be too immensely tedious for a human to
solve. But fortunately, while computers arent as suited for dynamic thinking, they are very well suited
for tedious and repetitive tasks. It is for that reason they are well suited for using numerical quadrature
techniques to solve for the area under a curve. Our team seeks to do just that; create a system which can
numerically approximate the area under the curve, and analyze the accuracy of such results. In this
report, you can expect to see:
- Code written in Fortran for approximating the area under a curve using left sums, right sums,
midpoint sums, trapezoid sums and Simpson approximations
- Results generated from this code using a number of test functions
- Visual representations of the approximations as generated by MATLAB
- Error analysis of the results generated by our code
4 | P a g e
2. Fortran Component
This section contains all the methods used to generate our results for five different quadrature
techniques: Left Point, Right Point, Midpoint, Trapezoid and Simpson approximations. The sample runs
and output files in this section, as well as the results analyzed in subsequent sections, will use data
generated from these methods.
2.1. Code
!Programmers: Dillon Figurelle, Robert Peason, Brian Smith
!Section: 001
!Program: Group Project - Numerical Integration
!Date: 12/12/13
!Description: A program that numerically integrates one of three functions using Right,
! Left, Midpoint, Trapezoidal or Simpson approximations
real function f(x)
!PRE: x is initialized and in the domain of f
!POST: FCTVAL == (28/5) + 2sin(4x) - (1/x)
implicit none
real, intent(in) :: x
f = (28.0/5) + 2*sin(4*x) - (1/x)
return
end function f
real function g(x)
!PRE: x is initialized and in the domain of g
!POST: FCTVAL == 2x + log(5x)
implicit none
real, intent(in) :: x
g = 2*x + log(5*x)
return
end function g
real function h(x)
!PRE: x is initialized and in the domain of h
!POST: FCTVAL == (1/sqrt(2PI))*exp((-x^2)/2)
implicit none
real, intent(in) :: x
real, parameter :: PI = 3.141592
h = (1.0/sqrt(2*PI))*exp((-x**2)/2)
return
end function h
5 | P a g e
real function Left(func, a, b)
! PRE: func has one parameter and is continuous between a and b, where a < b
! POST: FCTVAL == area of the rectangle with width = (b - a) and height = func(a)
implicit none
! Parameters
real, external :: func
real, intent(in) :: a
real, intent(in) :: b
Left = func(a)*(b - a)
return
end function Left
real function Right(func, a, b)
! PRE: func has one parameter and is continuous between a and b, where a < b
! POST: FCTVAL == area of the rectangle with width = (b - a) and height = func(b)
implicit none
! Parameters
real, external :: func
real, intent(in) :: a
real, intent(in) :: b
Right = func(b)*(b - a)
return
end function Right
real function Midpoint(func, a, b)
! PRE: func has one parameter and is continuous between a and b, where a < b
! POST: FCTVAL == area of the rectangle with width (b - a) and height func(x), where
! x is the midpoint between a and b
implicit none
! Parameters
real, external :: func
real, intent(in) :: a
real, intent(in) :: b
! Variables
real :: x ! Midpoint between a and b
x = (b - a)/2 + a ! Takes half the distance from a to b
! and adds it to a to find the midpoint
Midpoint = func(x)*(b - a)
return
end function Midpoint
6 | P a g e
real function Trapezoid(func, a, b)
! PRE: func has one parameter and is continuous between a and b, where a < b
! POST: FCTVAL == The area bounded by a trapezoid on func(x) between leftPoint and
! rightPoint
implicit none
! Parameters
real, external :: func
real, intent(in) :: a
real, intent(in) :: b
! Variables
real :: width !The width of the trapezoid
real :: leftHeight !The height of the left base of the trapezoid
real :: rightHeight !The height of the right base of the trapezoid
width = b - a
leftHeight = func(a)
rightHeight = func(b)
Trapezoid = (1.0/2)*(leftHeight + rightHeight)*(width)
return
end function Trapezoid
real function Simpson(func, a, b )
! PRE: func has one parameter and is continuous between a and b, where a < b
! POST: FCTVAL == area of the interval between a and b using the simpson approx.
implicit none
! Parameters
real, external :: func
real, intent(in) :: a
real, intent(in) :: b
! Functions used
real :: midpoint
real :: trapezoid
Simpson = (1.0/3.0)*(2.0*(Midpoint(func, a, b)) +
+ Trapezoid(func, a, b))
return
end function Simpson
subroutine GeneralQuadrature(f, leftBound, rightBound,
+ numIntervals, type, output)
!PRE: leftBound and rightBound are in the domain of function f and leftBound < rightBound;
! numIntervals is a positive integer; type = L, R, M, T or S; and output is an array
! with length numIntervals
!POST: FCTVAL == The approximate area under function f between leftBound and rightBound,
! found through using one of the following estimations:
! if type = L then Left Point Approximation
! if type = R then Right Point Approximation
! if type = M then Mid Point Approximation
! if type = T then Trapezoid Approximation
! if type = S then Simpson Approximation
implicit none
! Parameters
real, external :: f
real, intent(in) :: leftBound
real, intent(in) :: rightBound
integer, intent(in) :: numIntervals
character, intent(in) :: type
real, dimension(numIntervals), intent(out) :: output
7 | P a g e
! Variables
real :: boundWidth !Length between leftBound and rightBound
real :: intervalWidth !Length between each individual interval
real :: currentLeft !Left point being used to evaluate interval area
real :: currentRight !Right point being used to evaluate interval area
integer :: i !loop controll variable
! Functions used
real :: Left
real :: Right
real :: Midpoint
real :: Trapezoid
real :: Simpson
boundWidth = rightBound - leftBound
intervalWidth = boundWidth/numIntervals
do i = 1, numIntervals, 1 !Fills outut array with the areas of each
! subinterval based on the approriate
! technique
currentLeft = leftBound + intervalWidth*(i - 1)
currentRight = leftBound + intervalWidth*i
select case(type) !Uses the appropriate technique as
case('L') ! described in the post condition
output(i) = Left(f, currentLeft, currentRight)
case('R')
output(i) = Right(f, currentLeft, currentRight)
case('M')
output(i) = Midpoint(f, currentLeft, currentRight)
case('T')
output(i) = Trapezoid(f, currentLeft, currentRight)
case('S')
output(i) = Simpson(f, currentLeft, currentRight)
end select
end do
end subroutine GeneralQuadrature
subroutine WriteToFile(array, fileName, N)
! PRE: N is a positive integer, array contains N values, fileName is the name of the
! file to be created and written to
! POST: Each value of array is written to a line on fileName
implicit none
! Parameters
integer, intent(in):: N
real, dimension(N), intent(in) :: array
character(20), intent(in) :: fileName
! Variables
integer :: i ! LCV
open(unit=1, file=fileName, form='formatted', ! Open file with name fileName to
+ action='write', status='new') ! written to.
do i = 1, N ! writes all values of array to
write(1, '(f18.10)') array(i) ! fileName
end do
close(unit = 1)
end subroutine WriteToFile
8 | P a g e
program main
implicit none
!DATA DICTIONARY
character, parameter :: FUNCTION = 'F' !The function among the three defined at
! the start of the program based on:
! if FUNCTION = F then use function f
! if FUNCTION = G then use function g
! if FUNCTION = H then use function h
real, parameter :: LEFT_BOUND = 0.2 !The left boundary to find the area under
real, parameter :: RIGHT_BOUND = 3.2 !The right boundary to find the area under
integer, parameter :: INTERVALS = 20 !The number of intervals to compute over
character, parameter :: TYPE = 'L' !The type of quadrature to use based on:
! TYPE = L then Left Point Approximation
! TYPE = R then Right Point Approximation
! TYPE = M then Mid Point Approximation
! TYPE = T then Trapezoid Approximation
! TYPE = S then Simpson Approximation
character(20), parameter :: FILE_NAME = "output.txt" !File name to output to
real, dimension(INTERVALS) :: output !Array holding the areas under each
! interval
integer :: i !LCV
!functions used
real, external :: f
real, external :: g
real, external :: h
!PROCESS
select case (FUNCTION) !Selects chosen function
case('F')
call GeneralQuadrature(f, LEFT_BOUND, RIGHT_BOUND,
+ INTERVALS, TYPE, output)
case('G')
call GeneralQuadrature(g, LEFT_BOUND, RIGHT_BOUND,
+ INTERVALS, TYPE, output)
case('H')
call GeneralQuadrature(h, LEFT_BOUND, RIGHT_BOUND,
+ INTERVALS, TYPE, output)
end select
!OUTPUT
call WriteToFile(output, FILE_NAME, INTERVALS)
write(*, *) "Area under function ", FUNCTION, " found from ",
+ LEFT_BOUND, " to ", RIGHT_BOUND
write(*, *) INTERVALS, " intervals used"
select case(TYPE) !Selects chosen technique to report
case('L')
write(*, *) "Left point approximation used"
case('R')
write(*, *) "Right point approximation used"
case('M')
write(*, *) "Midpoint approximation used"
case('T')
write(*, *) "Trapezoid approximation used"
case('S')
write(*, *) "Simpson approximation used"
end select
write(*, *) "Interval areas written to ", FILE_NAME
end program main
9 | P a g e
2.2. Sample Runs All sample runs runs use 20 intervals over the bounds [0.2, 3.2]
2.2.1. Function f(x) = (28/5) + 2sin(4x) - (1/x)
Left Point Approximation Area under function F found from 0.2 to 3.2
20 intervals used
Left point approximation used
Interval areas written to run1-1.txt
Right Point Approximation Area under function F found from 0.2 to 3.2
20 intervals used
Right point approximation used
Interval areas written to run1-2.txt
Midpoint Approximation Area under function F found from 0.2 to 3.2
20 intervals used
Midpoint approximation used
Interval areas written to run1-3.txt
Trapezoid Approximation Area under function F found from 0.2 to 3.2
20 intervals used
Trapezoid approximation used
Interval areas written to run1-4.txt
Simpson Approximation Area under function F found from 0.2 to 3.2
20 intervals used
Simpson approximation used
Interval areas written to run1-5.txt
2.2.2. Function g(x) = 2x + ln(5x)
Left Point Approximation Area under function G found from 0.2 to 3.2
20 intervals used
Left point approximation used
Interval areas written to run2-1.txt
Right Point Approximation Area under function G found from 0.2 to 3.2
20 intervals used
Right point approximation used
Interval areas written to run2-2.txt
Midpoint Approximation Area under function G found from 0.2 to 3.2
20 intervals used
Midpoint approximation used
Interval areas written to run2-3.txt
Trapezoid Approximation Area under function G found from 0.2 to 3.2
20 intervals used
Trapezoid approximation used
Interval areas written to run2-4.txt
Simpson Approximation Area under function G found from 0.2 to 3.2
20 intervals used
Simpson approximation used
Interval areas written to run2-5.txt
10 | P a g e
2.2.3. Function h(x) = (1/sqrt(2PI))*exp((-x^2)/2)
Left Point Approximation Area under function H found from 0.2 to 3.2
20 intervals used
Left point approximation used
Interval areas written to run3-1.txt
Right Point Approximation Area under function H found from 0.2 to 3.2
20 intervals used
Right point approximation used
Interval areas written to run3-2.txt
Midpoint Approximation Area under function H found from 0.2 to 3.2
20 intervals used
Midpoint approximation used
Interval areas written to run3-3.txt
Trapezoid Approximation Area under function H found from 0.2 to 3.2
20 intervals used
Trapezoid approximation used
Interval areas written to run3-4.txt
Simpson Approximation Area under function H found from 0.2 to 3.2
20 intervals used
Simpson approximation used
Interval areas written to run3-5.txt
11 | P a g e
2.3. Output Files All output files are generated using 20 intervals over the bounds [0.2, 3.2]
2.3.1. Function f(x) = (28/5) + 2sin(4x) - (1/x)
Run 1-1.txt: Left Point Approximation 0.3052068651
0.7070633769
0.8127894402
0.7638809681
0.6349878907
0.4985477626
0.4181556404
0.4323229790
0.5434771776
0.7182989717
0.8999986649
1.0285311937
1.0618081093
0.9905522466
0.8416485786
0.6688321233
0.5339591503
0.4854577780
0.5414276719
0.6833504438
Run 1-2.txt: Right Point Approximation 0.7070635557
0.8127890825
0.7638812661
0.6349876523
0.4985479712
0.4181556404
0.4323226213
0.5434775949
0.7182989717
0.8999986649
1.0285311937
1.0618072748
0.9905522466
0.8416498899
0.6688310504
0.5339599848
0.4854570031
0.5414276719
0.6833515167
0.8625771403
Run 1-3.txt: Midpoint Approximation 0.5619077682
0.7845581174
0.8028421402
0.7048781514
0.5633366108
0.4481752813
0.4123633504
0.4771125317
0.6261421442
0.8122282028
0.9741836190
1.0585281849
1.0383629799
0.9228909612
0.7542960644
0.5930731297
0.4969341755
0.5006971955
0.6041392088
0.7721026540
Run 1-4.txt: Trapezoid Approximation 0.5061352253
0.7599262595
0.7883353829
0.6994343400
0.5667679310
0.4583517015
0.4252391458
0.4879002869
0.6308880448
0.8091487885
0.9642648697
1.0451692343
1.0261801481
0.9161010385
0.7552398443
0.6013960838
0.5097081065
0.5134427547
0.6123896241
0.7729638219
Run 1-5.txt: Simpson Approximation 0.5433169603
0.7763475180
0.7980065942
0.7030635476
0.5644804239
0.4515674412
0.4166553020
0.4807084501
0.6277241111
0.8112017512
0.9708774090
1.0540752411
1.0343021154
0.9206276536
0.7546106577
0.5958474874
0.5011921525
0.5049457550
0.6068893671
0.7723897099
12 | P a g e
2.3.2. Function g(x) = 2x + ln(5x)
Run 2-1.txt: Left Point Approximation 0.0600000098
0.1889423579
0.2874436677
0.3717982173
0.4479442537
0.5187216401
0.5857120752
0.6498876214
0.7118864655
0.7721538544
0.8310098052
0.8886933923
0.9453883767
1.0012365580
1.0563510656
1.1108295918
1.1647413969
1.2181565762
1.2711231709
1.3236857653
Run 2-2.txt: Right Point Approximation 0.1889424175
0.2874435782
0.3717983663
0.4479440749
0.5187218189
0.5857120752
0.6498871446
0.7118870616
0.7721538544
0.8310098052
0.8886933923
0.9453876019
1.0012365580
1.0563527346
1.1108279228
1.1647431850
1.2181545496
1.2711231709
1.3236879110
1.3758869171
Run 2-3.txt: Midpoint Approximation 0.1302680820
0.2405657321
0.3309080005
0.4106781185
0.4838860631
0.5526195168
0.6181058288
0.6811280251
0.7422143221
0.8017417789
0.8599855900
0.9171544313
0.9734104872
1.0288798809
1.0836642981
1.1378525496
1.1915071011
1.2446928024
1.2974531651
1.3498296738
Run 2-4.txt: Trapezoid Approximation 0.1244712099
0.2381929606
0.3296210170
0.4098711312
0.4833330214
0.5522168875
0.6177996397
0.6808873415
0.7420201898
0.8015818000
0.8598515987
0.9170405269
0.9733124375
1.0287946463
1.0835894346
1.1377863884
1.1914479733
1.2446398735
1.2974054813
1.3497864008
Run 2-5.txt: Simpson Approximation 0.1283357888
0.2397748083
0.3304790258
0.4104091227
0.4837017357
0.5524853468
0.6180037856
0.6810477972
0.7421496511
0.8016884923
0.8599409461
0.9171164632
0.9733778238
1.0288515091
1.0836393833
1.1378304958
1.1914874315
1.2446751595
1.2974373102
1.3498152494
13 | P a g e
2.3.3. Function h(x) = (1/sqrt(2PI))*exp((-x^2)/2)
Run 3-1.txt: Left Point Approximation 0.0586564206
0.0562860481
0.0528098159
0.0484458506
0.0434537455
0.0381088555
0.0326778218
0.0273973830
0.0224591158
0.0180013478
0.0141073596
0.0108097298
0.0080986507
0.0059325090
0.0042490498
0.0029756054
0.0020374430
0.0013640354
0.0008928802
0.0005714637
Run 3-2.txt: Right Point Approximation 0.0562860668
0.0528097935
0.0484458692
0.0434537269
0.0381088704
0.0326778218
0.0273973607
0.0224591345
0.0180013478
0.0141073596
0.0108097298
0.0080986442
0.0059325090
0.0042490568
0.0029756008
0.0020374462
0.0013640333
0.0008928802
0.0005714645
0.0003576128
Run 3-3.txt: Midpoint Approximation 0.0576208532
0.0546737760
0.0507232621
0.0460111685
0.0408082642
0.0353883989
0.0300056059
0.0248755403
0.0201637018
0.0159807391
0.0123837376
0.0093828570
0.0069509964
0.0050348546
0.0035657820
0.0024691764
0.0016717701
0.0011067025
0.0007163291
0.0004533383
Run 3-4.txt: Trapezoid Approximation 0.0574712418
0.0545479208
0.0506278425
0.0459497906
0.0407813080
0.0353933387
0.0300375931
0.0249282587
0.0202302318
0.0160543546
0.0124585452
0.0094541870
0.0070155798
0.0050907829
0.0036123253
0.0025065260
0.0017007381
0.0011284578
0.0007321724
0.0004645382
Run 3-5.txt: Simpson Approximation 0.0575709827
0.0546318255
0.0506914556
0.0459907092
0.0407992788
0.0353900455
0.0300162695
0.0248931143
0.0201858785
0.0160052776
0.0124086738
0.0094066337
0.0069725243
0.0050534974
0.0035812964
0.0024816263
0.0016814262
0.0011139543
0.0007216103
0.0004570716
14 | P a g e
3. Analysis of Total Approximate Areas
This section contains a comparison of the five quadrature techniques using intervals of lengths 5, 10,
25, 50 and 100. The comparison is conducted over functions f and g, and the results are measured
against an analytically calculated answer. The results found in this section are generated using the code
from the previous section with a slightly modified main program block, which computes the total area
rather than writing the individual interval areas to an external file.
3.1. Code Modified main program block
program main
implicit none
!DATA DICTIONARY
character, parameter :: FUNCTION = 'F' !The function among the three defined at
! the start of the program based on:
! if FUNCTION = F then use function f
! if FUNCTION = G then use function g
! if FUNCTION = H then use function h
real, parameter :: LEFT_BOUND = 0.2 !The left boundary to find the area under
real, parameter :: RIGHT_BOUND = 3.2 !The right boundary to find the area under
integer, parameter :: INTERVALS = 5 !The number of intervals to compute over
character, parameter :: TYPE = 'L' !The type of quadrature to use based on:
! TYPE = L then Left Point Approximation
! TYPE = R then Right Point Approximation
! TYPE = M then Mid Point Approximation
! TYPE = T then Trapezoid Approximation
! TYPE = S then Simpson Approximation
real, dimension(INTERVALS) :: output !Array holding the areas under each
! interval
real :: area !Total area of all the values in output
integer :: i !LCV
!functions used
real, external :: f
real, external :: g
real, external :: h
!PROCESS
select case (FUNCTION) !Selects chosen function
case('F')
call GeneralQuadrature(f, LEFT_BOUND, RIGHT_BOUND,
+ INTERVALS, TYPE, output)
case('G')
call GeneralQuadrature(g, LEFT_BOUND, RIGHT_BOUND,
+ INTERVALS, TYPE, output)
case('H')
call GeneralQuadrature(h, LEFT_BOUND, RIGHT_BOUND,
+ INTERVALS, TYPE, output)
end select
area = 0
do i=1, INTERVALS, 1 !Adds up the areas of each interval
area = area + output(i)
end do
15 | P a g e
!OUTPUT
write(*, *) "Area under function ", FUNCTION, " found from ",
+ LEFT_BOUND, " to ", RIGHT_BOUND
write(*, *) INTERVALS, " intervals used"
select case(TYPE) !Selects chosen technique to report
case('L')
write(*, *) "Left point approximation used"
case('R')
write(*, *) "Right point approximation used"
case('M')
write(*, *) "Midpoint approximation used"
case('T')
write(*, *) "Trapezoid approximation used"
case('S')
write(*, *) "Simpson approximation used"
end select
write(*, *) ""
write(*, *) "The area under the curve is: ", area
end program main
3.2. Data Tables
3.2.1. Function f(x) = (28/5) + 2sin(4x) - (1/x)
The following table shows the total areas computed under f(x) over the bounds [0.2, 3.2] for each quadrature technique, with
differing numbers of intervals (represented by n)
Area under function f(x)
n=5 n=10 n=25 n=50 n=100
Actual Area Computed Analytically 13.88934835
Left Sum Approximation 12.317757 13.186918 13.640118 13.771131 13.831914
Right Sun Approximation 14.547241 14.301661 14.086015 13.994081 13.943389
Midpoint Rule Approximation 14.056082 13.953675 13.902144 13.892695 13.890193
Trapezoid Rule Approximation 13.432498 13.744289 13.863067 13.882606 13.887651
Simpsons Rule Approximation 13.848221 13.88388 13.889118 13.889332 13.889345
3.2.2. Function g(x) = 2x + ln(5x)
The following table shows the total areas computed under g(x) over the bounds [0.2, 3.2] for each quadrature technique, with
differing numbers of intervals (represented by n)
Area under function g(x)
n=5 n=10 n=25 n=50 n=100
Actual Area Computed Analytically 16.07228391
Left Sum Approximation 13.319844 14.723202 15.540371 15.807707 15.940345
Right Sun Approximation 18.583397 17.354979 16.593079 16.334063 16.203522
Midpoint Rule Approximation 16.12656 16.08821 16.075039 16.072983 16.072458
Trapezoid Rule Approximation 15.951621 16.039091 16.066725 16.07088 16.071934
Simpsons Rule Approximation 16.068247 16.071838 16.072268 16.072283 16.072283
16 | P a g e
3.3. Discussion
The previous page contains a whole lot of numbers, which do not seem very meaningful without
any analysis. The first step to analyzing our results will be creating a table of the percent error of each
technique with each of their intervals. In the error computation, we will use the analytically computed
area as our exact value and the area computed by our quadrature as the approximate value.
%Error for function f(x)
n=5 n=10 n=25 n=50 n=100
Actual Area Computed Analytically Exact Value
Left Sum Approximation -11.31% -5.06% -1.74% -0.85% -0.41%
Right Sun Approximation 4.74% 2.67% 1.42% 0.75% 0.39%
Midpoint Rule Approximation 1.20% 0.46% 0.09% 0.02% 0.006%
Trapezoid Rule Approximation -3.29% -1.04% -0.19% -0.05% -0.01%
Simpsons Rule Approximation -0.30% -0.04% -0.002% -0.0001% -0.00002%
%Error for function g(x)
n=5 n=10 n=25 n=50 n=100
Actual Area Computed Analytically Exact Value
Left Sum Approximation -17.13% -8.39% -3.31% -1.65% -0.82%
Right Sun Approximation 15.62% 7.98% 3.24% 1.65% 0.82%
Midpoint Rule Approximation 0.34% 0.10% 0.02% 0.004% 0.001%
Trapezoid Rule Approximation -0.75% -0.21% -0.03% -0.009% -0.002%
Simpsons Rule Approximation -0.03 -0.003% -0.0001% -0.000006% -0.000006%
Using the above tables, we have two tests cases to judge to accuracy of the five quadrature
techniques, as well as the effect the number of intervals has on that accuracy. The two most obvious
results we can draw from this data is that: first, more intervals result in less error and second, The
Simpsons Rule Approximation is the most accurate approximation. It may also be easy to notice that in
both cases The Left Sum Approximation is less than the exact value while the Right Sum
Approximation is greater than the exact value, and that both produce a considerably large error.
However, if we examine the data some more, a few more interesting trends come up. For example,
in both test cases, the exact value is always between the Midpoint Rule Approximation and the
Trapezoid Rule Approximation; furthermore, in both cases the Midpoint Rule Approximation is greater
than the exact value, while the Trapezoid Rule Approximation is less than the exact value. Additionally
the Trapezoid Rule Approximation yields a larger error than the Midpoint Rule Approximation; this is
perhaps why the Simpsons Rule Approximation uses two parts Midpoint and one part Trapezoid in its
weighted average
17 | P a g e
4. Analysis of Subinterval Areas
In the previous section, we calculated the error generated by each quadrature technique using
different numbers of intervals. In this section we will use a fixed number of intervals and use our results
produced by our Fortran code along with Matlab to calculate the error generated by each individual
interval.
In this section we also use a modified main program block, which produces 5 output files. Each file
uses one of the five quadrature techniques, computing the area under f(x) using 10 intervals over the
bounds [0.2, 3.2]. These results will be compared to those generated in Matlab to calculate error
4.1. Code Modified main program block
program main
implicit none
!DATA DICTIONARY
real, parameter :: LEFT_BOUND = 0.2 !The left boundary to find the area under
real, parameter :: RIGHT_BOUND = 3.2 !The right boundary to find the area under
integer, parameter :: INTERVALS = 10 !The number of intervals to compute over
real, dimension(INTERVALS) :: output !Array holding the areas under each
! interval
!functions used
real, external :: f
!PROCESS & OUTPUT
call GeneralQuadrature(f, LEFT_BOUND, RIGHT_BOUND,
+ INTERVALS, 'L', output)
call WriteToFile(output, "fL.dat", INTERVALS)
call GeneralQuadrature(f, LEFT_BOUND, RIGHT_BOUND,
+ INTERVALS, 'R', output)
call WriteToFile(output, "fR.dat", INTERVALS)
call GeneralQuadrature(f, LEFT_BOUND, RIGHT_BOUND,
+ INTERVALS, 'M', output)
call WriteToFile(output, "fM.dat", INTERVALS)
call GeneralQuadrature(f, LEFT_BOUND, RIGHT_BOUND,
+ INTERVALS, 'T', output)
call WriteToFile(output, "fT.dat", INTERVALS)
call GeneralQuadrature(f, LEFT_BOUND, RIGHT_BOUND,
+ INTERVALS, 'S', output)
call WriteToFile(output, "fS.dat", INTERVALS)
end program main
18 | P a g e
4.2. Matlab code x = [0.2:0.01:3.2]; % Creates a vector from
% 0.2 to 3.2 by 0.01
y = ((28/5) + 2*sin(4*x) - (1./x)); % Creates vector for
% the function value at
% each point from x vector
plot(x,y),title('Preliminary Analysis'),xlabel('x'),ylabel('f(x)') % Creates a graph x vs. f(x)
x2 = linspace(0.2,3.2,11); % Creates a vector with 11
% values between 0.2 and
% 3.2
y2 = (28/5) + 2*sin(4*(x2)) - 1./(x2); % Creates a vector for the
% function values at each
% value in x2 vector
hold on % Stops new plot from being
% generated
plot(x2,y2,'r+') % Plots points unconnected
% marked with "+".
x3 = x2(:, 1:10); % Cuts off last end point
% from x2 vector
b = x3 + 0.3; % Right point
% Computes area of each subinterval.
areas = (28/5)*b - .5*cos(4*b) - log(b) - ((28/5)*x3 - .5*cos(4*x3) - log(x3));
% Used to compare with analytically calculated value.
sumAreas = sum(areas)
4.3. Matlab output intervalAreas =
1.0000 1.3201
2.0000 1.5011
3.0000 1.0161
4.0000 0.8974
5.0000 1.4389
6.0000 2.0249
7.0000 1.9549
8.0000 1.3505
9.0000 1.0062
10.0000 1.3793
sumAreas =
13.8893
19 | P a g e
4.4. Plots
20 | P a g e
4.5. Data Table
Interval Left Sum Error Right Sum Error Midpoint Rule Error Trapezoid Rule Error Simpson's Rule Error
0.2 0.5 0.709686389 -0.305478404 -0.094026992 0.202103903 0.004683306
0.5 0.8 -0.124478523 0.231124457 -0.026662413 0.053322967 -6.594E-07
0.8 1.1 -0.253875543 0.179788541 0.019004296 -0.037043501 0.000321697
1.1 1.4 0.061088362 -0.189554713 0.0327544 -0.064233146 0.000425198
1.4 1.7 0.351945645 -0.36109733 0.002302057 -0.004575842 9.4236E-06
1.7 2.0 0.22490267 -0.09871455 -0.032162387 0.06309406 -0.000410278
2.0 2.3 -0.168716219 0.27160022 -0.026204493 0.051442001 -0.000322368
2.3 2.6 -0.332798469 0.282580865 0.012837422 -0.025108802 0.000188602
2.6 2.9 -0.061719135 -0.076654509 0.035285219 -0.069186882 0.000461146
2.9 3.2 0.296445491 -0.345855592 0.012598039 -0.024705051 0.000163676
4.6. Discussion
In this component, we analyze the accuracy of the Fortran component of the five approximation
types: Left Sum, Right Sum, Midpoint Rule, Trapezoid Rule and Simpsons Rule. As you can interpret
from the data chart and graphs above you can see which approximation types yielded larger error from
the actual value.
The right sum approximation seemed to yield the largest error, followed by the left sum
approximation; these two had opposing results as you can see in the bar graphs. There was a similar
pattern for the trapezoid and midpoint rule approximations. These two approximation types yielded
opposite numerical values, which were also closely related. Lastly, Simpsons Rule approximation was,
without a doubt, the most efficient. This had the smallest error by as much as six orders of magnitude in
comparison to the other approximations at certain intervals. The cause of these errors can be further
examined by looking at a visual representation of the intervals
The above graphs show the ten intervals superimposed onto f(x), with the green representing
area correctly measured, the red representing area not measured, and the yellow representing excess area
measured. As you can see, intervals with red area measure negative error on the bar graphs, and intervals
with yellow area measure positive error on the bar graph. This clearly illustrates why the left and right
sum approximations yield their respective errors, and a similar visualization of the Midpoint or
Trapezoid approximation would likewise illustrate how they come to their errors, which are far smaller
than the left and right point approximations.
To conclude, the results show the Simpsons Rule Approximation, as expected, had the least
amount of error, as it takes the weighted average of the more accurate Midpoint and Trapezoid
Approximations.
21 | P a g e
5. Conclusion
Our five quadrature techniques, Right Point Approximation, Left Point Approximation, Midpoint
Approximation, Trapezoid Approximation and Simpsons Rule have all proven to displaying varying
degrees of accuracy depending on the parameters given to them. In the second section of our report, we
displayed the code used to generate our data and several sample runs used to test the functionality of our
program. Of particular note are the parameters of the estimation we control through this program,
including the function to integrate, the bounds to integrate over, the number of intervals to use in the
estimation, the type of quadrature technique to use, and the file location to write our data to.
In the third section of this report, we analyze the effectiveness of each technique with varying
numbers of intervals by comparing the total areas they produce against each other, as well against an
analytically calculated value. These results showed, without a doubt, that a greater number of intervals
result in higher accuracy, and that the Simpsons Rule Approximation was significantly more accurate
than any of the other four techniques. This data also showed that the Left Sum Approximation and
Trapezoid Rule Approximation tend to underestimate the area, while the Right Sum Approximation and
Midpoint Rule Approximation tend to overestimate the area.
Finally, the fourth section of the report examines the accuracy of each individual interval for the
five techniques as they make their estimations with ten intervals. The data found in this section further
supports to conclusions in the previous section; that Simpsons Rule is most accurate, and that where
estimations are underestimated by the Left Sum Approximations and Trapezoid Approximations they
are overestimated by the Right Sum Approximation and Midpoint Rule Approximation. It can further
be concluded that the Midpoint Rule Approximation is roughly two times more effective than the
Trapezoid Rule Approximation, and that may be why Simpsons Rule Approximation chooses its
weighted average in favor of the Midpoint Rule Approximation.
In conclusion, while computers may not be as capable in the dynamic thinking we humans are,
they can crunch numbers exceedingly well. And in this, if given the optimal technique and a large
enough number of iterations, they can come to awfully precise results.
Recommended