In previous sections, we approximated the definite integral
and derivatives of any given function based on its
Lagrange interpolation :
whre
|
(81) |
is the coefficient
in Eq. (12)
evaluated at
, and
is given in Eq. (25).
These coefficients can also be obtained by an alternative method
of undetermined coefficients. As both and
are independent of the specific function in question,
we can find them based on a set of particular functions
:
We consider as the component in the kth row and ith
column of a
matrix so that
the two equations can be expressed in matrix forms as:
In the second equation, the jth column
of
is composed of the
coefficients for approximating .
Solving these equation systems we get the coefficients needed:
Once and are obtained, the integral
and derivatives of any function represented by its
samples in
can be
approximated as
This method of undetermined coefficients is implemented by
the Matlab code below:
A=zeros(n+1); % coefficient matrix on the left
b=zeros(n+1,1); % vector on the right for integral
B=zeros(n+1); % matrix on the right for derivatives
A(1,:)=1; % first row of A
B(1,:)=0; % first row of B
b(1,:)=b-a; % first element of b
for i=1:n % for the remaining n rows
A(i+1,:)=x.^i;
B(i+1,:)=i*x.^(i-1);
b(i+1)=(b^(i+1)-a^(i+1))/(i+1);
end
c=inv(A)*b; % find coefficients for approximating integral
D=inv(A)*B; % find coefficients for approximating derivatives
f(x)*c; % approximated integral
f(x)*D(:,i+1); % approximated derivatives
Example: Find and
of
, with groun truth:
|
(90) |
When , we have
, and
the approximation of the integral generated by the code above is
with relative error . The approximated
derivatives at
are:
When ,
, we get the approximated integral
with error , and the approximated
derivatives at
are:
The method of undetermined coefficients can also be used
to approximate higher-order derivatives of the given function
at any point , based on a set of equally spaced
points around , with on its left and on its right:
|
(91) |
To determine the unknown coefficients , we replace each term
by the first terms of its Taylor expansion:
Collecting all terms for each
on
the right-hand side and equating them to the corresponding terms
on the left-hand side (only one non-zero term
), we
obtain a system of linear equations:
i.e., |
(93) |
Here can be considered as the component in the ith row
and kth column of a matrix , and the equations
above can be written in matrix form:
|
(94) |
or
|
(95) |
Here on the right-hand side is a vector containing all
zero elements except the jth one
.
Moreover, we can get for the jth derivative
) by solving equations at the same time:
|
(96) |
to get
|
(97) |
Note that given points, the highest derivative that can be
approximated is
. More points are needed to approximate
if .
Consider the following examples:
-
Based on the points
, we can find
the coefficients needed for approximating and by
solving the following equation systems:
|
(98) |
Solving the system we get
and
, and
We can also use three points all on either side of . For example,
if , we have:
Solving the equation systems
|
(99) |
we get
and
for approximating and
, respectively:
-
Based on equally spaced points
,
we can approximate any of
by
|
(100) |
The coefficients can be found by solving the following equation
systems
|
(101) |
to get
|
(102) |
and
-
Based on equally space points
,
we can approximate and of
by
|
(104) |
Solving this equation
we get the coefficients in the approximation:
For example, the first two derivative are:
Alternatively, We can also choose to use seven unequally
spaced points, such as
and get:
The Matlab code that implements the method of undetermined
coefficients is shown below.
p=[-m:n]; % all m+n+1 points
k=m+n+1; % total number of points
A=zeros(k); % matrix on left-hand side
b=zeros(k,k-1); % vector on right-hand side
A(1,:)=1; % first row of A
for i=:k-1
A(i+1,:)=p.^i;
b(i+1,i)=factorial(i)/h^i;
end
c=inv(A)*b; % solving linear system to get coefficients
dx=f(x+p*h)*c; % f(x): vector of all m+n+1 function values
% dx: vector of derivatives of order 1 to m+n
Example: Use the method of undetermined coefficients to
find the derivatives of the Gaussian function at
with 3, 5, 7, and 9 points and step sizes 1, 1/2, 1/4, 1/8. As
can be seen from the approximated its relative error
listed in the table, higher accuracy is achieved by smaller step
sizes and larger number of points used in the approximation: