Notes About Numerical Methods With Matlab
Notes About Numerical Methods With Matlab
Jaan Kiusalaas
This is an easy example of how Matlab can solve simultaneous equations 𝐀𝐱 = 𝐛 by left division:
0.2500
0.5000
0.6250
Transpose of A in Matlab:
>> A'
ans =
2 -1 0
1 2 1
0 2 4
In order to create a new one you have to click New in Matlab, Write or paste the code and save
the file in the predefined path. Then you can use the function as any other in Matlab.
This is the way we can plot two functions in the same plot:
>> x = 0:0.2:pi; % Create x-array
>> y = sin(x); % Create y-array
>> plot(x,y,’k:o’) % Plot x-y points with specified color
% and symbol (’k’ = black, ’o’ = circles)
>> hold on % Allow overwriting of current plot
>> z = cos(x); % Create z-array
>> plot(x,z,’k:x’) % Plot x-z points (’x’ = crosses)
>> grid on % Display coordinate grid
>> xlabel(’x’) % Display label for x-axis
>> ylabel(’y’) % Display label for y-axis
>> gtext(’sin x’) % Create mouse-movable text
>> gtext(’cos x’)
Systems of Linear Algebraic Equations
A system of n linear equations in n unknowns has a unique solution, provided that
the determinant of the coefficient matrix is nonsingular: |𝐀| ≠ 𝟎.
However, if the determinant is different from zero but it is very small compared with the
coefficients of the matrix, the system is said to be ill conditioned. This means that round off error
will most likely affect the solution, and therefore the solution is untrustable:
2 1 𝑥 3
[ ] [𝑦] = [ ]
2 1.001 0
Solution:
𝑥 1501.5
[𝑦] = [ ]
−3000
2 1
But, since 𝑑𝑒𝑡 [ ] = 0.002, we expect this solution to be very sensitive to small changes
2 1.001
on the coefficients or round off errors. Let’s suppose we change the system to:
2 1 𝑥 3
[ ] [𝑦] = [ ]
2 1.002 0
As you may notice, a change of 0.1% on one of the coefficients causes a 100% change in the
solution.
The function gauss combines the elimination and back substitution phases. This is already saved in
the predetermined path and it is working.
It is possible to show that any square matrix A can be expressed as a product of a lower triangular
matrix L and an upper triangular matrix U:
𝐀 = 𝐋𝐔
After decomposing A it is easy to solve the equations Ax=b by writing the equation as LUx=b and
using notation Ux=y. This forms equation Ly=b which is directly resolvable.
L and U are not unique matrixes for each matrix A, so we need to apply certain constraints in order
to obtain unique LU decomposition. The most common method of decomposition are the
following. In each one of them, the involved constraints are given
Choleski’s decomposition (𝐋 = 𝐔 𝑇 )
Coefficients matrices that are sparsely populated means that most of the elements of the matrix
are zero. If all non zero elements are clustered about the leading diagonal the matrix is said to be
banded:
The banded structure of a coefficient matrix can be exploited to save storage and computation
time. The matrix shown above has a bandwidth of three, since there are at most three nonzero
elements in each row (or column). Such a matrix is called tridiagonal.
Functions LUdec3 and LUsol3; LUdec5 and LUsol5 have been saved to solve equations involving
tridiagonal matrixes. Decomposition and solution for pentadiagonal matrixes have been tested
and are working properly.
If the matrix A is symmetric, then the LU decomposition can be presented in the form:
𝐀 = 𝐋𝐔 = 𝐋𝐃𝐋𝑇
Bandwith 5 matrixes are found in the solution of fourth order differential equations by finite
differences.
Reordering the coefficients of a matrix is important when the first element is zero or is too much
small compared to the other coefficients.
An n× n matrix A is said to be diagonally dominant if each diagonal element is larger than the sum
of the other elements in the same row:
If the matrix A is diagonally dominant it can be shown that the solution does not benefit from
pivoting, so reordering the equations should allow us getting a matrix as close as possible to a
diagonally dominant matrix.
This is a tridiagonal that can be solved with LUdec3 and LUsol3. We solve the system first with left
division:
>> k1=10;
>> k2=10;
>> k3=10;
>> k4=5;
>> k5=5;
>> A1=[k1+k2 -k2 0 0 0];
>> A2=[-k2 k2+k3 -k3 0 0];
>> A3=[0 -k3 k3+k4 -k4 0];
>> A4=[0 0 -k4 k4+k5 -k5];
>> A5=[0 0 0 -k5 k5];
>> A=[A1;A2;A3;A4;A5];
>> W1=100;
>> W3=100;
>> W5=100;
>> W2=50;
>> W4=50;
>> W=[W1;W2;W3;W4;W5];
>> x=A\W
x=
40.0000
70.0000
95.0000
125.0000
145.0000
And we confirm that we get the same solution as with left division.
Iterative methods
Iterative, or indirect methods, start with an initial guess of the solution x and then repeatedly
improve the solution until the change in x becomes negligible. The initial guess for x plays no role
in determining whether convergence takes place—if the procedure converges for one starting
vector, it would do so for any starting vector.
The equation
∑ 𝐴𝑖𝑗 𝑥𝑗 = 𝑏𝑖
𝑗=1
Which is equal to:
𝑛
𝐴𝑖𝑖 𝑥𝑖 + ∑ 𝐴𝑖𝑗 𝑥𝑗 = 𝑏𝑖
𝑗=1
𝑗≠𝑖
𝑛
1
𝑥𝑖 = 𝑏𝑖 − ∑ 𝐴𝑖𝑗 𝑥𝑗
𝐴𝑖𝑖
𝑗=1
𝑗≠𝑖
( )
When using function gaussSeidel remember that an iterative function has to be called by the user
in a different m file from the user. Example fex2_17 is an example that solves matrixes like:
(This matrix appears in FEM second order differential equations with periodic boundary
conditions)
-4.5000
-4.0000
-3.5000
-3.0000
-2.5000
-2.0000
-1.5000
-1.0000
-0.5000
0.0000
0.5000
1.0000
1.5000
2.0000
2.5000
3.0000
3.5000
4.0000
4.5000
5.0000
The displacement formulation for a plane truss is similar to that of a mass–spring system. The
differences are: (1) the stiffnesses of the members are ki = (E A/L)i , where E is the modulus of
elasticity, A represents the crosssectional area and L is the length of the member; (2) there are two
components of displacement at each joint. For the statically indeterminate truss shown the displacement
formulation yields the symmetric equations Ku = p, where
And
2 -1 0 0 0
-1 2 -1 0 0
0 -1 2 -1 0
0 0 -1 2 -1
0 0 0 -1 2
In interpolation we construct a curve through the data points. In doing so, we make the implicit
assumption that the data points are accurate and distinct. Curve fitting is applied to data that
contain scatter (noise), usually due to measurement errors. Here we want to find a smooth curve
that approximates the data in some sense.
𝑥 − 𝑥1 𝑥 − 𝑥2 𝑥 − 𝑥𝑖−1 𝑥 − 𝑥𝑖+1 𝑥 − 𝑥𝑛
𝑙𝑖 (𝑥) = ( )( )…( )( )…( )
𝑥𝑖 − 𝑥1 𝑥𝑖 − 𝑥2 𝑥𝑖 − 𝑥𝑖−1 𝑥𝑖 − 𝑥𝑖+1 𝑥𝑖 − 𝑥𝑛
𝑛
𝑥 − 𝑥𝑗
=∏ , 𝑖 = 1,2 … , 𝑛
𝑥𝑖 − 𝑥𝑗
𝑗=1
𝑗≠𝑖
are called the cardinal functions. It is useful to remember this multiplication does not include the
corresponding term of 𝑥𝑖 .
Newton’s method is a very useful method when we need to have a polynomial equation, if we
need only a value on a determined point then we can use the Neville’s method better.
Neville’s method
Function Neville performs this method in a single step.
Polynomial interpolation should be carried out with the fewest feasible number of
data points. Linear interpolation, using the nearest two points, is often sufficient if
the datapoints are closely spaced. Three to six nearest-neighbor points produce good
results in most cases. An interpolant intersecting more than six points must be viewed
with suspicion.
If there are more than a few data points, a cubic spline is hard to beat as a global interpolant. It is
considerably “stiffer” than a polynomial in the sense that it has less tendency to oscillate between
data points. Because the strip is unloaded between the pins, each segment of the spline curve is a
cubic polynomial.
Example of use of spline functions:
Use natural cubic spline to determine y at x = 1.5. The data points are
X 1 2 3 4 5
Y 0 1 0 1 0
>> xData=[1:1:5];
>> yData=[0 1 0 1 0];
>> k = splineCurv(xData,yData)
k=
0
-4.2857
5.1429
-4.2857
0
>> y = splineEval(xData,yData,k,1.5)
y=
0.7679
>>
Least-Squares Fit
Thus curve fitting consists of two steps: choosing the form of f (x), followed by computation of the
parameters that produce the best fit to the data.
In the case of a straight line 𝒇(𝒙) = 𝒂 + 𝒃𝒙, the function to be minimized is:
𝑛
∑ 𝑦𝑖 (𝑥𝑖 − 𝑥)
𝑏=
∑ 𝑥𝑖 (𝑥𝑖 − 𝑥)
𝑎 = 𝑦 − 𝑥𝑏
𝐀𝐚 = 𝐛
Where
𝑛
𝑗+𝑘−2
𝐴𝑘𝑗 = ∑ 𝑥𝑖
𝑖=1
𝑏𝑘 = ∑ 𝑥𝑖𝑘−1 𝑦𝑖
𝑖=1
This matrix equation can be solved by any of the previously studied methods (Gauss, LU, Choleski)
M files polynFit and stdDev also can solve this. When using this function remember that m is the
grade of the polynomial plus 1. Also remember the answer from the function are the coefficients
ordered descendent by the grade of x:
And:
p=
0.5366 1.3321
>> x1=[1:0.1:8];
>> y1=[exp(1.3321)*exp(0.5366*x1)];
>> plot(x,y,'*')
>>hold
>> plot(x1,y1,'-')
The following table shows examples on how to fit several types of functions.
Estructura Observaciones
ans=
a b a = pendiente de la recta
b = ordenada al origen
Ajuste de una curva exponencial
y e x
>>x=[x1 x2 x3 … xn];
Valores de la variable independiente (sin restricción)
>>y=[y1 y2 y3 … yn];
Valores de la variable dependiente (positivos)
>>p=polyfit(x,log(y),1)
Linealización de la variable dependiente
ans =
Los valores deseados son: a, e b
a b
Ajuste de una curva de potencia
y x
>>x=[x1 x2 x3 … xn];
Valores de la variable independiente (positivos)
>>y=[y1 y2 y3 … yn];
Valores de la variable dependiente (positivos)
>>p=polyfit(log10(x),log10(y),1)
Linealización de AMBAS variables
ans =
>>x=[x1 x2 x3 … xn]; 1 a
>>y=[y1 y2 y3 … yn]; Los valores deseados son ,
>>p=polyfit(1./x,1./y,1)
b b
ans =
a b
Example:
Determine the parameters a and b so that 𝑓(𝑥) = 𝑎𝑒 𝑏𝑥 fits the following data in the least-squares
sense.
This is not an m file. By copying and pasting this code in Matlab you can get the polynomial
coefficients that better fit a series of data. The less standard deviation, the better the fitting. (it
has some exceptions, you’d better plot the data also)
% Example 3.10 (Polynomial curve fitting)
xData = [-0.04,0.93,1.95,2.90,3.83,5.0,...
5.98,7.05,8.21,9.08,10.09]';
yData = [-8.66,-6.44,-4.36,-3.27,-0.88,0.87,...
3.31,4.63,6.19,7.4,8.85]';
format short e
while 1
k = input('degree of polynomial = ');
if isempty(k) % Loop is terminated
fprintf('Done') % by pressing ''return''
break
end
coeff = polynFit(xData,yData,k+1)
sigma = stdDev(coeff,xData,yData)
fprintf('\n')
end
Another Example:
The table displays the mass M and average fuel consumption φ of motor vehicles manufactured by
Ford and Honda in 1999. Fit a straight line φ = a + bM to the data and compute the standard
deviation.
Roots of equations
A common problem encountered in engineering analysis is this: given a function f (x), determine
the values of x for which f (x) = 0. The solutions (values of x) are known as the roots of the
equation f (x) = 0, or the zeroes of the function f (x).
Brent’s Method
Brent’s method combines bisection and quadratic interpolation into an efficient root-finding
algorithm. In most problems the method is much faster than bisection alone, but it can become
sluggish if the function is not smooth. It is the recommended method of root finding if the
derivative of the function is difficult or impossible to compute.
M- file Brent calculates the root of a function given a certain bracketed range in which the solution
can be found. The range of the solution should be bracketed trying to avoid the potential
troublesome regions. It is usually convenient to plot the function to visually detect the interval of
the solution.
Determine the root of 𝑓(𝑥) = 𝑥 3 − 10𝑥 2 + 5 = 0 that lies in (0.6, 0.8) with Brent’s method.
The first thing to do is to write an m-file with the given function (it is named fex1 for this example)
Then the function fex1 and the m-file brent can be called as:
0.7346
Newton-Raphson
The Newton–Raphson algorithm is the best-known method of finding roots for a good reason: it is
simple and fast. The only drawback of the method is that it uses the derivative f ‘(x) of the function
as well as the function f (x) itself. Therefore, the Newton–Raphson method is usable only in
problems where f ‘(x) can be readily computed.
The newton Raphson formula can be derived from the Taylor series expansion of f(x) about x. The
formula is:
𝑓(𝑥𝑖 )
𝑥𝑖+1 = 𝑥𝑖 −
𝑓′(𝑥𝑖 )
newtonRaphson m-file performs this task and finds the solution. Since newtonRaphson uses the
function f(x) as well as its derivative, function routines for both (denoted by func and dfunc in the
listing) must be provided by the user.
Example:
Find the smallest positive zero of 𝑓(𝑥) = 𝑥 4 − 6.4𝑥 3 + 6.45𝑥 2 + 20.538𝑥 − 31.752
Solution:
>> root =
2.1000
When using this function, take care with solutions that are close to a point where the derivative is
close to zero (this previous is a good example of that).
Find a solution of:
newtonRaphson2(@fex4_9,[1;1;1])
ans =
0.5991
2.3959
2.0050
Where [1;1;1] is an initial guess of the solution, @fex4_9 is a call of the matrix including the
system of equations.
Example:
Determine the two roots of 𝑠𝑖𝑛𝑥 + 3𝑐𝑜𝑠𝑥 − 2 = 0 that lie in the interval (−2, 2). Use the Newton-
Raphson method.
function f = funcBetaFreq(b)
%Function used with example of natural frequency of a beam
f = cosh(b)*cos(b)+1;
function df = dfuncBetaFreq(b)
%Derivative of the function associated with natural frequency
% of a beam
df=-cosh(b)*sin(b)+cos(b)*sin(b);
By looking closer, we realize that we have a solution in each one of the followings intervals:
(0-2)
(4-6)
(7,8)
𝛽 4 𝐸𝐼 𝛽𝑖2 𝐸𝐼
𝑓𝑖 = √ 𝑖2 3 = √
4𝜋 𝑚𝐿 2𝜋 𝑚𝐿3
>> f1=(root1^2/(2*pi))*(sqrt(E*I/(m*L^3)))
f1 =
2.5166
>> f2=(root2^2/(2*pi))*(sqrt(E*I/(m*L^3)))
f2 =
15.7713
>> f3=(root3^2/(2*pi))*(sqrt(E*I/(m*L^3)))
f3 =
44.1601
The real zeroes of polynomials with real coefficients can always be computed by one of the
methods already described. But if complex roots are to be computed, it is best to use a method
that specializes in polynomials. Here we present a method due to Laguerre, which is reliable and
simple to implement.
Before proceeding to Laguerre’s method, we must first develop two numerical tools that are
needed in any method capable of determining the zeroes of a polynomial.
Here is the first tool, a function that evaluates a polynomial and its derivatives:
Deflation of Polynomials
After a root r of Pn(x) = 0 has been computed, it is desirable to factor the polynomial as follows:
Which is called synthetic division. This can be done with Horner’s deflation algorithm which is the
second tool:
b(1) = a(1);
for i = 2:n
b(i) = a(i) + r*b(i-1);
end
The procedure for finding a zero of a general polynomial by Laguerre’s formula is:
1. Let x be a guess for the root of Pn(x) = 0 (any value will do).
2. Evaluate 𝑃𝑛 (𝑥), 𝑃𝑛′ (𝑥) and 𝑃𝑛′′ (𝑥)n using evalpoly function.
3. Compute G(x) and H(x) from the following equations:
4. Determine the improved root r from the following equation choosing the sign that results in the
Larger magnitude of the denominator (this can be shown to improve convergence)
𝑛
𝑥−𝑟 =
𝐺(𝑥) ± √(𝑛 − 1)[𝑛𝐻(𝑥) − 𝐺 2 (𝑥)]
5. Let x ←r and repeat steps 2–5 until |Pn(x)| < ε or |x − r| < ε, where ε is the error
tolerance.
The function polyRoots in computes all the roots of Pn(x) = 0, where the polynomial Pn(x) defined
by its coefficient array a = [a1, a2, a3, . . . , an+1].
Example:
Use polyRoots to compute all the roots of 𝑥 4 − 5𝑥 3 − 9𝑥 2 + 155𝑥 − 250 = 0
>> polyroots([1 -5 -9 155 -250])
ans =
2.0000
4.0000 - 3.0000i
4.0000 + 3.0000i
-5.0000
Numerical Differentiation
𝑑𝑛𝑦
Given the function 𝑓(𝑥), compute 𝑑𝑥 𝑛 at given 𝑥.
If we only keep the first term on the second hand we get the first central difference
approximation:
Noncentral finite differences can also be obtained from the first equations shown:
If we only keep the first term on the second hand we get the first forward and backward
difference approximation:
The third and fourth derivatives can be derived in a similar fashion. The results are shown in the
following tables:
The effect on the roundoff error when calculating by this method, can be profound. However, we
can obtain some relief by taking the following precautions:
EXAMPLE 5.1
Given the evenly spaced data points
compute 𝑓′(𝑥) and 𝑓′′(𝑥) at 𝑥 = 0 and 𝑥 = 0.2 using finite difference approximations of 𝑂(ℎ2 ).
Solution
From the forward difference formulas:
Note the forward and backward formulas are used with data in the extremes (first and last data)
and central formulas are used with central values.
EXAMPLE 5.3
The linkage shown has the dimensions a = 100 mm, b = 120 mm, c = 150 mm and d = 180 mm. It
can be shown by geometry that the relationship between the angles α and β is:
The following table was obtained by solving this transcendental equation for β and several values
of α:
M files newtonRaphson, FourLinkedMech and dFourLinkedMech were used to obtain the table.
newtonRaphson was used with [1 2] interval and the default tolerance as shown:
If link AB rotates with the constant angular velocity of 25 rad/s, use finite difference
approximations of 𝑂(ℎ2 ) to tabulate the angular velocity dβ/dt of link BC against α.
We know that:
where dβ/dα is computed from finite difference approximations using the data in the table.
Forward and backward differences of 𝑂(ℎ2 ) are used at the endpoints, central differences
elsewhere. Note that the increment of α is:
Derivatives by Interpolation
If f (x) is given as a set of discrete data points, interpolation can be a very effective means of
computing its derivatives. The idea is to approximate the derivative of f (x) by thederivative of the
interpolant.Thismethodis particularlyuseful if the datapoints are located at uneven intervals of x,
when the finite difference approximations listed in the last article are not applicable.
through n data points and then evaluate its derivatives at the given x.
Example 5.4
Given the data:
−0.7714
𝐚 = [ 1.5075 ]
−0.1930
>> k =
0
-0.4258
-0.3774
-0.3880
-0.5540
0
EXAMPLE 5.5
Determine f’(0) and f’(1) fromthe following noisy data
We can use the program in example 3.1 to determine the best polynomial fitting:
degree of polynomial = 2
coeff =
-7.0240e-001
6.4704e-001
2.0262e+000
sigma =
3.6097e-002
degree of polynomial = 3
coeff =
4.0521e-001
-1.5533e+000
1.0928e+000
1.9921e+000
sigma =
8.2604e-003
degree of polynomial = 4
coeff =
-1.5329e-002
4.4813e-001
-1.5906e+000
1.1028e+000
1.9919e+000
sigma =
9.5193e-003
degree of polynomial =
Done
So:
𝑓(𝑥) ≈ 𝑎1 𝑥 3 + 𝑎2 𝑥 2 + 𝑎3 𝑥 + 𝑎4
𝑓 ′ (𝑥) ≈ 3𝑎1 𝑥 2 + 2𝑎2 𝑥 + 𝑎3
𝑓 ′ ′(0) ≈ 𝑎3 = 1.093
′(1)
𝑓′ ≈ 3𝑎1 (1)2 + 2𝑎2 (1) + 𝑎3 = −0.798
MATLAB Functions
d = diff(y) returns the differences d(i) = y(i+1) - y(i). Note that length(d) = length(y) - 1.
dn = diff(y,n) returns the nth differences; e.g., d2(i) = d(i+1) - d(i), d3(i) = d2(i+1) - d2(i), etc. Here
length(dn) = length(y) - n.
d = gradient(y,h) returns the finite difference approximation of dy/dx at each point, where h is the
spacing between the points.
d2 = del2(y,h) returns the finite difference approximation of (d2y/dx2)/4 at each point, where h is
the spacing between the points.
Numerical Integration
Numerical integration, also known as quadrature, is intrinsically a muchmore accurate procedure
than numerical differentiation. Quadrature approximates the definite integral:
𝑏
∫ 𝑓(𝑥)𝑑𝑥
𝑎
𝐼 = ∑ 𝐴𝑖 𝑓(𝑥𝑖 )
𝑎
Methods of numerical integration can be divided into two groups: Newton–Cotes formulas and
Gaussian quadrature.
Newton–Cotes Formulas
We divide the range of integration (𝑎, 𝑏) into 𝑛 − 1 equal intervals of length ℎ = (𝑏 − 𝑎)⁄(𝑛 − 1)
Next we approximate 𝑓(𝑥) by a polynomial of degree 𝑛 − 1 that intersects all nodes. Lagrange’s
formula of this polynomial is:
𝑛
𝐼 = ∑ 𝐴𝑖 𝑓(𝑥𝑖 )
𝑎
Where:
𝑏
𝐴𝑖 = ∫ 𝑙𝑖 (𝑥)𝑑𝑥
𝑎
𝑖 = 1, 2, … 𝑛
Trapezoidal Rule
If n=2 then
(𝑥 − 𝑥2 ) (𝑥 − 𝑏)
𝑙𝑖 (𝑥) = =−
(𝑥1 − 𝑥2 ) ℎ
Therefore:
1 𝑏 ℎ
𝐴1 = 𝐴2 = − ∫ (𝑥 − 𝑏)𝑑𝑥 =
ℎ 𝑎 2
𝑏
ℎ
𝐼 = ∑ 𝐴𝑖 𝑓(𝑥𝑖 ) = [𝑓(𝑎) + 𝑓(𝑏)]
2
𝑎
The function trapezoid computes I(h), given I(2h) from the recursive trapezoidal rule. We can
𝑏
compute ∫𝑎 𝑓(𝑥)𝑑𝑥 by calling trapezoid repeatedly with k = 1, 2, . . . until the desired precision is
attained.
Simpson’s rule.
Simpson’s 1/3 rule can be obtained from Newton-Cotes formula with n=3.
ℎ 𝑎+𝑏
𝐼= [𝑓(𝑎) + 4𝑓 ( ) + 𝑓(𝑏)]
3 2
The composite Simpson’s 1/3 rule is perhaps the best known method of numerical integration.
Simpson’s 1/3 rule requires the number of panels to be uneven. If this condition is not satisfied,
we can integrate over the first (or last) three panels with Simpson’s 3/8 rule:
3ℎ
𝐼= [𝑓(𝑥1 ) + 3𝑓(𝑥2 ) + 3𝑓(𝑥3 ) + 𝑓(𝑥4 )]
8
Example 6.2
𝜋
Evaluate the bounds on ∫0 𝑠𝑖𝑛(𝑥) 𝑑𝑥 with the composite trapezoidal rule using (1) eight panels
and (2)sixteen panels.
𝜋 𝜋(𝑖−1)
(1) With 8 panels there are 9 nodes spaced ℎ = . The abscissas of the nodes are 𝑥𝑖 = ,
8 8
𝑖 = 1, 2, … 9
𝜋
(2) With 16 panels there are 17 nodes spaced ℎ = 16
The integral can be quickly evaluated in Matlab with the following command line:
I=polyval(polyint(polyfit(x,y,Grade)),Lsup)-polyval(polyint(polyfit(x,y,Grade)),Linf)
Where x and y are the vector of dependent and independent variables respectively, Grade Is the
grade of the polynomial (usually is the number of paired points minus 1) and Lsup and Linf are the
superior and inferior limits of the integral.
For this example:
>> x=0:pi/8:pi;
>> y=sin(x);
>> Lsup=pi;
>> Linf=0;
>> Grade=7;
>> I=polyval(polyint(polyfit(x,y,Grade)),Lsup)-polyval(polyint(polyfit(x,y,Grade)),Linf)
I=
2.00000740950669
4.0993
The function trapezoid computes I(h) given I(2h). We can compute the integral calling trapezoid
repeateadly until the desired precision.
𝜋
Example: Evaluate ∫0 √𝑥𝑐𝑜𝑠𝑥 wit six digits accuraccy.
The program listed below utilizes the function trapezoid. Apart from the value of the integral, it
displays the number of function evaluations used in the computation.
Romberg Integration
Romberg integration combines the composite trapezoidal rule with Richardson extrapolation.
It starts with the computation of R1,1 = I1 (one panel) and R2,1 = I2 (two panels) from the
trapezoidal rule. The leading error term c1h2 is then eliminated by Richardson extrapolation.
Using p = 2 (the exponent in the error term) in and denoting the result by R2,2, we obtain:
The next step is to calculate R3,1 = I3 (four panels) and repeat Richardson extrapolationwith R2,1
and R3,1, storing the result as R3,2:
Both elements of the second column have an error of the form c2h4, which can also
be eliminatedwith Richardson extrapolation. Using p = 4:
This process is continued until the difference between two successive diagonal terms becomes
sufficiently small. M file romberg performs this algorithm.
𝜋
Example 6.6 Use Romberg integration to evaluate ∫0 𝑠𝑖𝑛𝑥𝑑𝑥.
I=
2.00000000000132
numEval =
33
Another example:
√𝜋
Use Romberg integration to evaluate ∫0 2𝑥 2 𝑐𝑜𝑠𝑥 2 𝑑𝑥
Integral =
-0.89483146948416
numEval =
129
As you can see, Romberg integration is more effiecient integration algorithm than the trapezoidal
rule.
Estimate the integral as accurate as possible with this set of data:
1.37308542912898
𝜋⁄2
𝑑𝜃
ℎ(𝜃0 ) = ∫
0 √1 − 𝑠𝑖𝑛2 (𝜃0 ⁄2)(𝑠𝑖𝑛2 𝜃)
Compute the period of a pendulum of length L=0.5m at an amplitud of 15°, 30° and 45°
T(15°)= 1.41922992361407
T(30°)= 1.41923880292893
T(45°)= 1.41925292849723
𝑙
For comparison, the approximation 𝑇 = 2𝜋√𝑔 valid for small amplitudes results in
1.41922689511373. This approximation is in fact very precise for almost all practical purposes.
1
𝑡𝑎𝑛−1 √𝑥 2 + 2 𝑑𝑥
𝐴=∫
0 √𝑥 2 + 2 𝑥 2 + 1
Evaluate this integral numerically using romberg function and compare the result with the
5𝜋2
analytical result which is 𝐴 = 96
I=
0.51404189589959
>> ExactSolution=5*pi^2/96
ExactSolution =
0.51404189589007
Gaussian Integration
Gaussian cuadrature is good at estimating integrals of the form:
𝑏
∫ 𝑤(𝑥) 𝑓(𝑥)𝑑𝑥
𝑎
𝐼 = ∑ 𝐴𝑖 𝑓(𝑥𝑖 )
𝑖=1
The difference lies in the way that the weights Ai and nodal abscissas xi are determined.
Practical methods of finding xi and Ai require some knowledge of orthogonal polynomials. But
fortunate some classical Gaussian integration formulas that have been accurately computed and
tabulated and can be used without the knowledge of the theory behind.
We list here some classical Gaussian integration formulas. The tables of nodal abscissas and
weights, covering n = 2 to 6, have been rounded off to six decimal places. These tables should be
adequate for hand computation, but in programming you may need more precision or a larger
number of nodes. In that case you should consult other references.
This is the most used Gaussian integration formula is the Gauss-Legendre quadrature:
1 𝑛
∫ 𝑓(𝜉) ≈ ∑ 𝐴𝑖 𝑓(𝜉𝑖 )
−1 𝑖=1
The nodes are arranged symmetrically about ξ = 0, and the weights associatedwith a symmetric
pair of nodes are equal. For example, for n = 2 we have ξ 1 = −ξ 2 and A1 = A2.
𝑏
To applyGauss–Legendre quadrature to the integral ∫𝑎 𝑓(𝑥)𝑑𝑥, we must first map the integration
range (a, b) into the “standard” range (−1, 1˙):
𝑏+𝑎 𝑏−𝑎
𝑥= + ξ
2 2
Then apply:
The function gaussNodes computes the nodal abscissas xi and the corresponding weights Ai used
in Gauss–Legendre quadrature. It can be shown that the approximate values of the
abscissas are:
𝜋(𝑖 − 0.25)
𝑥𝑖 = 𝑐𝑜𝑠 ( )
𝑛 + 0.5
𝑏
The function gaussQuad evaluates ∫𝑎 𝑓(𝑥)𝑑𝑥 with Gauss Legendre cuadrature using n nodes.
1
Example: Evaluate ∫−1(1 − 𝑥 2 )3⁄2 𝑑𝑥 using gaussian integration.
We can use Gaussian Chebyshev formulas. We write:
1 1
∫ (1 − 𝑥 2 )3⁄2 𝑑𝑥 = ∫ (1 − 𝑥 2 )−1⁄2 𝑓(𝑥)𝑑𝑥
−1 −1
1 3 2 2 2 2 2 2
𝜋 𝜋 √3 √0 √3 3𝜋
∫ (1 − 𝑥 2 )3⁄2 𝑑𝑥 = ∑ 𝑓(𝑥𝑖 ) = [(1 − ( ) ) + (1 − ( ) ) + (1 − ( ) ) ] =
−1 3 3 2 2 2 8
𝑖=1
Example:
0.5
Use Gaussian integration to evaluate ∫0 𝑐𝑜𝑠𝜋 𝑥𝑙𝑛𝑥𝑑𝑥.
For the first equation we use Gauss cuadrature with logaritmic singularity and n=4:
The second integral can be evaluated with Gauss Legendre cuadrature as it is free of singularities.
From which:
Therefore:
Which can be evaluated with Gauss Hermite formula using only two nodes:
Example 6.12
3
Evaluate numerically ∫1.5 𝑓(𝑥)𝑑𝑥 where 𝑓(𝑥) is represented by the unevenly spaced data:
3
We aproximate 𝑓(𝑥) by the polynomial 𝑃5 (𝑥) an then we evaluate ∫1.5 𝑃5 (𝑥)𝑑𝑥 with Gauss
Legendre formula.
We obtain the abscissas of the nodes:
We compute the values of the interpolant at the nodes. This can be done with newtonPoly, neville
or polyval. The results are:
The data point fall on cosx so the analytical value of the integral is 0.85638, which is within round
off error.
Multiple Integrals
Multiple integrals can also be evaluated by quadratue.
For cuadrilateral elements:
Where xk and yk are the values of the coordinates of the cuadrilateral element.
These both equations use the determinant of the jacobian:
Then:
Which yields to:
And finally:
The function triangleQuad computes ∬ 𝑓(𝑥, 𝑦)𝑑𝑥𝑑𝑦 over a triangular region using the cubic
formula. The triangle is defined by its corner coordinate arrays x and y and they must be supplied
in a counterclockwise direction.
Example:
1 1 2
Evaluate 𝐼 = ∬ (2 (𝑥 2 + 𝑦 2 ) − 6 (𝑥 3 − 3𝑥𝑦 2 ) − 3) 𝑑𝑥𝑑𝑦 over the triangular region. Use (1)
cuadrilateral and (2) triangular integration. This equation is usually called Prandtl stress function
for torsion of a bar with the cross section shown.
(1) This can be done by:
>> I = gaussQuad2(@fex6_16,[-1;-1;2;2],...
[sqrt(3);-sqrt(3);0;0],3)
I=
-1.5588
Example: The coordinates of a triangle are (0,0), (16,10) and (12,20). Compute ∬(𝑥 2 − 𝑦 2 )𝑑𝑥 𝑑𝑦
over this triangle.
As 𝑓(𝑥, 𝑦) is cuadratic quadrature over the three integration points will suffice:
(6,10)
(8,5)
(14,15)
>> I=triangleQuad(@fex6_17,[0;16;12],[0;10;20])
-1.8000e+003
Integrate
1 1
∫ ∫ (1 − 𝑥 2 )(1 − 𝑦 2 ) 𝑑𝑥 𝑑𝑦
−1 −1
𝑑𝑦
= 𝑓(𝑥, 𝑦)
𝑑𝑥
Any ordinary differential equation can be converted into 𝑛 first order equations:
The solution requires 𝑛 auxiliary conditions. If these conditions are specified at the same value of
𝑥 the problem is said to be an initial problem:
We have to take into account that a numerical solution of a differential equation is a table x-y
listed at discrete intervals of 𝑥.
Example 7.1
Given that:
𝑑𝑦
+ 4𝑦 = 𝑥 2
𝑑𝑥
𝑦(0) = 1
Determine 𝑦(0.2) with the fourth order Taylor series method using a single integration step. Also
compute the estimated error. Compare with the actual value given by the analytical solution:
31 −4𝑥 1 2 1 1
𝑦= 𝑒 + 𝑥 − 𝑥+
32 4 8 32
They are evaluated in 0 because we already know the value of y(0)=1. Remember that x was
supressed in these evaluations as we are evaluating when x=0.
Which is evaluated:
0.24
𝐸= [256(0.4539) − 64(0.2)2 + 32(0.2) − 8 − 248] = −0.0018
5!
The analytical solution leads to:
Numerical Integration
Numerical Differentiation
Fit of a curve
Interpolation of a curve
Find the roots of equation
Systems of Linear Algebraic Equations