a "technical" "blog" by Eric Zheng

Simpson's Rule on the TI nspire
Summary: this post is nspiring

On the multivar quiz tomorrow, we need to know Simpson’s rule for numerical integration using a second-order polynomial approximation. The formula is:

We can write a function to do this on the TI-nspire for an arbitrary expression in .

Here’s what our function will look like in the end:

Define simpson(f, a, b, m) = ...

For: the expression , lower bounds , upper bounds , and number of intervals . First, we need a way to evaluate at all those intervals. We’ll need to use the seqn function to generate the list of x-values we want and evaluate the expression at each of those points:

This evaluates to the list:

{f(x0), f(x1), f(x2), ..., f(xn)}

Now, we need to generate the coefficient list {1,4,2,4,2,...4,1} and take the inner product of these two lists. We can express the coefficient list in terms of a piecewise function on the nspire:

We can take the inner product by simply multiplying the lists and using the sum function on the result. So, our final (somewhat unwieldy) function is something like:

If we want to store this function so that it can be accessed anywhere (Scratchpad, other documents, etc.), we can define it as a library function (thanks to the great philosopher Adithya for pointing this out). Make a new document, add a 9: Program Editor, and set it to Type: Function and Library Access: LibPub. Then, simply type the definition above (make sure to add the correct parameters to the function declaration). Then, save it within the MyLib directory and select Menu > 2: Check Synax... > 1: Check... Finally, from within My Documents, select Menu > B: Refresh Libraries. Now, when you go to the function catalog, you can move over to tab 6 (libraries) and select the simpson function from any document.

An important note: the simpson function takes an expression in . It does not take a function. For example, to use it, we would write:

If we wanted to use it on a univariate function f1 defined as:

We would first evaluate the function at and then use the simpson function:

But wait, why stop here? Actually, I first wrote the above simpson function after I figured out how to use the nspire to get the maximum value of the fourth derivative (as you might have guessed, the purpose was to determine the error bounds for Simpson’s rule). This example is illustrative of the great joys of calculator programming and how clever hacks can improve the functionality of the nspire. Let’s use the function as an example on the interval . The obvious method (that doesn’t involve graphing) is is to use nfMax to get the maximum value and then simply evaluate the fourth derivative at that value. This looks something like:

But wait…if we type this in, the TI-nspire will give us the answer 97.4091 (to six significant figures). While this is good enough for Dr. Eng’s assessments, it isn’t exact. The problem is that the TI-nspire is numerically locating the maximum value, so it’s not reported as a precise number. Usually, this is what we want, but sometimes (like with cosine), we know that the maximum occurs at exactly . How can we get the nspire to recognize this?

There is again an obvious method: just split the computation into two parts. First, we find the x-value of the maximum. Then, we manually enter the value to evaluate the fourth derivative there. But, being the clever hackers we are, we’re not satisfied with the manual approach. Is it possible to trick the calculator into automatically being exact?

Let’s go back to what we did the first time. We used the nfMax function to get the numerical x-value at the maximum. The reason we didn’t use the regular fMax function is that on the nspire (unlike, say, the TI-84), this function gives an expression for the x-value (in this case, that expression is ). Can we use that to our advantage? We can–we just need to be clever with how we cast the expression around.

If you go into the function catalog, you’ll find that there are various functions for casting variables to different types. The ones we want are exp►list (which, confusingly, actually makes a matrix instead of a list) and mat►list (so that we can actually get a list). We can use these functions to trick the TI-nspire into thinking that the value 0 is exact. The end result is something like:

If we try this for f2(x) = cos(πx), we get…the exact value π⁴!