# Cubic Splines on the HP 50g

by

## Merv NewtonThiel College

Suppose we have empirical data {(x1, y1), (x2, y2), …, (xn, yn)} that was obtained from some experiment. We have strong reason to believe y is related to x by some smooth function, but we do not know what that function is. We would like to find a function that can be used to approximate values of y for given values of x between the given data points. There are many ways to approach this problem, but the three most common are least squares, Lagrange interpolation, and cubic splines. Each of these methods has advantages and limitations compared to the other two.

The biggest advantage of the least squares method is that it can use all of the data, even if some of the x values are repeated with different corresponding values of y. This is not an unusual situation if an experiment is repeated several times. The other two methods require that the x values be distinct. A disadvantage of the least squares method is that you must know the nature of the function you are trying to approximate; that is, is the function a line, an exponential, a quadratic, etc.? Another advantage of the least squares method is that the calculator has four least squares models built into it. See Lesson 35 of HP-50g Calculator Tutorial. However, if your data does not fit one of those models you will need to do your own programming. Depending on the complexity of the function you are trying to approximate, that can be an extremely difficult job,

The main advantage of Lagrange interpolation is that is a based on a fairly straight forward formula that is relatively easy to program. Given n nodes, it produces a polynomial of degree n - 1. It does, however, have several disadvantages. For one thing, it tends to be quite sensitive to the accuracy of the data. Even a small error in a few of the y values (not at all uncommon in empirical data) can cause large errors in an approximation. This problem becomes more pronounced as the degree of the polynomial increases. The rule of thumb is to not use Lagrange polynomials of degree greater than three. A common practice is to write programs for Lagrange polynomials that will use only two nodes to the left and two to the right of the point at which the function is to be approximated. In that way, only four nodes are being used so the degree of the polynomial will be three. But, of course, that means that the rest of the data is being ignored.

Although it still has the restriction that the x values must be distinct, one of the advantages of the cubic spline method is that it uses all of the data. The other big advantage is that it is not as sensitive to minor errors in the data as Lagrange polynomials. Given n nodes, the cubic spline is a sequence of n - 1 cubic polynomials, one between each pair of adjacent nodes. These polynomials are such that they fit together smoothly at the nodes; not only do the values agree at the nodes, but the first and second derivatives also agree at the nodes. Finding the coefficients for the polynomials involves solving a system of n - 2 equations for n unknowns. That means there are two degrees of freedom, so two of the unknowns can be chosen as desired. This leads to two different commonly used splines, the natural or free spline and the clamped spline. In the case of the free spline the two parameters that are chosen is that the second derivatives at x1 and xn are set to zero. In the case of the clamped spline we must know (or be able to estimate) the derivatives of the function we are approximating at x1 and xn. We then use those as the derivatives of the spline at those points. There are, of course, a variety of ways those derivatives can be estimated from the given nodes.

There are two ways to load the spline programs into your calculator. The easy way is to right click HERE, choose the" Save target as" option and download the programs to your computer. Then, using the connectivity tools provided with the calculator, download it from your computer to the calculator. Lesson 9 of HP 50g Calculator Tutorials discusses the use of the connectivity tools.

The other way, the hard way, to get the programs into your calculator is to type them in. All of the code is in APPENDIX. That is a PDF file.

Click INSTRUCTIONS to learn how to use the program. Those instructions are also in a PDF file. The abbreviations and notation used in the instructions are described in Lesson 1 of HP 50g Calculator Tutorials.

 Below are other resources you may find helpful. An Introduction to Programming HP Graphing Calculators More Programming Commands Differential Equations on the HP 50g