Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Name:__________________________________ Date:_____________________

Course & Section:_________________________ Program & Year:____________

Experiment 3
Numerical Integration

Objectives
By the end of the experiment, the student should be able to
 define the different numerical integration methods.
 analyze the different techniques in numerical integration.
 demonstrate the usage of matlab in computing numerical integration.
 construct a script file for numerical integration operation.

Tools Required
 Personal Computer/Desktop Computer/Computer Workstation
 MATLAB software

MATLAB FUNCTIONS

integral Numerically evaluate integral.


Q = integral(FUN,A,B) approximates the integral of function FUN from A
to B using global adaptive quadrature and default error tolerances.

FUN must be a function handle. A and B can be -Inf or Inf. If both are
finite, they can be complex. If at least one is complex, integral
approximates the path integral from A to B over a straight line path.

For scalar-valued problems the function Y = FUN(X) must accept a vector


argument X and return a vector result Y, the integrand function
evaluated at each element of X. For array-valued problems (see the
'ArrayValued' option below) FUN must accept a scalar and return an
array of values.

Q = integral(FUN,A,B,PARAM1,VAL1,PARAM2,VAL2,...) performs the


integration with specified values of optional parameters. The available
parameters are

'AbsTol', absolute error tolerance


'RelTol', relative error tolerance

integral attempts to satisfy |Q - I| <= max(AbsTol,RelTol*|Q|),


where I denotes the exact value of the integral. Usually RelTol
determines the accuracy of the integration. However, if |Q| is
sufficiently small, AbsTol determines the accuracy of the
integration, instead. The default value of AbsTol is 1.e-10, and
the default value of RelTol is 1.e-6. Single precision integrations
may require larger tolerances.

'ArrayValued', FUN is an array-valued function when the input is scalar


When 'ArrayValued' is true, FUN is only called with scalar X, and
if FUN returns an array, integral computes a corresponding array of
outputs Q. The default value is false.

'Waypoints', vector of integration waypoints

If FUN(X) has discontinuities in the interval of integration, the


locations should be supplied as a 'Waypoints' vector. Waypoints
should not be used for singularities in FUN(X). Instead, split the
interval and add the results from separate integrations with
singularities at the endpoints. If A, B, or any entry of the
waypoints vector is complex, the integration is performed over a
sequence of straight line paths in the complex plane, from A to the
first waypoint, from the first waypoint to the second, and so
forth, and finally from the last waypoint to B.

Examples:
% Integrate f(x) = exp(-x^2)*log(x)^2 from 0 to infinity:
f = @(x) exp(-x.^2).*log(x).^2
Q = integral(f,0,Inf)

% To use a parameter in the integrand:


f = @(x,c) 1./(x.^3-2*x-c)
Q = integral(@(x)f(x,5),0,2)

% Specify tolerances:
Q = integral(@(x)log(x),0,1,'AbsTol',1e-6,'RelTol',1e-3)

% Integrate f(z) = 1/(2z-1) in the complex plane over the


% triangular path from 0 to 1+1i to 1-1i to 0:
Q = integral(@(z)1./(2*z-1),0,0,'Waypoints',[1+1i,1-1i])

% Integrate the vector-valued function sin((1:5)*x) from 0 to 1:


Q = integral(@(x)sin((1:5)*x),0,1,'ArrayValued',true)

Class support for inputs A, B, and the output of FUN:


float: double, single

trapz Trapezoidal numerical integration.


Z = trapz(Y) computes an approximation of the integral of Y via
the trapezoidal method (with unit spacing). To compute the integral
for spacing different from one, multiply Z by the spacing increment.

For vectors, trapz(Y) is the integral of Y. For matrices, trapz(Y)


is a row vector with the integral over each column. For N-D
arrays, trapz(Y) works across the first non-singleton dimension.

Z = trapz(X,Y) computes the integral of Y with respect to X using the


trapezoidal method. X can be a scalar or a vector with the same length
as the first non-singleton dimension in Y. trapz operates along this
dimension. If X is scalar, then trapz(X,Y) is equivalent to X*trapz(Y).

Z = trapz(X,Y,DIM) or trapz(Y,DIM) integrates across dimension DIM


