Undetermined Coefficients

In previous sections, we approximated the definite integral and derivatives of any given function $y=f(x)$ based on its Lagrange interpolation $L_n(x)$:

$\displaystyle \frac{d}{dx}f(x_j)$ $\displaystyle \approx$ $\displaystyle \frac{d}{dx}L_n(x_j)
=\frac{d}{dx}\left[\sum_{i=0}^n f(x_i)l_i(x_j)\right]$  
  $\displaystyle =$ $\displaystyle \sum_{i=0}^n f(x_i)\frac{d}{dx}l_i(x_j)
=\sum_{i=0}^n d_{ij} f(x_i),\;\;\;\;\;(j=0,\cdots,n)$ (79)
$\displaystyle \int_a^b f(x)\,dx$ $\displaystyle \approx$ $\displaystyle \int_a^b L_n(x)\,dx
=\int_a^b \left[\sum_{i=0}^n f(x_i)l_i(x)\right]\,dx$  
  $\displaystyle =$ $\displaystyle \sum_{i=0}^n f(x_i)\int_a^b l_i(x)\,dx
=\sum_{i=0}^n c_if(x_i)$ (80)

whre

$\displaystyle d_{ij}=d_i(x)\big\vert _{x=x_j}=\frac{d}{dx}l_i(x)\big\vert _{x=x_j}
=\frac{d}{dx}l_i(x_j)$ (81)

is the coefficient $d_i(x)=l'_i(x)$ in Eq. (12) evaluated at $x=x_j,\;(i,j=0,\cdots,n)$, and $c_i=\int_a^b l_i(x)\,dx$ is given in Eq. (25).

These coefficients can also be obtained by an alternative method of undetermined coefficients. As both $c_i$ and $d_{ij}$ are independent of the specific function $f(x)$ in question, we can find them based on a set of particular functions $f(x)=x^k,\;(k=0,\cdots,n)$:

$\displaystyle I[f]=\int_a^b f(x)\,dx$ $\displaystyle =$ $\displaystyle \int_a^b x^k\,dx=\frac{b^{k+1}-a^{k+1}}{k+1}
=\sum_{i=0}^n c_if(x_i)=\sum_{i=0}^n c_ix_i^k
\;\;\;\;\;\;(k=0,\cdots,n)$ (82)
$\displaystyle D[f]=\frac{d}{dx}f(x_j)$ $\displaystyle =$ $\displaystyle \frac{d}{dx}x^k_j=k x^{k-1}_j
=\sum_{i=0}^n d_{ij} f(x_i)=\sum_{i=0}^n d_{ij}x_i^k
\;\;\;\;\;\;(k,j=0,\cdots,n)$ (83)

We consider $x_i^k$ as the component in the kth row and ith column of a $(n+1)\times (n+1)$ matrix ${\bf A}$ so that the two equations can be expressed in matrix forms as:
    $\displaystyle \left[\begin{array}{cccc}1&1&\cdots&1\\ x_0&x_1&\cdots&x_n\\
x_0...
...b^3-a^3}{3}\\ \vdots\\ \frac{b^{n+1}-a^{n+1}}{n+1}\end{array}\right]
\;\;\;\;\;$or$\displaystyle \;\;\;\;\;\; {\bf Ac}={\bf b}$ (84)
    $\displaystyle \left[\begin{array}{cccc}1&1&\cdots&1\\ x_0&x_1&\cdots&x_n\\
x_0...
...dots&\ddots&\vdots\\
nx_0^{n-1}&nx_1^{n-1}&\cdots&nx_n^{n-1}\end{array}\right]$  
    $\displaystyle \;\;\;\;\;$or$\displaystyle \;\;\;\;\;\;
{\bf AD}={\bf A}[{\bf d}_0,\cdots,{\bf d}_n]={\bf B}
=[{\bf b}_0,\cdots,{\bf b}_n]$ (85)

In the second equation, the jth column ${\bf d}_j=[d_{0j},\cdots,d_{nj}]^T$ of ${\bf D}=[{\bf d}_0,\cdots,{\bf d}_n]$ is composed of the $n+1$ coefficients for approximating $f'(x_j)$. Solving these equation systems we get the coefficients needed:
$\displaystyle {\bf c}$ $\displaystyle =$ $\displaystyle {\bf A}^{-1}{\bf b}$ (86)
$\displaystyle {\bf D}$ $\displaystyle =$ $\displaystyle {\bf A}^{-1}{\bf B},
\;\;\;\;$or$\displaystyle \;\;\;\;
{\bf d}_j={\bf A}^{-1}{\bf b}_j\;\;\;\;\;\;(j=0,\cdots,n)$ (87)

Once ${\bf c}$ and ${\bf D}$ are obtained, the integral and derivatives of any function $f(x)$ represented by its $n+1$ samples in ${\bf f}=[f(x_0),\cdots,f(x_n)]^T$ can be approximated as
$\displaystyle \int_a^b f(x)\,dx$ $\displaystyle =$ $\displaystyle \sum_{i=0}^nc_if(x_i)={\bf c}^T{\bf f}$ (88)
$\displaystyle \frac{d}{dx}f(x_j)$ $\displaystyle =$ $\displaystyle \sum_{i=0}^nd_{ij}f(x_i)={\bf d}_j^T{\bf f}
\;\;\;\;\;(j=0,\cdots,n)$ (89)

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 $f'(x_i)$ and $\int_0^1 f(x)\,dx$ of $f(x)=e^x$, with groun truth:

$\displaystyle \int_0^1 e^x\,dx=e^x\big\vert _0^1=e-1=1.718281828,\;\;\;\;\;\;
\frac{d}{dx}e^x=e^x=2.718281828^x$ (90)

When $n=5$, we have $a=0,\,b=1,\,n=5,\;h=(b-a)/n=1/5$, and the approximation of the integral generated by the code above is $1.71828231$ with relative error $2.82e-07$. The approximated derivatives at $x_0=a,\cdots,x_n=x_5=1$ are:

$\displaystyle \begin{tabular}{c\vert\vert c\vert c\vert r}\hline
j & \mbox{appr...
... & 8.31e-06\\ \hline
5 & 2.718187 & 2.718282 & -3.50e-05\\ \hline
\end{tabular}$    

When $n=7$, $h=(b-a)/n=1/7$, we get the approximated integral $1.71828183$ with error $3.79e-10$, and the approximated derivatives at $x_0=a,\cdots,x_n=x_7=1$ are:

$\displaystyle \begin{tabular}{c\vert\vert c\vert c\vert r}\hline
j & \mbox{appr...
... & 1.59e-08\\ \hline
7 & 2.718282 & 2.718282 & -9.79e-08\\ \hline
\end{tabular}$    

The method of undetermined coefficients can also be used to approximate higher-order derivatives of the given function at any point $x$, based on a set of $m+n+1$ equally spaced points around $x$, with $m$ on its left and $n$ on its right:

$\displaystyle \frac{d^j}{dx^j}f(x)=f^{(j)}(x)\approx \sum_{k=-m}^n c_k f(x+kh),
\;\;\;\;\;\;\; (j=1,\cdots,m+n)$ (91)

To determine the unknown coefficients $c_i$, we replace each term $f(x+kh)$ by the first $m+n+1$ terms of its Taylor expansion:
    $\displaystyle f^{(j)}(x)\approx \sum_{k=-m}^n c_k f(x+kh)$  
  $\displaystyle \approx$ $\displaystyle \sum_{k=-m}^n c_k \left[f(x)+f'(x)(kh)
+\frac{f''(x)}{2}(kh)^2+\cdots+\frac{f^{(m+n)}(x)}{(m+n)!}(kh)^{m+n}\right]$  
    $\displaystyle (j=1,\cdots,m+n)$ (92)

Collecting all terms for each $f^{(i)}(x),\;(i=0,\cdots,m+n)$ on the right-hand side and equating them to the corresponding terms on the left-hand side (only one non-zero term $f^{(j)}(x)$), we obtain a system of $m+n+1$ linear equations:

$\displaystyle \sum_{k=-m}^n c_k\frac{(kh)^i}{i!}=\delta_{ij}
\;\;\;\;$i.e.,$\displaystyle \;\;\;\;
\sum_{k=-m}^n k^i c_k =\left\{\begin{array}{ll}i!/h^i&i=j
\\ 0&i\ne j\end{array}\right.
\;\;\;\;\;\;\;\;\;\;(i=0,\cdots,m+n)$ (93)

Here $k^i$ can be considered as the component in the ith row and kth column of a matrix ${\bf A}$, and the $m+n+1$ equations above can be written in matrix form:

$\displaystyle \left[\begin{array}{lllll}
(-m)^0&(-m+1)^0&\cdots&(n-1)^0&n^0\\
...
...ight]
=\left[\begin{array}{c}0\\ \vdots\\ j!/h^j\\ \vdots\\ 0\end{array}\right]$ (94)

or

$\displaystyle {\bf A}{\bf c}_j={\bf b}_j$ (95)

Here ${\bf b}_j$ on the right-hand side is a vector containing all zero elements except the jth one $b_j=j!/h^j$.

Moreover, we can get ${\bf c}_j$ for the jth derivative $f^{(j)}(x)$ $(j=1,\cdots,m+n$) by solving $m+n$ equations at the same time:

$\displaystyle {\bf A}[{\bf c}_1,\cdots,{\bf c}_{m+n}]
=[{\bf b}_1,\cdots,{\bf b}_{m+n}]$ (96)

to get

$\displaystyle [{\bf c}_1,\cdots,{\bf c}_{m+n}]={\bf A}^{-1}[{\bf b}_1,\cdots,{\bf b}_{m+n}]$ (97)

Note that given $m+n+1$ points, the highest derivative that can be approximated is $f^{(m+n)}(x)$. More points are needed to approximate $f^{(j)}(x)$ if $j>m+n$.

Consider the following examples:

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 $f(x)=e^x$ at $x=1$ with 3, 5, 7, and 9 points and step sizes 1, 1/2, 1/4, 1/8. As can be seen from the approximated $f'(x)$ its relative error listed in the table, higher accuracy is achieved by smaller step sizes and larger number of points used in the approximation:

$\displaystyle \begin{tabular}{c\vert\vert c\vert c\vert c\vert c}\hline
h & \mb...
...05&2.7182597/8.2e-08&2.7182819/2.7e-10&2.7182818/9.5e-13\\ \hline
\end{tabular}$