Sunteți pe pagina 1din 35

MATH 373 Homework Solutions

Section 01, Fall 2016

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

MATH 373 Numerical Analysis


Homework #4
Assigned: October 26, 2016
Due: November 9, 2016

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:

func = @(x) sqrt(1-x^2);


romberg (func, -1, 1, 10^-7, 100);

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

f u n c t i o n [ I n t e g r a l , ea , i t e r a t i o n ] = romberg ( func , x min , x max , es , . . .


max iterations )

% 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;

while i t e r a t i o n < max iterations


iteration = iteration + 1;
n u m s l i c e s = 2 i t e r a t i o n ;

%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 ) ;

d i s p ( The number o f i t e r a t i o n s t o perform t h i s c a l c u l a t i o n was : ) ;

7
disp ( i t e r a t i o n ) ;

d i s p ( The approximated i n t e g r a l was found t o be : ) ;


I n t e g r a l = i n t e g r a l ( 1 , i t e r a t i o n +1);

Listing 1.2: Code for Trapezoidal Rule


%T r a p e z o i d a l Rule

f u n c t i o n i n t e g r a l = t r a p e z o i d ( func , x min , x max , n u m s l i c e s )

%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 ;

h=(x max x min ) / n u m s l i c e s ;

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.

function [ area] = Gauss_Quad(func,a,b,N)

%Joshua Patterson

%Gaussian Quadrature is a method of how to estimate the integral of an unknown value.


%This is a method which depends the number of points that is to be used for the given situ

%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);

fprintf(\nExact answer = %e\n,exact);

%Spacing the number of points out

for N=1:5
N=N*2;
i=i+1;
K(i)=N-1;
xd=linspace(a,b,N);
h=xd(2)-xd(1);

%Main part of Gaussian Quad code

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);

%Showing the results to the user

fprintf(\nNumber of subintervals = %4i \n,K(i));


fprintf(\nGauss 1 = %e Gauss 2 = %e \n,G1(i), G2(i));
fprintf(\nGauss Error 1 = %e Gauss Error 2 = %e \n\n,errG1(i), errG2(i));

%Just returning the area of the total

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,

func = @(x) x^2;


integral = quad_adapt(func, 0, 1, 0.0001);

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.

Listing 1.3: MATLAB code for Adaptive Quadrature


function i n t e g r a l = quad adapt ( func , xmin , xmax , t o l e r a n c e )
% A d a p t i v e Quadrature Method
% November 1 , 2016
% David R e i n h a r d t

% E v a l u a t e s a d e f i n i t e i n t e g r a l ( area under t h e c u r v e ) by accomodating


% r e g i o n s w i t h h i g h e r v a r i a b i l i t y as w e l l as s e c t i o n s t h a t a r e more
% g r a d u a l . This method a d j u s t s t h e s t e p s i z e so t h a t s m a l l i n t e r v a l s
% a r e used i n r e g i o n s o f r a p i d v a r i a t i o n s and l a r g e r i n t e r v a l s a r e used
% where t h e f u n c t i o n c h a n g e s g r a d u a l l y . I f t h e e r r o r b e t w e e n t h e two
% l e v e l s o f Simpsons Rule i s t o o g r e a t , i t k e e p s making t h e i n t e r v a l
% s m a l l e r and s m a l l e r u n t i l t h e e r r o r ( t o l e r a n c e ) i s met . I t s t a r t s at
% t h e l e f t and moves t o w a r d s t h e r i g h t u n t i l e v e n t u a l l y t h e e n t i r e
% i n t e r v a l s a rea i s e v a l u a t e d .

% 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 ;

% Simpson s 1/3 Rule


I 1 = ( h1 / 3 ) ( f u n c ( xmin ) + 4 f u n c ( xbar ) + f u n c ( xmax ) ) ;

% Simpson s 1/3 Rule w i t h n = 4


I 2 = ( h2 / 3 ) ( f u n c ( xmin ) + 4 f u n c ( x b a r l ) + 2 f u n c ( xbar ) + . . .
4 f u n c ( x b a r r ) + f u n c ( xmax ) ) ;

