0% found this document useful (0 votes)
11 views26 pages

Ae23b103 AS2101exp05

this is the experiment for the iitm

Uploaded by

bsharshith1808
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views26 pages

Ae23b103 AS2101exp05

this is the experiment for the iitm

Uploaded by

bsharshith1808
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

DEPARTMENT OF

AEROSPACE ENGINEERING
Indian Institute Of Technology, Madras

AS2101: Introduction to Aerospace


Engineering
Methods of Numerical Integration
Siddhi Sharma
AE23B103
Report LIST OF FIGURES

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.

4.1 Numerical methods of integration


Numerical methods of integration is a broad analytic approach, that comprises
of a family of algorithms that calculate approximate numerical values of definite
integrals given an error bound. Some of the one dimensional approaches involve:

1. Right Endpoint Method


2. Left Endpoint Method

3. Midpoint Method
4. Trapezoidal Method
5. Simpson’s 1/3 Rule
6. Simpson’s 3/8 Rule

4.1.1 Right Endpoint Method


This is a method of approximation of Riemann’s sum. The area under the curve
is divided into rectangles of equal width. The interval between the two end point
taken into consideration is divided into n intervals of equal length. The value
of the function at the right end point of these intervals is taken as height of the
rectangle. According to this approximation, the value of the integral is equal to
summation of all the rectangles formed.

Let f(x) be a continuous, non-negative function defined on the closed interval


[a,b],

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

I ≈ f (x0 )∆x + f (x1 )∆x + . . . + f (xn−1 )∆x


Thus, we have
Z b n−1
X
f (x) dx ≈ f (xi )∆x
a i=0

4.1.2 Left End Point Method


This is a method of approximation of Riemann’s sum and is similar to the right
endpoint method. The area under the curve is divided into rectangles of equal
width. The interval between the two end point taken into consideration is di-
vided into n intervals of equal length. The value of the function at the left
end point of these intervals is taken as height of the rectangle. According to

4
Report 4 THEORY

this approximation, the value of the integral is equal to summation of all the
rectangles formed.

Let f(x) be a continuous, non-negative function defined on the closed interval


[a,b],

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

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

I ≈ f (x0 )∆x + f (x1 )∆x + . . . + f (xn−1 )∆x


Thus, we have
Z b n−1
X
f (x) dx ≈ f (xi )∆x
a i=0

4.1.3 Middle Point Method


This method is also an approximation of Riemann’s sum. It is similar to right
end point and left endpoint rule but is a better approximation in most cases. In
the former there is possibility of loosing some area whereas in the latter there
is a possibility of over approximation. Thus the midpoint rule balances these
discrepancies by considering the height of the rectangle to be the value of the
function at the average point of the left and right end point of the interval. in
this method as well, he area under the curve is divided into rectangles of equal
width. the given interval is divided into n equal intervals with the height of
each rectangle being the value of the function of the interval end points.

Figure 1: Caption

Let f(x) be a continuous, non-negative function defined on the closed interval


[a,b],

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

4.1.4 Trapezoidal method


In this method, the area under of the curve is approximated using trapezoids
of equal width. Hence in this method, similar to the ones above, the given
interval is divided into n intervals of equal length. In the trapezoidal method,
rather than taking the end points and drawing rectangles of the height equal to
value of the function at that point, a straight line is formed joining the values
of the function at these end points, creating a trapezoid. The value of integral
is approximated to be the sum of the trapezoids formed.

7
Report 4 THEORY

Let f(x) be a continuous, non-negative function defined on the closed interval


[a,b],

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

base1 + base2 f (xi−1 ) + f (xi )


A = height × = ∆x
2 2
and the approximation of the integral is given by

8
Report 4 THEORY

f (x0 ) + f (x1 ) f (x1 ) + f (x2 ) f (xn−1 ) + f (xn )


I≈ ∆x + ∆x + . . . + ∆x
2 2 2
Thus, we have
Z b
∆x
f (x) dx ≈ (f (x0 ) + 2f (x1 ) + 2f (x2 ) + . . . + f (xn ))
a 2

4.1.5 Simpson’s 1/3 Rule


Simpson’s 1/3 Rule is a more advanced numerical integration technique com-
pared to the right and left endpoint rules. It provides a better approximation
by fitting a quadratic polynomial (a parabola) through three consecutive points
in the interval. This method results in more accurate integral estimates, partic-
ularly for smooth functions.
In this method, the interval [a, b] is divided into an even number of equal subin-
tervals. Instead of approximating the area under the curve using rectangles or
trapezoids, Simpson’s 1/3 Rule approximates the curve using parabolas, ensur-
ing higher accuracy in the integration.

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

According to Simpson’s 1/3 Rule, the integral is approximated by com-


bining the function values at the endpoints and midpoints of the subintervals.
The formula for Simpson’s 1/3 Rule is:

∆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

4.1.6 Simpson’s 3/8 Rule


Simpson’s 3/8 Rule is a numerical integration technique used for approximat-
ing the value of definite integrals. This method is an extension of Simpson’s
1/3 Rule, involving the fitting of a cubic polynomial (third-degree polynomial)
through four points. It is particularly useful when the number of subintervals
is a multiple of 3.
In this method, the interval [a, b] is divided into n subintervals, where n must be
a multiple of 3. The advantage of Simpson’s 3/8 Rule lies in its ability to handle
higher order approximations more effectively, especially for functions with rapid
variation.

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.

This method provides a higher level of accuracy compared to the Trapezoidal


Rule and Simpson’s 1/3 Rule when the function exhibits rapid variations.
To summarize, the integral approximation using Simpson’s 3/8 Rule is:

 
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

4.2 Error Calculation


