

CalculusThis short chapter introduces some basic functions related to numerical differentiation and integration.
8.1 DifferentiationnumEclipse implements three numerical differentiation functions, i.e., diff, gradient and del2. diffdiff is used to calculate the forward differences of a function values given in a vector or a matrix.
It calculates the first order forward differences. If Y is a vector of length l, then it returns a vector of length n  1 such that d(i) = y(i+1)  y(i). If X is a matrix of size (k, l) then it returns a matrix of size (k1, l) such that d(i, j) = y(i + 1, j) = y(i, j).
The second option calculates the n^{th} order forward differences. If Y is a vector of length l, then it returns a vector of length l  n such that d_{n}(i) = d_{n1}(i+1)  d_{n1}(i) and d_{0}(i) = y(i). If X is a matrix of size (k, l) then it returns a matrix of size (kn, l) such that d_{n}(i, j) = d_{n1}(i+1, j)  d_{n1}(i, j) and d_{0}(i, j) = y(i, j).
This variation of the function works like the previous alternatives except that the difference are calculated along the dimension specified by the third argument dim. The legal values of dim are 1 (rowwise) and 2 (columnwise).
> x = rand(3, 4) x = 0.5391 0.6476 0.9679
0.5680
> diff (x,1, 1) ans = 0.2997 0.5897 0.0857
0.2048 > diff (x, 2, 2) ans = 0.2117 0.7202
In the above example, we create a random matrix of size (3, 4). Then we show the first order differences along the rows, diff(x, 1, 1). Then, we show the second order differences along columns, diff(x, 2, 2). gradientgradient of a function calculates the direction of increasing value of the function.
If f is a vector, then gradient calculates the one dimensional numerical gradient of f. In case of a matrix, it returns the x and y components of the gradient, i.e., f_{x}, f_{y}.
> f = rand(3, 4) f =
0.2428 0.9297 0.3220 0.5276
> [fx, fy] = gradient(f) fx =
0.6869 0.0396 0.2010 0.2056 fy =
0.0304 0.6846 0.2034 0.1842
numEclipse adopted the implementation from GNU Octave. del2del2 calculates the finite difference approximation of Laplace's differential operator.
L is a matrix of same size as U, such that each element in L is a difference of corresponding element in X with the average of its four neighbouring elements.
Here h is the spacing spacing at each point.
Here h_{x} and h_{y} are spacings in x and y direction at each point.
_{> u = rand(4)} _{u = } _{0.5124 0.6993 0.8292 0.2036 0.8024 0.2478 0.6449 0.0626 0.2812 0.2102 0.4668 0.8694 0.9626 0.7843 0.3583 0.0825}
_{> l = del2(u, 0.1, 0.2)} _{l = } _{6.4945 1.1620 18.8502 12.9650 18.7224 26.3790 24.4466 18.5615 15.7072 12.0146 4.0869 6.3098 8.8410 1.4557 4.6235 16.1699}
numEclipse adopted the implementation from GNU Octave. 8.2 IntegrationnumEclipse implements three numerical integration functions, i.e., quad, quadl and dblquad. quadThis method evaluates the numerical integral of a given function using adaptive Simpson method.
The first function call approximates the definite integral of function func from a to b with error tolerance 1e6. The second alternative allows a userdefined tolerance in the last argument.
> f = @sin f = @sin
> quad(f, 0, pi) ans = 2.0000
numEclipse adopted the implementation from the paper "Adaptive Quadrature  Revisited" by Gander, W. This paper is available on the internet at http:// www.inf.ethz.ch/personal/gander. quadlThis function is a variation of the above. It approximates the numerical integral using adaptive Lobatto quadrature.
The first function call approximates the definite integral of function func from a to b with error tolerance 1e6. The second alternative allows a userdefined tolerance in the last argument.
> f = @sqrt f = @sqrt
> quadl(f, 0, pi) ans = 3.7122
numEclipse adopted the implementation from the paper "Adaptive Quadrature  Revisited" by Gander, W. This paper is available on the internet at http:// www.inf.ethz.ch/personal/gander. dblquaddblquad approximates the double numerical integral of a given function.
The first function call approximates the definite double integral of function func(x, y) for x from xmin to xmax and y from ymin and ymax with error tolerance 1e6. The second alternative allows a userdefined tolerance in the last argument.
function z = func2(x, y)
> I = dblquad(@func2, 0, 1, 0, 1, 1e8) I = 0.6667
The implementation of this function is based on Gaussian Quadrature on rectangular elements and code is adopted from the book "Numerical Methods in Engineering with MATLAB" by Jaan Kiusalaas.
