console.blog()

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:

\[\int_a ^b f(x) \,\mathrm{d}x \approx \frac{b-a}{3n}(f(x_0) + 4f(x_1) + 2f(x_2) + ... + f(x_n))\]

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

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

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

For: the expression \(f\), lower bounds \(a\), upper bounds \(b\), and number of intervals \(m\). First, we need a way to evaluate \(f\) 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 \(f\) at each of those points:

\[f\vert x = \textrm{seqn}\left(a + \frac{(b-a) \cdot (n-1)}{m}, m+1\right)\]

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:

\[\textrm{seqn}\left( \begin{cases} 1, & n = 1 \;\textrm{or}\; n = m+1 \\ 4, & \textrm{mod}(n,2) = 0 \\ 2, & \textrm{Else} \end{cases}, m+1 \right)\]

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:

\[\textrm{Define }\mathbf{simpson}(f, a, b, m) = \frac{b - a}{3 \cdot m} \cdot \textrm{sum}\left( \left( f\vert x = \textrm{seqn}\left(a + \frac{(b-a) \cdot (n-1)}{m}, m+1\right) \right) \cdot \textrm{seqn}\left( \begin{cases} 1, & n = 1 \;\textrm{or}\; n = m+1 \\ 4, & \textrm{mod}(n,2) = 0 \\ 2, & \textrm{Else} \end{cases}, m+1 \right) \right)\]

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 \(x\). It does not take a function. For example, to use it, we would write:

\[\mathbf{simpson}\left( 100x\sqrt{125-x^3}, 0, 5, 12 \right)\]

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

\[\mathbf{f1}(x) = 100x\sqrt{125-x^3}\]

We would first evaluate the function at \(x\) and then use the simpson function:

\[\mathbf{simpson}\left( \mathbf{f1}(x), 0, 5, 12 \right)\]

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 \(f2 : x \mapsto \cos \pi x\) as an example on the interval \(0 \le x \le 1\). 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:

\[\newcommand{\d}{\mathrm{d}} \left\vert \frac{\d^4}{\d x^4} \mathbf{f2}(x)\right\vert \vert x = \mathbf{nfMax}\left( \left\vert\frac{\d^4}{\d x^4}\mathbf{f2}(x)\right\vert, x, 0, 1 \right)\]

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 \(x=0\). 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 \(x=0\)). 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:

\[\newcommand{\d}{\mathrm{d}} \left\vert \frac{\d^4}{\d x^4} \mathbf{f2}(x)\right\vert \vert x = \textrm{mat}\triangleright\textrm{list}\left( \textrm{exp}\triangleright\textrm{list}\left( \mathbf{fMax}\left( \left\vert\frac{\d^4}{\d x^4}\mathbf{f2}(x)\right\vert, x, 0, 1 \right),\{x\} \right) \right)[1]\]

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