i f ( ( abs ( I1I 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 ;
else
% S p l i t s i n t e r v a l s a g a i n and r e e v a l u a t e s u n t i l a rea i s found
i n t e g r a l = quad adapt ( func , xmin , xbar , t o l e r a n c e ) ;
i n t e g r a l = i n t e g r a l + quad adapt ( func , xbar , xmax , t o l e r a n c e ) ;

14
end
end

15
1.4 Problem 4

4. [Sieh, B.] Problem 20.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

%I n i t i a l Values o b t a i n e d from t h e problem


f u n c = @( x ) e (x 2 ) ;
x min = 0 ;
x max = 1 . 5 ;

%C a l c u l a t i n g GaussLegendre a p p r o x i m a t i o n s f o r each number o f p o i n t s


p a r t a = ( 2 / ( pi . 5 ) ) g a u s s g e n ( func , x min , x max , 2 ) ;
p a r t b = ( 2 / ( pi . 5 ) ) g a u s s g e n ( func , x min , x max , 3 ) ;
e x t r a = ( 2 / ( pi . 5 ) ) g a u s s g e n ( func , x min , x max , 4 ) ;
%C a l c u l a t i n g t r u e e r f v a l u e
true = erf ( 1 . 5 ) ;

%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 ;

%F orm atti ng o u t p u t t h e way I want i t t o


disp ( True Value from Matlab e r f f u n c t i o n : )
disp ( num2str ( t r u e ) )
disp ( 2 p o i n t GaussLengendre : )
disp ( num2str ( p a r t a ) )
disp ( [ P e r c e n t e r r o r = , num2str ( e r r o r a ) ] )
disp ( 3 p o i n t GaussLegendre )
disp ( num2str ( p a r t b ) )
disp ( [ P e r c e n t e r r o r = , num2str ( e r r o r b ) ] )
disp ( 4 p o i n t GaussLegendre )
disp ( num2str ( e x t r a ) )
disp ( [ P e r c e n t e r r o r = , num2str ( e r r o r e x t r a ) ] )
end

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.

% C o p y r i g h t 19842010 The MathWorks , Inc .


% B u i l t i n f u n c t i o n .

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.

Listing 1.4: MATLAB code for Gauss Quadrature


function i n t e g r a l = g a u s s g e n ( func , x min , x max , num pt )

%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)

This method found the value of F = 2.1502 103 .

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)

This method found the value of F = 1.4806 103 .

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.

Listing 1.5: MATLAB code for Romberg Integration


function [ 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 )
% Romberg I n t e g r a t i o n
% October 31 , 2016
% Gordon W. Vandine

% This program u s e s Romberg i n t e g r a t i o n t o c a l c u l 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 ] . U t i l i z i n g t h e
% t r a p e z o i d a l r u l e , t h i s program u s e s two e s t i m a t e s o f an i n t e g r a l t o
% compute a t h i r d , more a c c u r a t e a p p r o x i m a t i o n . I t c o n t i n u e s t o do t h i s
% u n t i l t h e d e s i r e d e r r o r f a l l s b e l o w t h e a p p r o x i m a t e e r r o r , or u n t i l t h e
% maximum number o f i t e r a t i o n s i s r e a c h e d .

% 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 )

% Check t o s e e i f c o r r e c t number o f v a r i a b l e s has been e n t e r e d i n o r d e r t o


% s a t i s f y the function .
i f ( nargin < 3 )
error ( This f u n c t i o n r e q u i r e s a t l e a s t t h r e e arguments . ) ;
e l s e i f ( nargin < 4 )
error desired = 0.00001;
max iterations = 50;
else
max iterations = 50;
end

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;

% Using p r i o r e s t i m a t e s , Romberg i n t e g r a t i o n i s used t o c a l c u l a t e a f i n a l


% answer .
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 ) && ( e r r o r d e s i r e d < e r r o r a p p r o x ) )
num iterations = num iterations + 1;
n u m s l i c e s = 2 n u m i t e r a t i o n s ;