Error calculation is necessary to create margins for the numerical integration
methods to produce most accurate results. The integral approximated through
these model is not exactly equal to the true value of the integral and hence having
a error factor will help in the predictions and analysis in being as accurate as
possible.

4.2.1 Right Endpoint Method - Error Calculation


The absolute error for the Right Endpoint Method is given by:

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.

4.2.2 Left Endpoint Method - Error Calculation


The absolute error for the Left Endpoint Method is:

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.

4.2.3 Midpoint Method - Error Calculation


The absolute error for the Midpoint Method is:

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.

4.2.4 Trapezoidal Method - Error Calculation


The absolute error for the Trapezoidal Method is:

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.

4.2.5 Simpson’s 1/3 Rule - Error Calculation


The absolute error for Simpson’s 1/3 Rule is:

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.

4.2.6 Absolute Error


The absolute error for the integral approximation is defined as:

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

2. The curve obtained for Simpson’s approximation is almost smooth.


3. The Midpoint approximation has the least upper bound absolute error
compared to any other method.
4. The absolute error is highest at the largest interval i.e at least number of
intervals and decreases as the number of intervals increases and the size
of the interval decreases.

5.2 Logarithm of Absolute error vs Logarithm of Interval


size
The graphs obtained by different approximation methods is as 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 the logarithm of absolute error versus the loga-
rithm of interval size is approximately a straight line for all the approxi-
mation methods.

2. The x- and y-intercepts of the curves vary depending on the approximation


method used.
3. The Right Endpoint (REP), Left Endpoint (LEP), and Trapezoidal meth-
ods exhibit similar graphs, which is also consistent with previous observa-
tions.

4. The Trapezoidal approximation has the most negative x- and y-intercepts.

14
Report 6 CONCLUSION

5.3 Computational time vs Step size


The graphs below show the computational cost, measured in time, for calculat-
ing the integral approximation with respect to the interval size, as obtained by
different approximation methods.

(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

endpoint method, right endpoint method, midpoint method, trapezoidal


method, and Simpson’s rule. The goal was to compare their accuracy and
computational speed.
2. The analysis showed that the left and right endpoint methods produced
similar graphs and identical errors, making them comparable in perfor-
mance. The midpoint method, however, resulted in a lower absolute er-
ror compared to the endpoint methods. Simpson’s rule stood out as the
most accurate, with its error decreasing rapidly as the number of inter-
vals increased, offering the best approximation among the methods as the
interval size approached zero.

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

% I n i t i a l i z e arrays for storing r e s u l t s


y = [0];
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 Riemann sum


sum = 0 ;

% 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

% I n i t i a l i z e arrays for storing r e s u l t s


y = [0];

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 ;

% I n i t i a l i z e t h e sum f o r Riemann sum


sum = 0 ;

% 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

% I n i t i a l i z e arrays for storing r e s u l t s


y = [0];
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 midpoint Riemann sum


sum = 0 ;

% C a l c u l a t e t h e midpoint Riemann sum


for i = 1:n
x i r = a + i ∗ dx ; % Right e n d p o i n t
x i l = a + ( i − 1 ) ∗ dx ; % Left endpoint
x i m = ( x i r + x i l ) / 2; % Midpoint 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 m ) ∗ 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 ) ;

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

% I n i t i a l i z e arrays for storing r e s u l t s


y = [0];

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

% I n i t i a l i z e arrays for storing r e s u l t s


y = [0];
x = [0];
integral = [ 0 ] ;
time = [ 0 ] ;

% Range o f number o f s u b i n t e r v a l s ( must be even f o r Simpson ’ s r u l e )


for n = 2:2: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 Simpson ’ s 1/3 r u l e


sum = 0 ;

% C a l c u l a t e t h e Simpson ’ s 1/3 sum


f o r i = 0 : n/2−1
x0 = a + 2∗ i ∗dx ; % Even i n d e x
x1 = a + ( 2 ∗ i + 1 ) ∗ dx ; % Odd i n d e x
x2 = a + ( 2 ∗ i + 2 ) ∗ dx ; % Next even i n d e x
sum = sum + ( s i n ( 2 ∗ x0 ) + 4∗ s i n ( 2 ∗ x1 ) + s i n ( 2 ∗ x2 ) ) ; % Simpson ’ s r u l e f o r m
endfor

sum = sum ∗ ( dx / 3 ) ; % F i n a l adjustment

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

% I n i t i a l i z e arrays for storing r e s u l t s


y = [0];
x = [0];
integral = [ 0 ] ;
time = [ 0 ] ;

% Range o f number o f s u b i n t e r v a l s ( must be a m u l t i p l e o f 3 f o r Simpson ’ s 3/8 r u l


for n = 3:3:99
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 Simpson ’ s 3/8 r u l e


sum = 0 ;

% C a l c u l a t e t h e Simpson ’ s 3/8 sum


f o r i = 0 : ( n/3)−1
x0 = a + 3∗ i ∗dx ; % Starting point
x1 = a + ( 3 ∗ i + 1 ) ∗ dx ; % First point
x2 = a + ( 3 ∗ i + 2 ) ∗ dx ; % Second p o i n t
x3 = a + ( 3 ∗ i + 3 ) ∗ dx ; % Ending p o i n t

% Apply Simpson ’ s 3/8 f o r m u l a


sum = sum + ( s i n ( 2 ∗ x0 ) + 3∗ s i n ( 2 ∗ x1 ) + 3∗ s i n ( 2 ∗ x2 ) + s i n ( 2 ∗ x3 ) ) ;
endfor

sum = sum ∗ ( 3 ∗ dx / 8 ) ; % F i n a l adjustment

% 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy