Example 4. Find the
continuous least squares polynomial of degree n=4
that approximates the function
over
the interval
.
Solution 4.
The set of functions is
.
The inner product ![]()
Since i and j are positive integers, this can be simplified with the command
Therefore, the Gram matrix G is the 5×5 Hilbert matrix, which is a real, symmetric and positive definite matrix.
(a). Form the matrix A and vector B.
Enter the function
, and
the set of functions
,
and compute
for
,
and write down the linear system AC = B to be
solved.
(b). Construct the Cholesky factorization of matrix A.
Invoke the subroutine Cholesky.
Verify the factorization.
(c). Solve the linear system
for the coefficients
using our ForeSub[n] and [BackSub[n] subroutines.
First, solve the lower-triangular system LY = B for Y.
Verify that LY = B.
Second, solve the upper-triangular system UX = Y for X.
Verify that UX = Y.
Therefore X is the solution to LUX =
B. and hence AX = B
And we can verify that it is the solution.
Now use the solution to X make the
coefficients
.
Solve the linear system for the coefficients
using
and the computation
.
Construct the polynomial p[x]. The
coefficients are stored in the array c and
the elements are
.
We are done.
We can graph the polynomial, this is just for fun !
![[Graphics:../Images/CholeskyMod_gr_301.gif]](../Images/CholeskyMod_gr_301.gif)
![[Graphics:../Images/CholeskyMod_gr_306.gif]](../Images/CholeskyMod_gr_306.gif)
![[Graphics:../Images/CholeskyMod_gr_311.gif]](../Images/CholeskyMod_gr_311.gif)
(c) John H. Mathews 2004