Ae23b103 AS2101exp05
Ae23b103 AS2101exp05
AEROSPACE ENGINEERING
Indian Institute Of Technology, Madras
Contents
1 Abstract 2
2 Aim 2
3 Introduction 2
4 Theory 3
4.1 Numerical methods of integration . . . . . . . . . . . . . . . . . . 3
4.1.1 Right Endpoint Method . . . . . . . . . . . . . . . . . . . 3
4.1.2 Left End Point Method . . . . . . . . . . . . . . . . . . . 4
4.1.3 Middle Point Method . . . . . . . . . . . . . . . . . . . . 6
4.1.4 Trapezoidal method . . . . . . . . . . . . . . . . . . . . . 7
4.1.5 Simpson’s 1/3 Rule . . . . . . . . . . . . . . . . . . . . . . 9
4.1.6 Simpson’s 3/8 Rule . . . . . . . . . . . . . . . . . . . . . . 10
4.2 Error Calculation . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.2.1 Right Endpoint Method - Error Calculation . . . . . . . . 11
4.2.2 Left Endpoint Method - Error Calculation . . . . . . . . 12
4.2.3 Midpoint Method - Error Calculation . . . . . . . . . . . 12
4.2.4 Trapezoidal Method - Error Calculation . . . . . . . . . . 12
4.2.5 Simpson’s 1/3 Rule - Error Calculation . . . . . . . . . . 12
4.2.6 Absolute Error . . . . . . . . . . . . . . . . . . . . . . . . 13
5 Results 13
5.1 Absolute Error vs Step size . . . . . . . . . . . . . . . . . . . . . 13
5.2 Logarithm of Absolute error vs Logarithm of Interval size . . . . 14
5.3 Computational time vs Step size . . . . . . . . . . . . . . . . . . 15
6 Conclusion 15
7 Appendix 16
List of Figures
1 Caption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1
Report 3 INTRODUCTION
1 Abstract
The report presents various numerical integration methods for approximating
the given function, along with their corresponding absolute errors, logarithms
of the errors, polynomial approximations, and computational cost graphs. The
results indicate that Simpson’s rule provides a highly accurate approximation
for continuous functions as the number of intervals increases, outperforming the
other methods analyzed.
2 Aim
To explore various numerical integration techniques, generate error plots and
polynomial regression plots for the given function at increasing interval sizes.
Analyze the results obtained from each integration method and evaluate their
accuracy based on the plotted data.
3 Introduction
The function f(x) = sin(2x) was integrated over the interval [0, π2 ] using five nu-
merical methods: Right Endpoint, Left Endpoint, Midpoint, Trapezoidal, and
Simpson’s Rule.
Integration was computed for various interval sizes to observe the behavior
of each method as the number of intervals increased. Absolute and logarithmic
error plots were generated to evaluate the accuracy of these methods.
The calculations were performed in Octave, utilizing for loops, while loops,
and if statements to determine the summation values and adjust interval sizes.
The results were visualized using the ‘plot‘ function. Additionally, the com-
putational cost in terms of time for each method was recorded and plotted to
compare their efficiency.
By plotting both error and computational time, the trade-off between accu-
racy and speed for each method was assessed. This allowed for a clear analysis
of how each method performs, especially as the number of intervals approaches
infinity.
Finally, based on the error analysis and computational cost, conclusions were
drawn regarding the best numerical integration technique for the given function,
balancing both precision and computational efficiency.
2
Report 4 THEORY
4 Theory
In various practical applications, integration of functions are required. How-
ever, more often than not, anti-derivative of many functions either cannot be
expressed or cannot be expressed easily in closed form. Thus, rather than de-
riving the integral of these complex functions directly, we can utilise various
techniques of numerical integration to approximate their values with marginal
errors.
3. Midpoint Method
4. Trapezoidal Method
5. Simpson’s 1/3 Rule
6. Simpson’s 3/8 Rule
3
Report 4 THEORY
We divide the interval [a, b] into n equally spaced intervals of length ∆x,
which is given by
b−a
∆x =
n
Thus,
b−a
xi − xi−1 =
n
for i = 1, 2, 3, . . .. Thus utilizing this expression,
xi = x0 + i∆x
where x0 = a and i = 1, 2, 3, 4, . . ..
According to the Left Endpoint Approximation rule, we construct a rectangle
in the interval [xi−1 , xi ], with the height of the rectangle being f (xi−1 ), i.e., the
value of the function at the left endpoint of the interval. Then, the area of each
rectangle is f (xi−1 )∆x, and the approximation of the integral is given by
4
Report 4 THEORY
this approximation, the value of the integral is equal to summation of all the
rectangles formed.
We divide the interval [a, b] into n equally spaced intervals of length ∆x,
which is given by
b−a
∆x =
n
Thus,
b−a
xi − xi−1 =
n
for i = 1, 2, 3, . . .. Utilizing this expression, we have
xi = x0 + i∆x
where x0 = a and i = 1, 2, 3, 4, . . ..
5
Report 4 THEORY
f (xi−1 )∆x
and the approximation of the integral is given by
Figure 1: Caption
6
Report 4 THEORY
We divide the interval [a, b] into n equally spaced intervals of length ∆x,
which is given by
b−a
∆x =
n
Thus,
b−a
xi − xi−1 =
n
for i = 1, 2, 3, . . .. Thus, utilizing this expression,
xi = x0 + i∆x
where x0 = a and i = 1, 2, 3, 4, . . ..
According to the Midpoint Approximation rule, we construct a rectangle in
the interval [xi−1 , xi ], with the height of the rectangle being
xi−1 + xi
f
2
i.e., the value of the function at the midpoint of the interval. Then, the area
of each rectangle is
xi−1 + xi
f ∆x
2
and the approximation of the integral is given by
x0 + x1 x1 + x2 xn−1 + xn
I≈f ∆x + f ∆x + . . . + f ∆x
2 2 2
Thus, we have
Z b n−1
X xi−1 + xi
f (x) dx ≈ f ∆x
a i=0
2
7
Report 4 THEORY
We divide the interval [a, b] into n equally spaced intervals of length ∆x,
which is given by
b−a
∆x =
n
Thus,
b−a
xi − xi−1 =
n
for i = 1, 2, 3, . . .. Utilizing this expression, we have
xi = x0 + i∆x
where x0 = a and i = 1, 2, 3, 4, . . ..
According to the Trapezoidal Approximation rule, we construct a trapezoid
in the interval [xi−1 , xi ] by joining the endpoints f (xi−1 ) and f (xi ) with a
straight line. Then, the area of each trapezoid is given by
8
Report 4 THEORY
To apply Simpson’s 1/3 Rule, we divide the interval into n equally spaced
subintervals, where n must be an even number. We divide the interval [a, b] into
n equally spaced intervals, each of width ∆x, where:
b−a
∆x =
n
Thus, for each i = 1, 2, 3, . . . , n, we define the points:
xi = x0 + i∆x
where x0 = a and xn = b.
9
Report 4 THEORY
∆x
I≈ [f (x0 ) + 4f (x1 ) + 2f (x2 ) + 4f (x3 ) + · · · + 4f (xn−1 ) + f (xn )]
3
The alternating factors of 4 and 2 apply to the function values at the odd and
even points respectively, except for the first and last points, which are weighted
as 1.
In this rule: - f (x0 ) and f (xn ) are the function values at the endpoints. -
f (x1 ), f (x3 ), . . . , f (xn−1 ) are the function values at the odd-numbered points,
and these are multiplied by 4. - f (x2 ), f (x4 ), . . . , f (xn−2 ) are the function values
at the even-numbered points, and these are multiplied by 2.
Therefore, the integral can be approximated as:
" #
Z b
∆x X X
f (x) dx ≈ f (x0 ) + 4 f (xi ) + 2 f (xi ) + f (xn )
a 3 even i
odd i
To apply Simpson’s 3/8 Rule, we divide the interval [a, b] into n equal subin-
tervals, where n is a multiple of 3. The width of each subinterval is:
10
Report 4 THEORY
b−a
∆x =
n
Thus, for each i = 1, 2, 3, . . . , n, the points in the interval are defined as:
xi = x0 + i∆x
where x0 = a and xn = b.
Simpson’s 3/8 Rule approximates the integral by fitting cubic polynomials
through four points, and the formula is given by:
3∆x
I≈ [f (x0 ) + 3f (x1 ) + 3f (x2 ) + 2f (x3 ) + 3f (x4 ) + 3f (x5 ) + · · · + 3f (xn−1 ) + f (xn )]
8
This method applies alternating factors of 3 and 2 to the function values at
different points within the interval.
Z b
3∆x X X
f (x) dx ≈ f (x0 ) + 3 f (xi ) + 2 f (xi ) + f (xn )
a 8
odd i every third i
e = |I ′ − I|
11
Report 4 THEORY
where:
Pn
I ′ = i=1 f (xi )∆x is the approximated integral using the Right Endpoint
Method.
I is the true value of the integral.
e = |I ′ − I|
where:
Pn−1
I ′ = i=0 f (xi )∆x is the approximated integral using the Left Endpoint
Method.
I is the true value of the integral.
e = |I ′ − I|
where:
Pn
I ′ = i=1 f xi−12+xi ∆x is the approximated integral using the Midpoint
Method.
I is the true value of the integral.
e = |I ′ − I|
where:
I ′ = ∆x
2 (f (x0 ) + 2f (x1 ) + · · · + 2f (xn−1 ) + f (xn )) is the approximated in-
tegral using the Trapezoidal Method.
I is the true value of the integral.
e = |I ′ − I|
12
Report 5 RESULTS
where:
I ′ = ∆x
3 (f (x0 ) + 4f (x1 ) + 2f (x2 ) + · · · + 4f (xn−1 ) + f (xn )) is the approxi-
mated integral using Simpson’s 1/3 Rule.
I is the true value of the integral.
e = |Itrue − Iapprox |
where:
Itrue is the true value of the integral and Iapprox is the approximate value ob-
tained through a numerical method.
5 Results
5.1 Absolute Error vs Step size
The graphs obtained by different methods is shown below:
(a) Right End Point (b) Left End Point (c) Mid Point
(d) Trapezoidal (e) Simpson 1/3 Rule (f) Simpson 13/8 Rule
Observations
1. The graph obtained for REP, LEP and Trapezoidal approximation are
almost similar.
13
Report 5 RESULTS
(a) Right End Point (b) Left End Point (c) Mid Point
(d) Trapezoidal (e) Simpson 1/3 Rule (f) Simpson 13/8 Rule
Observations
1. The graph obtained for the logarithm of absolute error versus the loga-
rithm of interval size is approximately a straight line for all the approxi-
mation methods.
14
Report 6 CONCLUSION
(a) Right End Point (b) Left End Point (c) Mid Point
(d) Trapezoidal (e) Simpson 1/3 Rule (f) Simpson 13/8 Rule
Observations
1. The computational time for the Right Endpoint (REP) and Left Endpoint
(LEP) methods is the lowest, while Simpson’s method has the highest
computational time.
2. Despite the similarity in the curves of the Trapezoidal approximation and
REP and LEP methods, there is a significant difference in the computa-
tional time between these methods.
3. The Midpoint method offers a good balance between computational cost
and accuracy, performing better than REP and LEP while requiring less
computational time than Simpson’s method.
6 Conclusion
1. Numerical integration methods allow us to estimate the value of an integral
based on discrete data points, without requiring the exact solution of the
function. By defining the integral as the limit of a Riemann sum, we can
ensure that as more data points are used, the estimated integral of the
model function converges to the true integral of the original function. In
this report, five numerical integration methods were evaluated: the left
15
Report 7 APPENDIX
7 Appendix
Right Endpoint Method
clc ; clear all ; close all ;
% Define the i n t e r v a l [ a , b ]
a = 0 ; % Left endpoint
b = p i / 2 ; % Right e n d p o i n t
% Range o f number o f s u b i n t e r v a l s
for n = 1:100
tic ;
% Width o f each s u b i n t e r v a l
dx = ( b − a ) / n ;
% C a l c u l a t e t h e r i g h t Riemann sum
for i = 1:n
x i r = a + i ∗ dx ; % Right e n d p o i n t o f t h e c u r r e n t s u b i n t e r v a l
sum = sum + s i n ( 2 ∗ x i r ) ∗ dx ; % Area o f t h e r e c t a n g l e
endfor
% Compute t h e a b s o l u t e e r r o r
d i f f = abs ( 1 − sum ) ;
16
Report 7 APPENDIX
% Store r e s u l t s
i n t e g r a l = [ i n t e g r a l ; sum ] ;
y = [y; diff ];
x = [ x ; dx ] ;
time = [ time ; t o c ] ;
endfor
% Trim a r r a y s t o remove i n i t i a l z e r o e n t r i e s
i n t e g r a l = i n t e g r a l ( 2 : end ) ;
y = y ( 2 : end ) ;
x = x ( 2 : end ) ;
time = time ( 2 : end ) ;
% P l o t a b s o l u t e e r r o r vs i n t e r v a l s i z e
figure ();
p l o t ( x , y , ’ b − ’ , ’ LineWidth ’ , 0 . 5 ) ;
x l a b e l ( ’ I n t e r v a l s i z e ( dx ) ’ ) ;
y l a b e l ( ’ Absolute e r r o r of i n t e g r a l ’ ) ;
t i t l e ( ’REP: A b s o l u t e e r r o r vs dx ’ ) ;
g r i d on ;
% P l o t l o g −l o g s c a l e o f a b s o l u t e e r r o r vs i n t e r v a l s i z e
figure ();
p l o t ( l o g ( x ) , l o g ( y ) , ’ b − ’ , ’ LineWidth ’ , 0 . 5 ) ;
x l a b e l ( ’ Logarithm o f i n t e r v a l s i z e ’ ) ;
y l a b e l ( ’ Logarithm o f a b s o l u t e e r r o r o f i n t e g r a l ’ ) ;
t i t l e ( ’REP: Log ( a b s o l u t e e r r o r ) vs Log ( dx ) ’ ) ;
g r i d on ;
% P l o t c o m p u t a t i o n a l time vs i n t e r v a l s i z e
figure ();
p l o t ( x , time , ’ b − ’ , ’ LineWidth ’ , 0 . 5 ) ;
x l a b e l ( ’ I n t e r v a l s i z e ( dx ) ’ ) ;
y l a b e l ( ’ Computational time ( s e c o n d s ) ’ ) ;
t i t l e ( ’REP: Computational time vs dx ’ ) ;
g r i d on ;
Left Endpoint Method
clc ; clear all ; close all ;
% Define the i n t e r v a l [ a , b ]
a = 0 ; % Left endpoint
b = p i / 2 ; % Right e n d p o i n t
17
Report 7 APPENDIX
x = [0];
integral = [ 0 ] ;
time = [ 0 ] ;
% Range o f number o f s u b i n t e r v a l s
for n = 1:100
tic ;
% Width o f each s u b i n t e r v a l
dx = ( b − a ) / n ;
% C a l c u l a t e t h e l e f t Riemann sum
for i = 1:n
x i l = a + ( i − 1 ) ∗ dx ; % L e f t e n d p o i n t o f t h e c u r r e n t s u b i n t e r v a l
sum = sum + s i n ( 2 ∗ x i l ) ∗ dx ; % Area o f t h e r e c t a n g l e
endfor
% Compute t h e a b s o l u t e e r r o r
d i f f = abs ( 1 − sum ) ;
% Store r e s u l t s
i n t e g r a l = [ i n t e g r a l ; sum ] ;
y = [y; diff ];
x = [ x ; dx ] ;
time = [ time ; t o c ] ;
endfor
% Trim a r r a y s t o remove i n i t i a l z e r o e n t r i e s
i n t e g r a l = i n t e g r a l ( 2 : end ) ;
y = y ( 2 : end ) ;
x = x ( 2 : end ) ;
time = time ( 2 : end ) ;
% P l o t a b s o l u t e e r r o r vs i n t e r v a l s i z e
figure ();
p l o t ( x , y , ’ b − ’ , ’ LineWidth ’ , 0 . 5 ) ;
x l a b e l ( ’ I n t e r v a l s i z e ( dx ) ’ ) ;
y l a b e l ( ’ Absolute e r r o r of i n t e g r a l ’ ) ;
t i t l e ( ’ A b s o l u t e e r r o r vs dx ’ ) ;
g r i d on ;
% P l o t l o g −l o g s c a l e o f a b s o l u t e e r r o r vs i n t e r v a l s i z e
figure ();
p l o t ( l o g ( x ) , l o g ( y ) , ’ b − ’ , ’ LineWidth ’ , 0 . 5 ) ;
18
Report 7 APPENDIX
x l a b e l ( ’ Logarithm o f i n t e r v a l s i z e ’ ) ;
y l a b e l ( ’ Logarithm o f a b s o l u t e e r r o r o f i n t e g r a l ’ ) ;
t i t l e ( ’LEP : Log ( a b s o l u t e e r r o r ) vs Log ( dx ) ’ ) ;
g r i d on ;
% P l o t c o m p u t a t i o n a l time vs i n t e r v a l s i z e
figure ();
p l o t ( x , time , ’ b − ’ , ’ LineWidth ’ , 0 . 5 ) ;
x l a b e l ( ’ I n t e r v a l s i z e ( dx ) ’ ) ;
y l a b e l ( ’ Computational time ( s e c o n d s ) ’ ) ;
t i t l e ( ’LEP : Computational time vs dx ’ ) ;
g r i d on ;
Midpoint Method
clear all ; close all ; clc ;
% Define the i n t e r v a l [ a , b ]
a = 0 ; % Left endpoint
b = p i / 2 ; % Right e n d p o i n t
% Range o f number o f s u b i n t e r v a l s
for n = 1:100
tic ;
% Width o f each s u b i n t e r v a l
dx = ( b − a ) / n ;
% Compute t h e a b s o l u t e e r r o r
d i f f = abs ( 1 − sum ) ;
19
Report 7 APPENDIX
% Store r e s u l t s
i n t e g r a l = [ i n t e g r a l ; sum ] ;
y = [y; diff ];
x = [ x ; dx ] ;
time = [ time ; t o c ] ;
endfor
% Trim a r r a y s t o remove i n i t i a l z e r o e n t r i e s
i n t e g r a l = i n t e g r a l ( 2 : end ) ;
y = y ( 2 : end ) ;
x = x ( 2 : end ) ;
time = time ( 2 : end ) ;
% P l o t a b s o l u t e e r r o r vs i n t e r v a l s i z e
figure ();
p l o t ( x , y , ’ b − ’ , ’ LineWidth ’ , 0 . 5 ) ;
x l a b e l ( ’ I n t e r v a l s i z e ( dx ) ’ ) ;
y l a b e l ( ’ Absolute e r r o r of i n t e g r a l ’ ) ;
t i t l e ( ’ A b s o l u t e E r r o r vs dx ’ ) ;
g r i d on ;
% P l o t l o g −l o g s c a l e o f a b s o l u t e e r r o r vs i n t e r v a l s i z e
figure ();
p l o t ( l o g ( x ) , l o g ( y ) , ’ b − ’ , ’ LineWidth ’ , 0 . 5 ) ;
x l a b e l ( ’ Logarithm o f i n t e r v a l s i z e ’ ) ;
y l a b e l ( ’ Logarithm o f a b s o l u t e e r r o r o f i n t e g r a l ’ ) ;
t i t l e ( ’ Log ( A b s o l u t e E r r o r ) vs Log ( dx ) ’ ) ;
g r i d on ;
% P l o t c o m p u t a t i o n a l time vs i n t e r v a l s i z e
figure ();
p l o t ( x , time , ’ b − ’ , ’ LineWidth ’ , 0 . 5 ) ;
x l a b e l ( ’ I n t e r v a l s i z e ( dx ) ’ ) ;
y l a b e l ( ’ Computational time ( s e c o n d s ) ’ ) ;
t i t l e ( ’ Computational Time vs dx ’ ) ;
g r i d on ;
Trapezoidal Method
clear all ; close all ; clc ;
% Define the i n t e r v a l [ a , b ]
a = 0 ; % Left endpoint
b = p i / 2 ; % Right e n d p o i n t
20
Report 7 APPENDIX
x = [0];
integral = [ 0 ] ;
time = [ 0 ] ;
% Range o f number o f s u b i n t e r v a l s
for n = 1:100
tic ;
% Width o f each s u b i n t e r v a l
dx = ( b − a ) / n ;
% I n i t i a l i z e t h e sum f o r t r a p e z o i d a l r u l e
sum = 0 ;
% C a l c u l a t e t h e t r a p e z o i d a l sum
for i = 1:n
x i l = a + ( i − 1 ) ∗ dx ; % L e f t e n d p o i n t
x i r = a + i ∗ dx ; % Right e n d p o i n t
f m = ( s i n ( 2 ∗ x i r ) + s i n ( 2 ∗ x i l ) ) / 2 ; % Average v a l u e a t e n d p o i n t
sum = sum + f m ∗ dx ; % Area o f t h e t r a p e z o i d
endfor
% Compute t h e a b s o l u t e e r r o r
d i f f = abs ( 1 − sum ) ;
% Store r e s u l t s
i n t e g r a l = [ i n t e g r a l ; sum ] ;
y = [y; diff ];
x = [ x ; dx ] ;
time = [ time ; t o c ] ;
endfor
% Trim a r r a y s t o remove i n i t i a l z e r o e n t r i e s
i n t e g r a l = i n t e g r a l ( 2 : end ) ;
y = y ( 2 : end ) ;
x = x ( 2 : end ) ;
time = time ( 2 : end ) ;
% P l o t a b s o l u t e e r r o r vs i n t e r v a l s i z e
figure ();
p l o t ( x , y , ’ b − ’ , ’ LineWidth ’ , 0 . 5 ) ;
x l a b e l ( ’ I n t e r v a l s i z e ( dx ) ’ ) ;
y l a b e l ( ’ Absolute e r r o r of i n t e g r a l ’ ) ;
t i t l e ( ’TRAP: A b s o l u t e E r r o r vs dx ’ ) ;
g r i d on ;
% P l o t l o g −l o g s c a l e o f a b s o l u t e e r r o r vs i n t e r v a l s i z e
21
Report 7 APPENDIX
figure ();
p l o t ( l o g ( x ) , l o g ( y ) , ’ b − ’ , ’ LineWidth ’ , 0 . 5 ) ;
x l a b e l ( ’ Logarithm o f i n t e r v a l s i z e ’ ) ;
y l a b e l ( ’ Logarithm o f a b s o l u t e e r r o r o f i n t e g r a l ’ ) ;
t i t l e ( ’TRAP: Log ( A b s o l u t e E r r o r ) vs Log ( dx ) ’ ) ;
g r i d on ;
% P l o t c o m p u t a t i o n a l time vs i n t e r v a l s i z e
figure ();
p l o t ( x , time , ’ b − ’ , ’ LineWidth ’ , 0 . 5 ) ;
x l a b e l ( ’ I n t e r v a l s i z e ( dx ) ’ ) ;
y l a b e l ( ’ Computational time ( s e c o n d s ) ’ ) ;
t i t l e ( ’TRAP: Computational Time vs dx ’ ) ;
g r i d on ;
Simpson’s 1/3 Rule
clear all ; close all ; clc ;
% Define the i n t e r v a l [ a , b ]
a = 0 ; % Left endpoint
b = p i / 2 ; % Right e n d p o i n t
22
Report 7 APPENDIX
% Compute t h e a b s o l u t e e r r o r
d i f f = abs ( 1 − sum ) ;
% Store r e s u l t s
i n t e g r a l = [ i n t e g r a l ; sum ] ;
y = [y; diff ];
x = [ x ; dx ] ;
time = [ time ; t o c ] ;
endfor
% Trim a r r a y s t o remove i n i t i a l z e r o e n t r i e s
i n t e g r a l = i n t e g r a l ( 2 : end ) ;
y = y ( 2 : end ) ;
x = x ( 2 : end ) ;
time = time ( 2 : end ) ;
% P l o t a b s o l u t e e r r o r vs i n t e r v a l s i z e
figure ();
p l o t ( x , y , ’ b − ’ , ’ LineWidth ’ , 0 . 5 ) ;
x l a b e l ( ’ I n t e r v a l s i z e ( dx ) ’ ) ;
y l a b e l ( ’ Absolute e r r o r of i n t e g r a l ’ ) ;
t i t l e ( ’ Simpson 1 / 3 : A b s o l u t e E r r o r vs dx ’ ) ;
g r i d on ;
% P l o t l o g −l o g s c a l e o f a b s o l u t e e r r o r vs i n t e r v a l s i z e
figure ();
p l o t ( l o g ( x ) , l o g ( y ) , ’ b − ’ , ’ LineWidth ’ , 0 . 5 ) ;
x l a b e l ( ’ Logarithm o f i n t e r v a l s i z e ’ ) ;
y l a b e l ( ’ Logarithm o f a b s o l u t e e r r o r o f i n t e g r a l ’ ) ;
t i t l e ( ’ Simpson 1 / 3 : Log ( A b s o l u t e E r r o r ) vs Log ( dx ) ’ ) ;
g r i d on ;
% P l o t c o m p u t a t i o n a l time vs i n t e r v a l s i z e
figure ();
p l o t ( x , time , ’ b − ’ , ’ LineWidth ’ , 0 . 5 ) ;
x l a b e l ( ’ I n t e r v a l s i z e ( dx ) ’ ) ;
y l a b e l ( ’ Computational time ( s e c o n d s ) ’ ) ;
t i t l e ( ’ Simpson 1 / 3 : Computational Time vs dx ’ ) ;
g r i d on ;
Simpson’s 3/8 Rule
clear all ; close all ; clc ;
% Define the i n t e r v a l [ a , b ]
a = 0 ; % Left endpoint
23
Report 7 APPENDIX
b = p i / 2 ; % Right e n d p o i n t
% Compute t h e a b s o l u t e e r r o r
d i f f = abs ( 1 − sum ) ;
% Store r e s u l t s
i n t e g r a l = [ i n t e g r a l ; sum ] ;
y = [y; diff ];
x = [ x ; dx ] ;
time = [ time ; t o c ] ;
endfor
% Trim a r r a y s t o remove i n i t i a l z e r o e n t r i e s
i n t e g r a l = i n t e g r a l ( 2 : end ) ;
y = y ( 2 : end ) ;
x = x ( 2 : end ) ;
time = time ( 2 : end ) ;
24
Report 7 APPENDIX
% P l o t a b s o l u t e e r r o r vs i n t e r v a l s i z e
figure ();
p l o t ( x , y , ’ b − ’ , ’ LineWidth ’ , 0 . 5 ) ;
x l a b e l ( ’ I n t e r v a l s i z e ( dx ) ’ ) ;
y l a b e l ( ’ Absolute e r r o r of i n t e g r a l ’ ) ;
t i t l e ( ’ Simpson 3 / 8 : A b s o l u t e E r r o r vs dx ’ ) ;
g r i d on ;
% P l o t l o g −l o g s c a l e o f a b s o l u t e e r r o r vs i n t e r v a l s i z e
figure ();
p l o t ( l o g ( x ) , l o g ( y ) , ’ b − ’ , ’ LineWidth ’ , 0 . 5 ) ;
x l a b e l ( ’ Logarithm o f i n t e r v a l s i z e ’ ) ;
y l a b e l ( ’ Logarithm o f a b s o l u t e e r r o r o f i n t e g r a l ’ ) ;
t i t l e ( ’ Simpson 3 / 8 : Log ( A b s o l u t e E r r o r ) vs Log ( dx ) ’ ) ;
g r i d on ;
% P l o t c o m p u t a t i o n a l time vs i n t e r v a l s i z e
figure ();
p l o t ( x , time , ’ b − ’ , ’ LineWidth ’ , 0 . 5 ) ;
x l a b e l ( ’ I n t e r v a l s i z e ( dx ) ’ ) ;
y l a b e l ( ’ Computational time ( s e c o n d s ) ’ ) ;
t i t l e ( ’ Simpson 3 / 8 : Computational Time vs dx ’ ) ;
g r i d on ;
25