i n t e g r a l ( 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 ;

% 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;

integral = integral (1 , num iterations + 1);

end

end

Listing 1.6: MATLAB code for Trapezoidal Rule.


function i n t e g r a l = t r a p e z o i d ( func , x min , x max , n u m s l i c e s )
% T r a p e z o i d a l Rule
% October 31 , 2016
% Gordon W. Vandine

% 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 )

% % Check t o s e e i f c o r r e c t number o f v a r i a b l e s has been e n t e r e d i n o r d e r t o


% s a t i s f y the function .
i f ( nargin < 3 )
error ( This f u n c t i o n r e q u i r e s a t l e a s t t h r e e arguments . ) ;
end

i f ( x max <= x min )


error ( The minimum and maximum v a l u e s s h o u l d be swapped . ) ;
end

i f ( ( nargin < 4 ) | | ( isempty ( n u m s l i c e s ) ) )


num slices = 50;
end

% Determines s t e p s i z e and c a l c u l a t e s f i r s t t r a p e z o i d are a .


x c u r r e n t = x min ;
h = ( x max x min ) / n u m s l i c e s ;
sum = f u n c ( x c u r r e n t ) ;

% 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

Listing 1.7: MATLAB code for Gauss Quadrature.


function i n t e g r a l = g a u s s q u a d ( func , x min , x max )
% Gauss Quadrature
% October 31 , 2016

24
% Gordon W. Vandine

% This program u s e s t h e Gauss q u a d r a t u r e method f o r two p o i n t s t o c a l c u l 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 t h e i n t e r v a l [ x min , x max ] .
% O r i g i n a l l y , t h i s method i s meant f o r t h e i n t e r v a l t o be [ 1 , 1 ] . However ,
% f o r a d i f f e r e n t i n t e r v a l , i t w i l l c a l c u l a t e new e n d p o i n t s and d e t e r m i n e
% the value of the i n t e g r a l .

% 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 )

% Check t o s e e i f c o r r e c t number o f v a r i a b l e s has been e n t e r e d i n o r d e r t o


% s a t i s f y the function .
i f ( nargin < 3 )
error ( This f u n c t i o n r e q u i r e s a t l e a s t t h r e e arguments . ) ;
end

% 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 ) ) ;

x new2 = ( x max + x min ) / 2 + ( ( x max x min ) / 2 ) ( 1 / sqrt ( 3 ) ) ;

% TwoP o i n t Gauss Quad Formula w i t h e n d p o i n t s o t h e r than 1 and 1 .


i n t e g r a l = ( f u n c ( x new1 ) + f u n c ( x new2 ) ) ( x max x min ) / 2 ;

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

Listing 1.8: MATLAB code for Adaptive Quadrature.


function i n t e g r a l = a d a p t i v e q u a d ( func , x min , x max , t o l e r a n c e )
% A d a p t i v e Quadrature
% October 31 , 2016
% Gordon W. Vandine

% This program u s e s t h e a d a p t i v e q u a d r a t u r e method t o c a l c u l 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

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 )

% Check t o s e e i f c o r r e c t number o f v a r i a b l e s has been e n t e r e d i n o r d e r t o


% s a t i s f y the function .
i f ( nargin < 3 )
error ( This f u n c t i o n r e q u i r e s a t l e a s t t h r e e arguments . ) ;
end

i f ( nargin < 4 )
tolerance = 0.001;
end

% C a l c u l a t e the midpoint of the i n i t i a l i n t e r v a l .


x b a r = ( x max + x min ) / 2 ;

% 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 ;

% C a l c u l a t e t h e s t e p s i z e s f o r 2 and 4 s l i c e s t o be used i n Simpson s 1/3


% Rule .
h1 = ( x max x min ) / 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 ) ;

