INTRP 1.50 A library of interpolation routines for the HP48G/GX By Matthew Willis 29 October 1995 ---------------------------------------------------------------- Library: INTRP (#1658) Version: 1.50 Purpose: Simple, powerful linear interpolation routines Easy to use "input form" based interpolation, Lookup table interpolation Compatability: G/GX Author: Matt Willis (mbw8@cornell.edu) ---------------------------------------------------------------- Preamble: Often in my studies I have had to look values up in tables. For example, I might be interested in the density of water as a function of temperature. If I have data specified in 5 degree (C) increments, and I wanted to know the density at 21C then I'd have to interpolate between the density at 20C and the density at 25C. It's a trivial exercise to show that given any two points we can specify a line. It's a repetitive, mistake-prone calculation, though, and amenable to any shortcuts we can find. This library includes three tools that will make interpolating a snap... Overview: INTRP - an interpolation tool you can use from programs or the stack e.g. [ 1 2 ] [ 2 2.5 ] 1.5 INTRP ---> 2.25 IINTRP - an interactive interpolation program that uses the HP48G friendly input forms. Very easy to use. LINTRP - an interpolation routine for a lookup table. You can enter a whole table into your HP and let the HP do all the figuring! e.g. [[ 1 1.5 ] [ 2 2.0 ] [ 4 3.0 ]] 3 LINTRP ---> 2.5 Using water density as an example, we could put all the densities for 0 to 100C and then let the HP find the density as a function of Temperature. More on this later on. Page 1 INSTALLING THE LIBRARY I made this a library so that it would be easy to install. It will run from any port, but for simplicity we'll assume you only have port 0 on your calculator. 1) download the library onto your calculator from your computer (use the pc link software) the file is INTRP15.LIB 2) recall the library object to the stack, you should see 1: Library 1658: INT... 3) Type 0 STO This will store the library in the 0 port 4) Turn your HP off, then on again The library should be installed. You can verify this by looking at the LIBRARY menu (right shift, library) VERSION HISTORY 1.50 Rewritten in Sysrpl. Added error protection and checking. Should be faster, more robust. 1.00 First version, mostly USER RPL code. Page 2 COMMAND CATALOG: INTRP ----------------------------------------------------------------- Command line version of the interpolation routine. Inputs 5: Real (X1) 4: Real (Y1) 3: complex 3: row vector 3: Real (X2) 2: complex 2: row vector 2: Real (Y2) 1: real 1: real 1: Real (X) Returns --> real --> real --> real (or row vector) Note that the real component of the complex number is the X value and the imaginary component is the function. Example: water density as a function of temperature. Temp Density 20C 998.21kg/m3 25C 997.05kg/m3 Say we want the density at 21 degrees C: (20,998.21) (25,997.05) 21 INTRP gives F(21): 997.98 If we wanted to interpolate more than one variable, use the row vector notation. Say we wanted to also find the absolute viscosity at T=21C: Temp Density Viscosity 20C 998.21kg/m3 1.000cp 25C 997.05kg/m3 0.890cp To get the values at 21C: [ 20 998.21 1.000 ] [ 25 997.05 0.890 ] 21 INTRP gives [ 21 997.98 0.978 ] (To extract one of the numbers use ARRY-> or 2 GET, etc.) If this all seems too hard, then look at IINTRP instead. It's much much friendlier. Note: INTRP will extrapolate - but make sure you know what you're doing. Ask yourself: does it make sense to extrapolate here? Page 3 COMMAND CATALOG: IINTRP ----------------------------------------------------------------- Friendly, forms based interpolation. Very easy to use. You don't have to remember the syntax of INTRP this way. Example: *****interpolate***** X1 1.0 F1 3.5 X2 3.0 F2 5.5 ----> 4.5 X 2 To start the IINTRP command: 1) type IINTRP, or open the library menu (right shift, 2) press INTRP, then IINTRP How to use this screen to find the density of water at 21C: X1: put the first temperature, 20, here F1: put the first density, 998.21, here X2: put the second temperature, 25, here F2: put the second density, 997.05, here X : put 21 here (what we actually want) i.e. your screen should look like this: *****interpolate***** X1 20 F1 998.21 X2 25 F2 997.05 X 21 Now press the soft key with the "OK" on it. You'll get your answer. When you want to get really efficient: 1) start IINTRP 2) type 20, hit ENTER type 998.21, hit ENTER, type 25, hit ENTER, type 997.05, hit ENTER, type 21 and hit ENTER twice. Note: IINTRP will extrapolate. Make sure you know that extrapolation is valid for your data, or else your numbers won't mean much. For example, does it make sense to extrapolate based on unemployment figures in 1930 and 1935 to get unemployment figures in 1995? Page 4 COMMAND CATALOG: LINTRP ----------------------------------------------------------------- The lookup interpolater. You can use this is any application where you'd have to go look something up in a table. Think of it as a replacement for tables. You enter the table of data on your HP and the HP finds the proper points to interpolate between. Note that this is pretty different from linear regression. Input: 2: Matrix 1: Real Output: 1: Row vector Example: I have some stream gauging data, depth as a function of X. I want a function that returns the linearly interpolated depth as a function of X. 1) store the stream data in a variable, 'STRM' with X data in the first column of a matrix and depth in the second column: 'STRM' [[ 0 0 ] [ 2 0.5 ] [ 3 0.6 ] [ 5 0.2 ] [ 6 0 ]] 2) create a user function that uses this data table: 'Sdepth' << -> X << STRM X LINTRP >> >> Now you can use the interpolant as a function, like 'Sdepth(x)' Other uses: You could put entire database for water characteristics as a matrix, then could get viscosity, density, etc., from an HP function rather than by looking it up in a table in a book. Notes: Unlike the first two programs, this one won't extrapolate. This program doesn't check to see if your data is sorted. If your data isn't sorted then your answer will not make sense. Page 5