of Y. The length of X must be the same as size(Y,DIM)).

Example:
Y = [0 1 2; 3 4 5]
trapz(Y,1)
trapz(Y,2)

Class support for inputs X, Y:


float: double, single

Exercise:

1. Open your numerical integration lecture notes and estimate the integral of the given function,
,from x = 0 to x = using (a)trapezoidal rule and multiple application trapezoidal rule, (b)
Simpson’s 1/3 rule and multiple application Simpson’s 1/3 rule, (c) simpson’s 3/8 rule and multiple application simpson’s
3/8 rule, for segments n = 2, 4, 6, and 8 segments. (Note: use four (4) decimal places and the true value equal is equal to
0.125)

2. Improve the trapezoidal integral estimates of the given function in problem (1) using Romberg integration.
Answer: (trapezoidal rule and multiple application trapezoidal rule)
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________

Syntax:
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
Answer: (Simpson’s 1/3 rule and multiple application Simpson’s 1/3 rule)
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________

Syntax:
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
Answer: (simpson’s 3/8 rule and multiple application simpson’s 3/8 rule)
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________

Syntax:
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
Answer: (Romberg integration)
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________

Syntax:
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
Name:__________________________________ Date:_____________________
Course & Section:_________________________ Program & Year:____________

Experiment 4
Numerical Differentiation

Objectives
By the end of the experiment, the student should be able to
 define the different numerical differentiation methods.
 analyze the different techniques in numerical differentiation.
 demonstrate the usage of matlab in computing numerical differentiation.
 construct a script file for numerical differentiation operation.

Tools Required
 Personal Computer/Desktop Computer/Computer Workstation
 MATLAB software

MATLAB FUNCTIONS

diff Difference and approximate derivative.


diff(X), for a vector X, is [X(2)-X(1) X(3)-X(2) ... X(n)-X(n-1)].
diff(X), for a matrix X, is the matrix of row differences,
[X(2:n,:) - X(1:n-1,:)].
diff(X), for an N-D array X, is the difference along the first
non-singleton dimension of X.
diff(X,N) is the N-th order difference along the first non-singleton
dimension (denote it by DIM). If N >= size(X,DIM), diff takes
successive differences along the next non-singleton dimension.
diff(X,N,DIM) is the Nth difference function along dimension DIM.
If N >= size(X,DIM), diff returns an empty array.

Examples:
h = .001; x = 0:h:pi;
diff(sin(x.^2))/h is an approximation to 2*cos(x.^2).*x
diff((1:10).^2) is 3:2:19

If X = [3 7 5 0 9 2]
then diff(X,1,1) is [-3 2 -3], diff(X,1,2) is [4 -2 9 -7],
diff(X,2,2) is the 2nd order difference along the dimension 2, and
diff(X,3,2) is the empty matrix.

Exercise:

1. Open your numerical differentiation lecture notes and estimate the derivative of the function, x2cosx at xi= 1 using
forward finite divided differences at step sizes h = 0.1 and h = 0.01 applying the truncated and the more accurate
formula for h = 0.01 step size.

2. Open your numerical differentiation lecture notes and estimate the derivative of the function, x2cosx at xi= 1 using
backward finite divided differences at step sizes h = 0.1 and h = 0.01 applying the truncated and the more accurate
formula for h = 0.01 step size.
3. Open your numerical differentiation lecture notes and estimate the derivative of the function, x2cosx at xi= 1 using
centered finite divided differences at step sizes h = 0.1 & h = 0.01 applying the truncated and the more accurate formula
for h = 0.01 step size.

4. Open your numerical differentiation lecture notes and improved the estimated derivative of the function, x2cosx at
xi= 1 using Richardson’s extrapolation on the finite divided differences techniques at step sizes h = 0.1 & h = 0.01.
Answer: (forward finite divided differences)
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________

Syntax:
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
Answer: (backward finite divided differences)
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________

Syntax:
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
Answer: (centered finite divided differences)
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________

Syntax:
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
Answer: (Richardson’s extrapolation)
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________

Syntax:
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________
__________________________________________________________________________________________________

You might also like