i n t e g r a l = i n t e g r a l + a d a p t i v e q u a d ( func , x bar , x max , 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

c(t) = 5e0.5t + 2e0.15t


For t1 = 2min until t2 = 8min, I used romberg integration and adaptive quadrature to find
the mass transported.

(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 )

The outputs were as follows:

integral = 335.9625
error_approx= 7.0685e06
num_iterations = 3

Therefore, the mass flowing through the pipe is 335.9625 mg.

(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 )

The output was as follows:

integral = 335.9625

Therefore, the mass flowing through the pipe is 335.9625 mg.

29
Listing 1.9: MATLAB code for romberg integration

function [ 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 )
%romberg Romberg I n t e g r a t i o n o f a f u n c t i o n
% This f u n c t i o n u s e s t h e Romberg i n t e g r a t i o n t e c h n i q u e t o f i n d an
% approximate i n t e g r a l of the f u n c t i o n . I t s t a r t s o f f with t r a p e z o i d
% i n t e g r a t i o n f o r 2 s l i c e s and t h e n 4 s l i c e s . Both o f t h e s e i n t e g r a l s
% have a f a i r l y l a r g e e r r o r . By a d d i n g them up , we can r e d u c e t h e e r r o r
% by a f a c t o r o f 2 and g e t a more a c c u r a t e a p p r o x i m a t i o n . This i s done
% r e p e a t e d l y t i l l t h e e r r o r c o n d i t i o n i s s a t i s f i e d or a maximum number
% of i t e r a t i o n s i s reached .

% 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 )

% c h e c k s t h e i n p u t s and a s s i g n s d e f a u l t v a l u e s i f t h e y haven t been


% s p e c i f i e d by t h e u s e r
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
i f ( nargin < 4 ) | | isempty ( e r r o r d e s i r e d )
error desired = 0.00001;
end
i f ( nargin < 5 ) | | isempty ( m a x i t e r a t i o n s )
max iterations = 100;
end

% 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

Listing 1.10: MATLAB code for adaptive quadrature


function [ i n t e g r a l ] = quad adapt ( func , x min , x max , t o l e r a n c e )
%q u a d a d a p t A d a p t i v e Quadrature u s i n g Simpson s 1/3 rd Rule and Romberg
% Integration
% A d a p t i v e Quadrature b r e a k s up t h e main i n t e r v a l i n t o sub i n t e r v a l s
% o f s i z e h1 ( n=2) and s i z e h2 ( n=4) and f i n d s i n t e g r a l s u s i n g Simpson s
% 1/3 rd r u l e . I f t h e t o l e r a n c e o f t h e d i f f e r e n c e b e t w e e n i n t e g r a l s meets
% t h e user d e f i n e d t o l e r a n c e c r i t e r i a , i t adds t h e two i n t e g r a l s u s i n g
% Romberg i n t e g r a t i o n .
% I f t h e t o l e r a n c e on t h i s d i f f e r e n c e i s not s m a l l enough , i t r e c u r s i v e l y
% c r e a t e s new s u b i n t e r v a l s and d o e s t h e same p r o c e d u r e a g a i n t o g e t a
% b e t t e r approximation .

% 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

% c h e c k s t h e i n p u t s and a s s i g n s d e f a u l t v a l u e s i f t h e y haven t been


% s p e c i f i e d by t h e u s e r
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
i f ( nargin < 4 ) | | isempty ( t o l e r a n c e )
tolerance = 0.00001;

% 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

%Implementing t h e f o r m u l a s f o r Simpson s 1/3 rd Rule


I 1 = ( h1 / 3 ) ( f u n c ( x min ) + 4 f u n c ( xbar ) + f u n c ( x max ) ) ;
I 2 = ( h2 / 3 ) ( f u n c ( x min ) + 4 ( f u n c ( x b a r l )+ f u n c ( x b a r r ) ) . . .
+ 2 f u n c ( xbar ) + f u n c ( x max ) ) ;

i f abs ( I1I 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 ;
else
% R e c u r s i v e c a l l s t o t h e same f u n c t i o n t o h a n d l e l e f t and r i g h t
% intervals
i n t e g r a l = quad adapt ( func , x min , xbar , t o l e r a n c e ) ;
i n t e g r a l = i n t e g r a l + quad adapt ( func , xbar , x max , t o l e r a n c e ) ;
end

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

8. [Volkmer, M.] Solve Problem 21.3.

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:

(f (xi+2 ) 2 f (xi+1 ) + 2 f (xi1 ) f (xi2 )


+ O(h2 ) = f 000 (xi )
(2 (x)3 )

This result is O(h2 ) and thus satisfies the specifications for this problem.

35

S-ar putea să vă placă și