In addition to statistical packages, R has powerful libraries that are useful for Numerical Analysis. R has a package
cmna for computational numerical analysis. Finding zeros of a function and matrix operations are two key topics in Numerical Analysis.
In this blog, we will do exercises on three topics: finding zeros of a function, solving a linear system AX = b, and numerical integration. Newton’s method, secant method functions in
cmna, are used to find an individual zero of a function. Matrix decomposition’s like LU decomposition and Cholesky decomposition can be used to solve the linear system of the form AX = b. We will also see numerical integration methods. Adaptive integration methods can be used for complicated integrals.
Answers to these exercises are available here.
Install and load the package
cmna. Create a function,
f, such that f(x) = sin(x) + log(x). Create another function,
fp, that is the derivative of f(x).
Use Newton’s method to find the zero of the function
f. Use the bisection method to find the zero of the function
f. For both methods, the maximum error should be 0.001 and maximum number of iterations 100. Compare the answers by plotting a graph of the function and see where it intersects at the x-axis.
Create a 4 x 4 square matrix “A” with each column being (1,2,1,2). Create a 4 x 4 matrix “B” with 1’s in super-diagonal and sub-diagonal; 0’s elsewhere. Find the transpose of A and the products A X B and At X B.
Show that B is non-singular. Verify the determinant of 2B = 2^4 * determinant of B. Show that the determinant of inverse of 2B is 1/(determinant of 2B).
Create a positive definite matrix 3×3 M with columns (2, -1, 0), (-1,2, -1) and (0, -1, 2) . Find the LU decomposition of M. Verify M = L X U. Find the Cholesky matrix C of M. Verify M = Ct X C where Ct is the transpose of C.
Create a vector b = ( 4, 2, -3). Solve the equation Mx = b using the gradient descent.
Use the adaptive integration integral from 0 to pi/4 of e^3x sin 2x. Use the function
adaptint. Vary the recursive depth.
Find the integral from 1 to 2 of
f using the rectangle method. Use the function
midpt. Vary the number of rectangles.