Documente Academic
Documente Profesional
Documente Cultură
November 9, 2016
2
Contents
1 Homework 4 5
1.1 Problem 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2 Problem 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.3 Problem 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4 Problem 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.5 Problem 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.6 Problem 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1.7 Problem 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
1.8 Problem 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3
4
Chapter 1
Homework 4
No problems are due from this homework set. If I were collecting homework, I would collect the
last three problems. If your name is attached to a problem, then you are responsible for typesetting
the solution for that problem in LATEX. The solution is due by the due date. LATEX code should
be submitted through the Submit It! feature on the MCS website.
1.1 Problem 1
1. [Pacha, B.] Develop an M-file for Romberg integration. Test it with the example we developed
in class. The command line is as follows, and the label used is label={code:romberg}.
[integral,error approx,num iterations] = romberg(func,x min,x max,...
error desired,max iterations)
5
Solution: The purpose of this problem was to use Romberg integration to find the area
under a specific curve. The test case that was used to solve this problem was one that we
developed in class and was entered into the command line using the following two commands:
The approximated integral was found to be 1.5557. The approximated error was found to
be 3.3396 108 %, and the number of iterations it took to reach this approximation was
11. The MATLAB code for Romberg Integration can be found under Listing 1.9. Inside the
Romberg Integration code, it uses the Trapezoidal Rule to help solve the problem. The code
for the Trapezoidal Rule can be found under Listing 1.2.
Listing 1.1: Code for Rombergs Integration
%Romberg I n t e g r a t i o n
% Test Case
% f u n c = @( x ) s q r t (1x 2 )
% romberg ( func , 1, 1 , 10 7 , 1 0 0 )
%
% Inputs :
% f u n c = f u n c t i o n we want t o i n t e g r a t e
% x min , x max = l i m i t s o f t h e f u n c t i o n
% es = error desired
% m a x i t e r a t i o n s = maximum number o f i t e r a t i o n s
% trapezoid = trapezoidal rule
%
% Outputs :
% I n t e g r a l = estimated i n t e g r a l
% ea = approximate e r r o r
% i t e r a t i o n = number o f i t e r a t i o n s
% Begin e r r o r c h e c k i n g
i f nargin < 3
e r r o r ( at l e a s t 3 inputs are required )
end
i f n a r g i n < 4 | | isempty ( e s )
%S e t t i n g t h e e r r o r
es = 0.001;
end
6
i f n a r g i n < 5 | | isempty ( m a x i t e r a t i o n s )
%S e t t i n g t h e max number o f i t e r a t i o n s
max iterations = 100;
end
n u m s l i c e s =1;
%C a l l s t h e t r a p e z o i d a l method
i n t e g r a l ( 1 , 1 ) = t r a p e z o i d ( func , x min , x max , n u m s l i c e s ) ;
%I n i t i a l i z i n g t h e number o f i t e r a t i o n s t o be z e r o
iteration = 0;
%i n t e g r a l o f v e c t o r
i n t e g r a l ( i t e r a t i o n +1, 1 ) = t r a p e z o i d ( func , x min , x max , n u m s l i c e s ) ;
for k = 2: iteration + 1
j = 2 + iteration k;
%i n t e g r a l o f Romberg i n t e g r a t i o n algorthm
i n t e g r a l ( j , k ) = ( 4 ( k1) i n t e g r a l ( j +1,k1) i n t e g r a l ( j , k 1 ) ) / . . .
( 4 ( k 1) 1);
end
%approximate e r r o r
ea = abs ( ( i n t e g r a l ( 1 , i t e r a t i o n +1) i n t e g r a l ( 2 , i t e r a t i o n ) ) / . . .
i n t e g r a l (1 , i t e r a t i o n +1))100;
%c h e c k s approximate e r r o r vs d e s i r e d e r r o r
i f ea<=e s
break
end
end
%D i s p l a y i n g t h e an sw e r s t o t h e s c r e e n
d i s p ( The e r r o r was found t o be : ) ;
d i s p ( ea ) ;
7
disp ( i t e r a t i o n ) ;
%f u n c = @( x ) s q r t (1x 2 )
%i n t e g r a l = t r a p e z o i d ( func , 1, 1 , 1 0 0 )
% Answer i s 1 . 5 6 9 1
x c u r r e n t=x min ;
sum= f u n c ( x c u r r e n t ) ;
f o r k =1:( n u m s l i c e s 1)
x c u r r e n t=x c u r r e n t+h ;
sum=sum+2 f u n c ( x c u r r e n t ) ;
end
sum=sum+f u n c ( x max ) ;
i n t e g r a l= ( hsum ) / 2 ;
end
8
1.2 Problem 2
2. [Patterson, J.] Develop an M-file for Gauss quadrature. The M-file should handle Gauss
quadrature for one through six points. Test it with a known integral. The command line is
as follows, and the label used is label={code:gauss}.
integral = gauss(func,x min,x max,num points)
9
Solution: The known integral that was tested was cosine from negative one half pi to one
half pi. The exact value for this is known to be 2.00. Depending on the number of points
used, the code had varying degrees of accuracy. When there are more points, the accuracy
increased to almost the exact value.
%Joshua Patterson
%Test Case
%Gauss_Quad(@cos,-pi/2,pi/2,1024)
% Inputs:
% a = left endpoint of interval
% b = right endpoint of interval
% N = number of subintervals
% Outputs:
% G1 = value from 1-point gaussian quadrature
% G2 = value from 2-point gaussian quadrature
i=0;
% Compute exact solution
exact= integral(func,a,b);
for N=1:5
N=N*2;
i=i+1;
K(i)=N-1;
xd=linspace(a,b,N);
h=xd(2)-xd(1);
area=0;
for j=1:N-1
area=area+func(xd(j)+0.5*h);
end;
G1(i)=h*area;
10
c1=0.5*h*(1-1/sqrt(3));
c2=0.5*h*(1+1/sqrt(3));
area= 0;
for j=1:N-1
area=area+func(xd(j)+c1)+func(xd(j)+c2);
end;
G2(i)=0.5*h*area;
errG1(i)=abs(G1(i)-exact); errG2(i)=abs(G2(i)-exact);
area=G2;
end;
The code that was inputted into the command line was as follows:
Gauss_Quad(@cos,-pi/2,pi/2,1024)
The result that came out of this is 1.9358, 1.9994, 1.9999, 2.0000, 2.0000 for the number of
points from 1 to 9 points. This had an error of 6.418043e 02, 5.767139e 04, 7.306877e
05, 1.890325e 05, 6.900139e 06 respectively for each of the sets of points. This shows that
the error is quite small meaning that the method is fairly accurate.
11
1.3 Problem 3
3. [Reinhardt, D.] Develop an M-file for adaptive quadrature. Test it with a known integral.
The command line is as follows, and the label used is label={code:adapt}.
integral = quad adapt(func,x min,x max,tolerance)
12
Solution:
All throughout mathematics, definite integrals play a very crucial role. As the level of math-
ematics rises, the amount of difficult definite integrals needed to be calculated increases.
Therefore, it is very important to be able to evaluate them accurately without always having
to do them by hand. Adaptive Quadrature is able to evaluate integrals very efficiently simply
by inputing a function, minimum, maximum, and a tolerance. It calculates the area under
the curve by adding up the areas of individual slices. One key feature of this method is that
it basically pinpoints areas that have a substantial amount of variation, and uses more slices
via Simpsons Rule to create a better estimate. It adds up the area of all these individual
slices to find the entire area. The code for Adaptive Quadrature can be found in Listing 1.10.
An example
R 1 of Adaptive Quadratures accuracy is demonstrated by calculating the definite
integral, 0 x2 dx. The actual answer computed by hand is 0.3333. In order to have this
method calculate the stated definite integral,
must be entered into the command line. The answer computed by Adaptive Quadrature is
the exact same as the actual value, 0.3333. This example can also be found as a test input
inside of the code in listing 1.10. As one can see, this method is a good way of calculating
definite integrals, especially as they become more and more complex.
% Inputs :
% func function of interest
% xmin l o w e r v a l u e of i nt e rv a l
% xmax upper v a l u e of interval
% t o l e r a n c e amount of error desired
13
% Outputs :
% i n t e g r a l are a under t h e f u n c t i o n i n d e s i r e d bounds
% Test i n p u t s 1
% f u n c = @( x ) x 2 ;
% i n t e g r a l = q u a d a d a p t ( func , 0 , 1 , 0 . 0 0 0 1 )
% Check f o r c o r r e c t number o f i n p u t s
i f ( nargin < 4 )
tolerance = 0.0001;
end
i f ( nargin < 3 )
error ( At l e a s t t h r e e arguments r e q u i r e d . ) ;
end
% Finds t h e c e n t e r o f t h e i n t e r v a l
xbar = ( xmin + xmax ) / 2 ;
% Finds t h e c e n t e r o f t h e l e f t s u b i n t e r v a l
x b a r l = ( xmin + xbar ) / 2 ;
% Finds t h e c e n t e r o f t h e r i g h t s u b i n t e r v a l
x b a r r = ( xbar + xmax ) / 2 ;
% Width w i t h n = 2
h1 = ( xmax xmin ) / 2 ;
% Width w i t h n = 4
h2 = ( xmax xmin ) / 4 ;
14
end
end
15
1.4 Problem 4
16
Solution:
Because the Gauss-Legendre method is nearly fully hardcoded, the only inputs were the erf(a)
function provided in the problem, the x min, which was 0, the x max or a, which was 1.5,
and the number of points used in the approximation.
The following file was written, calling the Gauss-Legendre program, in order to solve this
problem.
function s o l u t i o n = S o l n ( )
e = exp ( 1 ) ; %S e t t i n g t h e v a r i a b l e e t o be used i n t h e f u n c t i o n
%To C a l c u l a t e t h e p e r c e n t e r r o r
e r r o r a = abs ( ( t r u e p a r t a ) / t r u e ) 1 0 0 ;
e r r o r b = abs ( ( t r u e p a r t b ) / t r u e ) 1 0 0 ;
e r r o r e x t r a = abs ( ( t r u e e x t r a ) / t r u e ) 1 0 0 ;
When this file is run in the matlab command line using Soln(), the output is as follows:
>> S o l n ( )
True Value from Matlab e r f function :
0.96611
2 p o i n t GaussLengendre :
1.2989
P e r c e n t error = 3 4 . 4 4 6 8
17
3 p o i n t GaussLegendre
1.2873
Percent error = 3 3 . 2 5 0 1
4 p o i n t GaussLegendre
1.2882
Percent error = 3 3 . 3 3 6 7
The 4-point Gauss-Legendre approximation was performed to observe whether increasing the
number of points would continue to improve the accuracy of the Gauss-Legendre approxima-
tion. As shown above, it did not.
As you can see, the Gauss-Legendre Method was not very accurate in approximating the
result to the erf(a) equation. In an attempt to view the source code for Matlabs built-in
erf() function, edit erf was entered into the command line. This produced the following
file:
%ERF Error f u n c t i o n .
% Y = ERF(X) i s t h e e r r o r f u n c t i o n f o r each e l e m e n t o f X. X must be
% r e a l . The e r r o r f u n c t i o n i s d e f i n e d as :
%
% e r f ( x ) = 2/ s q r t ( p i ) i n t e g r a l from 0 t o x o f exp( t 2) d t .
%
% See a l s o ERFC, ERFCX, ERFINV, ERFCINV.
As you can see, this file is distinctly unhelpful. Unfortunately Matlab does not allow access
to the source code for their built in files, so there appears to be no way to see the actual
method Matlab uses to
approximate the erf(a) function in their program. In an attempt to find the actual method
used, I searched forums on the website of Matlabs creator, Mathworks. The best answer I
found is shown at:
https://www.mathworks.com/matlabcentral/answers/
196807-what-method-does-matlab-use-to-compute-the-erf-function
As the answer on this forum says, it can be assumed that Matlabs erf function does not
use quadrature to estimate the error function, which is reasonable given the solutions of our
approximations.
%A l l v a l u e s b e l o w t a k e n from [ Chapral , p 5 0 9 ]
i f num pt == 2
18
c0 = 1 ;
c1 = 1 ;
x0 = 1 / ( 3 ) . 5 ;
x1 = 1 / ( 3 ) . 5 ;
%F a c t o r i n g t h e c o n v e r s i o n from o r i g i n a l i n t e g r a t i o n range t o 0 ,1
i n t e g r a l = c0 f u n c ( ( ( x max+x min ) / 2 ) + ( ( x maxx min ) / 2 ) x0 ) + . . .
c1 f u n c ( ( ( x max+x min ) / 2 ) + ( ( x maxx min ) / 2 ) x1 ) ;
end
i f num pt == 3
c0 = 5 / 9 ;
c1 = 8 / 9 ;
c2 = 5 / 9 ;
x0 = ( 3 / 5 ) . 5 ;
x1 = 0 ;
x2 = ( 3 / 5 ) . 5 ;
i n t e g r a l = c0 f u n c ( ( ( x max+x min ) / 2 ) + ( ( x maxx min ) / 2 ) x0 ) + . . .
c1 f u n c ( ( ( x max+x min ) / 2 ) + ( ( x maxx min ) / 2 ) x1 ) + . . .
c2 f u n c ( ( ( x max+x min ) / 2 ) + ( ( x maxx min ) / 2 ) x2 ) ;
end
i f num pt == 4
c0 = ( 1 8 ( 3 0 ) . 5 ) / 3 6 ;
c1 = ( 1 8 + ( 3 0 ) . 5 ) / 3 6 ;
c2 = ( 1 8 + ( 3 0 ) . 5 ) / 3 6 ;
c3 = ( 1 8 ( 3 0 ) . 5 ) / 3 6 ;
x0 = (525 + 70 ( 3 0 ) . 5 ) . 5 / 3 5 ;
x1 = (525 70 ( 3 0 ) . 5 ) . 5 / 3 5 ;
x2 = ( 5 2 5 70 ( 3 0 ) . 5 ) . 5 / 3 5 ;
x3 = ( 5 2 5 + 70 ( 3 0 ) . 5 ) . 5 / 3 5 ;
i n t e g r a l = c0 f u n c ( ( ( x max+x min ) / 2 ) + ( ( x maxx min ) / 2 ) x0 ) + . . .
c1 f u n c ( ( ( x max+x min ) / 2 ) + ( ( x maxx min ) / 2 ) x1 ) + . . .
c2 f u n c ( ( ( x max+x min ) / 2 ) + ( ( x maxx min ) / 2 ) x2 ) + . . .
c3 f u n c ( ( ( x max+x min ) / 2 ) + ( ( x maxx min ) / 2 ) x3 ) ;
end
19
1.5 Problem 5
5. [Vandine, G.] Solve Problem 20.5. Instead of using the MATLAB command quad for (c),
use your own adaptive quadrature M-file.
20
For this problem, I was asked to compute the force on a sailboat mast using a) Romberg
integration to a tolerance of s = 0.5%, b) the two-point Gauss-Legendre formula, and c)
my own adaptive quadrature M-file. The force on a sailboat mast can be represented by the
following function: Z H
z 2z
F = 200( )e H dz,
0 5+z
where z = the elevation above the deck and H = 30.
Solution:
a) Romberg Integration was the first method used to evaluate the given function. The
code for this method can be found in Listing 1. To evaluate the function using this
method, the following commands were used:
func=@(z) 200*(z/(5+z))*exp((-2*z)/30);
romberg(func,0,30,0.005)
Note: Romberg Integration requires the use of the Trapezoidal Rule. The code for this
method can be found in Listing 2.
b) Gauss Quadrature or the Two-Point Gauss-Legendre formula was the second method
used to evaluate the given function. The code for this method can be found in Listing
3. To evaluate the function using this method, the following commands were used:
func=@(z) 200*(z/(5+z))*exp((-2*z)/30);
gauss_quad(func,0,30)
This method found the value of F = 1.6106 103 .
c) Adaptive Quadrature was the final method used to evaluate the given function. The
code for this method can be found in Listing 4. To evaluate the function using this
method, the following commands were used:
func=@(z) 200*(z/(5+z))*exp((-2*z)/30);
adaptive_quad(func,0,30,0.001)
Analysis of Results:
These three methods gave answers that are relatively close together (they all have a magni-
tude of times 103 ). However, the results are not what would be expected. Both Adaptive
21
Quadrature and Romberg Integration are inherently fairly accurate integral approximation
methods, yet for this particular function, they produced answers that are furthest apart. The
Gauss Quadrature method on the other hand, should be the least accurate method. It eval-
uates the integral based on two points and is dependent upon how accurate these two points
are with respect to the actual function, yet it produced an answer that lies in between the
other two methods. For these methods to produce results such as these for this particular
function is rather odd.
% Inputs :
% func function of i n t e r e s t
% x min l o w e r v a l u e o f i n t e r v a l
% x max upper v a l u e o f i n t e r v a l
% e r r o r d e s i r e d d e s i r e d a c c u r a c y o f t h e f i n a l answer
% m a x i t e r a t i o n s number o f t i m e s t h e i n t e g r a l i s a p p r o x i m a t e d
% Outputs :
% An a r r a y o f 1 x3 t h a t shows t h e v a l u e o f t h e i n t e g r a l o f t h e g i v e n
% f u n c i t o n w i t h i n t h e i n t e r v a l [ x min , x max ] , t h e a c c u r a c y o f t h e answer ,
% and t h e number o f i t e r a t i o n s t h a t t h e program used .
% Test I n p u t s :
% From page 512 ,
% f u n c=@( x ) 0.2+25 x 200x 2+675 x 3900 x 4+400 x 5 ;
% romberg ( func , 0 , 0 . 8 )
22
% Default
num slices = 1;
error approx = 100;
% Runs t r a p e z o i d a l r u l e t o c a l c u l a t e f i r s t e s t i m a t e o f t h e i n t e g r a l .
i n t e g r a l ( 1 , 1 ) = t r a p e z o i d ( func , x min , x max , n u m s l i c e s ) ;
num iterations = 0;
fo r k = 2 : ( n u m i t e r a t i o n s + 1 )
j = 2 + num iterations k ;
% E q u a t i o n f o r Romberg i n t e g r a t i o n
i n t e g r a l ( j , k ) = ( 4 ( k1) i n t e g r a l ( j +1,k1) i n t e g r a l ( j , k 1 ) ) / . . .
( 4 ( k 1) 1);
end
% Checks t h e a c c u r a c y o f t h e a p p r o x i m a t i o n .
e r r o r a p p r o x = abs ( ( i n t e g r a l ( 1 , n u m i t e r a t i o n s + 1 ) . . .
integral (2 , num iterations ))/ integral (1 , num iterations + 1))100;
end
end
% This program u s e s t h e t r a p e z o i d a l r u l e t o a p p r o x i m a t e t h e i n t e g r a l o f a
% g i v e n f u n c t i o n w i t h i n t h e i n t e r v a l [ x min , x max ] . This i s a c l o s e d
% i n t e g r a t i o n f o r m u l a i n which t h e a rea under t h e c u r v e i s a p p r o x i m a t e d by
% t h e are a o f a t r a p e z o i d t h a t i s c r e a t e d by a s t r a i g h t l i n e from t h e
% s t a r t i n g and e n d i n g p o i n t s o f t h e i n t e r v a l . The are a under t h e c u r v e i s
% s p l i t i n t o a s p e c i f i e d number o f s l i c e s and t h e t r a p e z o i d a l r u l e i s t h e n
% run on each s l i c e . The i n d i v i d u a l a p p r o x i m a t i o n s a r e t h e n added up t o
% g i v e an a p p r o x i m a t i o n o f t h e e n t i r e are a .
% Inputs :
% func function of i n t e r e s t
23
% x min l o w e r v a l u e o f i n t e r v a l
% x max upper v a l u e o f i n t e r v a l
% n u m s l i c e s number o f s l i c e s i n which t o d i v i d e t h e i n t e r v a l
% Outputs :
% This program o u t p u t s t h e a p p r o x i m a t e d v a l u e o f t h e i n t e g r a l o f t h e g i v e n
% f u n c t i o n w i t h i n t h e i n t e r v a l [ x min , x max ] .
% Test I n p u t s :
% From page 512 ,
% f u n c=@( x ) 0.2+25 x 200x 2+675 x 3900 x 4+400 x 5 ;
% t r a p e z o i d ( func , 0 , 0 . 8 )
% C a l c u l a t e s area f o r each t r a p e z o i d w i t h i n t h e g i v e n i n t e r v a l
% [ x min , x max ] .
f o r k =1:( n u m s l i c e s 1 )
x current = x current + h ;
sum = sum + 2 f u n c ( x c u r r e n t ) ;
end
% Sums a l l a r e a s t o g e t h e r .
sum = sum + f u n c ( x max ) ;
% Approximates t h e v a l u e o f t h e i n t e g r a l .
i n t e g r a l = ( hsum) / 2 ;
end
24
% Gordon W. Vandine
% Inputs :
% func function of i n t e r e s t
% x min l o w e r v a l u e o f i n t e r v a l
% x max upper v a l u e o f i n t e r v a l
% Outputs :
% This program o u t p u t s t h e c a l c u l a t e d v a l u e o f t h e i n t e g r a l o f t h e g i v e n
% f u n c t i o n w i t h i n t h e i n t e r v a l [ x min , x max ] .
% Test I n p u t s :
% From page 512 ,
% f u n c=@( x ) 0.2+25 x 200x 2+675 x 3900 x 4+400 x 5 ;
% g a u s s q u a d ( func , 0 , 0 . 8 )
% Determine e q u i v a l e n t e n d p o i n t s .
i f ( ( x min = 1) | | ( x max = 1 ) )
x new1 = ( x max + x min ) / 2 + ( ( x max x min )/2)( 1/ sqrt ( 3 ) ) ;
else
% TwoP o i n t Gauss Quad Formula .
i n t e g r a l = f u n c (1/ sqrt ( 3 ) ) + f u n c ( 1 / sqrt ( 3 ) ) ;
end
25
% program f i r s t s p l i t s t h e main i n t e r v a l i n h a l f and t h e n s p l i t s t h e r i g h t
% and l e f t p o r t i o n s i n h a l f . I t t h e n c a l c u l a t e s two d i f f e r e n t s t e p s i z e s i n
% o r d e r t o use Simpson s 1/3 Rule f o r 2 and 4 s l i c e s on each p o r t i o n . Using
% r e c u r s i o n , t h i s program w i l l run t h r o u g h each h a l f o f t h e new i n t e r v a l ,
% w o r k i n g from l e f t t o r i g h t , u n t i l t h e v a l u e o f t h e c a l c u l a t e d i n t e g r a l
% f a l l s below the t o l e r a n c e .
% Inputs :
% func function of i n t e r e s t
% x min l o w e r v a l u e o f i n t e r v a l
% x max upper v a l u e o f i n t e r v a l
% t o l e r a n c e t h e a c c u r a c y t h a t t h e answer must f a l l w i t h i n
% Outputs :
% This program o u t p u t s t h e c a l c u l a t e d v a l u e o f t h e i n t e g r a l o f t h e g i v e n
% f u n c t i o n w i t h i n t h e i n t e r v a l [ x min , x max ] .
% Test I n p u t s :
% From page 512 ,
% f u n c=@( x ) 0.2+25 x 200x 2+675 x 3900 x 4+400 x 5 ;
% a d a p t i v e q u a d ( func , 0 , 0 . 8 )
i f ( nargin < 4 )
tolerance = 0.001;
end
% C a l c u l a t e t h e m i d p o i n t o f t h e l e f t and r i g h t p o r t i o n s o f t h e i n t e r v a l .
x q u a r t e r = ( x min + x b a r ) / 2 ;
x 3 q u a r t e r = ( x b a r + x max ) / 2 ;
h2 = ( x max x min ) / 4 ;
% n = 2 slices
% Simpson s 1/3 Rule f o r 2 s l i c e s
I 1 = ( h1 / 3 ) ( ( f u n c ( x min ) + 4 f u n c ( x b a r ) + f u n c ( x max ) ) ) ;
% n = 4 slices
26
% Simpson s 1/3 Rule f o r 4 s l i c e s
I 2 = ( h2 / 3 ) ( ( f u n c ( x min ) + 4 f u n c ( x q u a r t e r ) + 2 f u n c ( x b a r ) + . . .
4 f u n c ( x 3 q u a r t e r ) + f u n c ( x max ) ) ) ;
i f ( abs ( I 1 I 2 ) < t o l e r a n c e )
% Romberg I n t e g r a t i o n
i n t e g r a l = (16/15) I2 (1/15) I1 ;
% Using r e c u r s i o n , t h e i n t e g r a l i s c a l c u l a t e d f o r s m a l l e r and s m a l l e r
% intervals .
else
i n t e g r a l = a d a p t i v e q u a d ( func , x min , x bar , t o l e r a n c e ) ;
end
end
27
1.6 Problem 6
6. [Vaz, L.] Solve Problem 20.8. Instead of using the MATLAB command quad for (b), use
your own adaptive quadrature M-file.
28
Solution: We are required to find the mass transported via a pipe using the equation:
Z t2
M= Q(t)c(t)dt
t1
where M = mass (mg), t1 = initial time (min), t2 = final time (min), Q(t) = flow rate
(m3 /min), and c(t) = concentration (mg/m3 ). These functions are given by:
Q(t) = 9 + 5cos(0.4t)2
(a) Romberg integration: The code for this method can be seen in Listing 1.9. In order to
run the method, I implemented the following commands in the command window:
error_desired = 0.001;
max_iterations = 30;
x_min = 2;
x_max = 8;
f unc = @(t)(9+5cos(0.4t)2 )(5exp(0.5t)+2exp(0.15t)); [integral,error_approx,num_iteration
= romberg( func,x_min,x_max,error_desired,max_iterations )
integral = 335.9625
error_approx= 7.0685e06
num_iterations = 3
(b) Adaptive Quadrature: The code for this method can be seen in Listing 1.10. In order
to run the method, I implemented the following commands in the command window:
tolerance = 0.00001;
x_min = 2;
x_max = 8;
f unc = @(t)(9 + 5 cos(0.4 t)2 ) (5 exp(0.5 t) + 2 exp(0.15 t)); [integral] =
quad_adapt( func,x_min,x_max,error_desired,max_iterations )
integral = 335.9625
29
Listing 1.9: MATLAB code for romberg integration
% Inputs :
% f u n c f u n c t i o n t o be i n t e g r a t e d
% x min l o w e r bound o f t h e i n t e r v a l
% x max upper bound o f t h e i n t e r v a l
% e r r o r d e s i r e d d e s i r e d e r r o r o f t h e answer
% m a x i t e r a t i o n s maximum a l l o w a b l e i t e r a t i o n s
% Output :
% i n t e g r a l t h e a p p r o x i m a t e answer by Romberg I n t e g r a t i o n
% e r r o r a p p r o x a p p r o x i m a t e r e l a t i v e e r r o r a t t h e most r e c e n t s t e p
% n u m i t e r a t i o n s number o f i t e r a t i o n s done when running t h e program
% Test i n p u t s :
% x min = 1;
% x max = 1 ;
% error desired = 0.00001;
% max iterations = 30;
% f u n c = @( x ) s q r t (1x 2 ) ;
% [ i n t e g r a l , e r r o r a p p r o x , n u m i t e r a t i o n s ] = romberg ( func , x min , . . .
% x max , e r r o r d e s i r e d , m a x i t e r a t i o n s )
% Initializing variables
num iterations = 0;
error approx = 100;
num slices = 2;
I ( 1 , 1 ) = t r a p e z o i d ( func , x min , x max , n u m s l i c e s ) ;
30
while ( ( n u m i t e r a t i o n s < m a x i t e r a t i o n s ) & & . . .
( error approx > error desired ))
num iterations = num iterations + 1;
n u m s l i c e s = 2 n u m s l i c e s ;
I ( n u m i t e r a t i o n s +1 ,1) = t r a p e z o i d ( func , x min , x max , n u m s l i c e s ) ;
fo r k = 2 : n u m i t e r a t i o n s +1
j = 2 + num iterations k ;
I ( j , k ) = ( 4 ( k1) I ( j +1,k1) I ( j , k 1)) / ( 4 ( k 1) 1);
end
e r r o r a p p r o x = abs ( ( I ( 1 , n u m i t e r a t i o n s +1) I ( 2 , n u m i t e r a t i o n s ) ) / . . .
I ( 1 , n u m i t e r a t i o n s +1)) 1 0 0 ;
end
i n t e g r a l = I ( 1 , n u m i t e r a t i o n s +1);
end
% Inputs :
% f u n c f u n c t i o n t o be i n t e g r a t e d
% x min l o w e r bound o f t h e i n t e r v a l
% x max upper bound o f t h e i n t e r v a l
% t o l e r a n c e t o l e r a n c e on t h e d i f f e r e n c e b e t w e e n t h e i n t e g r a l s o f
% n=2 s l i c e s and n=4 s l i c e s
% Output :
% i n t e g r a l t h e a p p r o x i m a t e answer by A d a p t i v e Quadrature
% Initialize variables
xbar = ( x min+x max ) / 2 ;
x b a r l = ( x min+xbar ) / 2 ;
x b a r r = ( xbar+x max ) / 2 ;
31
h1 = ( x maxx min ) / 2 ; % w i d t h o f a s l i c e f o r 2 s l i c e s
h2 = h1 / 2 ; % w i d t h o f a s l i c e f o r 4 s l i c e s
end
32
1.7 Problem 7
7. [Volk, B.] Solve Problems 21.1 and 21.4. These two are related, so it makes sense to put
them in the same problem.
No solution has been submitted.
33
1.8 Problem 8
34
Problem 21.3 out of the textbook asks you to create a order h2 approximation for the third
derivative of a function using the points f (xi1 ), f (xi2 ), f (xi+1 ), f (xi+2 ) out to the third
derivative with appropriate truncation to show the induced error. To do this, the first and
second derivatives need to be removed from the equation.
To do that, four equations are necesary, one for each of the points in question.
f 00 (xi ) f 000 (xi ) f (4) (xi )
(a) f (xi+1 ) = f (xi ) + f 0 (xi ) (x) + 2 (x)2 + 6 (x)3 + 24 (x)4 + . . .
f 00 (xi ) f 000 (xi ) f (4) (xi )
(b) f (xi+2 ) = f (xi ) + f 0 (xi ) + 2 (2x)2 + 6 (2x)3 + 24 (2x)4 + . . .
f 00 (xi ) f 000 (xi ) f (4) (xi )
(c) f (xi1 ) = f (xi ) f 0 (xi ) (x) + 2 (x)2 6 (x)3 + 24 (x)4 + . . .
00 (x 000 (x (4) (x
(d) f (xi2 ) = f (x2 )f 0 (xi )(2x)+ f 2
i)
(2x)2 f 6
i)
(2x)3 + f 24
i)
(2x)4 +. . .
The first step is to multiply equation 1 by a factor of -2, equation 3 by a factor of 2, and
equation 4 by -1 so that each of the f 0 (xi ) terms has the same coefficient. This will allow
the first derivative term to drop when all 4 equations are combined. This gives updated
equations:
f 00 (xi ) f 000 (xi )
(a) 2 f (xi+1 ) = 2 f (xi ) 2 f 0 (xi ) (x) 2 2 (x)2 2 6 (x)3
f (4) (x i)
2 24 (x)4 . . .
f 00 (xi f 000 (xi ) (4)
(b) f (xi+2 ) = f (xi ) + f 0 (xi ) + 2 (2x)2 + 6 (2x)3 + f 24(xi ) (2x)4 + . . .
00 000
(c) 2 f (xi1 ) = 2 f (xi ) 2 f 0 (xi ) (x) + 2 f (x 2
i)
(x)2 2 f 6(xi ) (x)3 + 2
f 4 (xi )
24 (x)4 + . . .
00 (x 000 (x 4 (x
(d) f (xi2 ) = f (xi )+f 0 (xi )(2x) f 2
i)
(2x)2 + f 6
i)
(2x)3 f 24
i)
(2x)4 +. . .
By combining the 4 equations above through addition, the end result is:
f 000 (xi )
f (xi+2 ) 2 f (xi+1 ) + 2 f (xi1 ) f (xi2 ) + O((x)5 ) = 6 (x)3
3
Which can be simplified to the following:
This result is O(h2 ) and thus satisfies the specifications for this problem.
35