Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!udel!haven!umbc3!math15.math.umbc.edu!rouben From: rouben@math15.math.umbc.edu (Rouben Rostamian) Newsgroups: comp.sys.handhelds Subject: SPLINE for HP48 (repost) Message-ID: <4976@umbc3.UMBC.EDU> Date: 8 Feb 91 01:45:17 GMT Sender: newspost@umbc3.UMBC.EDU Reply-To: rouben@math15.math.umbc.edu Organization: Mathematics Department University of Maryland, Baltimore County Lines: 161 I mentioned in a previous message that the program SPLINE that I had posted yesterday was an older version and did not perform as advertised. Here is the latest version. Please discard the old version. --- SPLINE is a program for generating cubic spline interpolants on the HP48sx calculator. Given an array [y_1,y_2,...,y_n] of numbers and an interval [a,b], it returns a piecewise-cubic, twice differentiable function defined on the interval which takes on the values y_i at the "node points". The node points are n equally spaced points on the interval [a,b]. The independent variable defaults to "X" unless it is specified otherwise in level 1. SPLINE does not use, generate, modify, or delete any global variables. It does not disturb the stack, nor does it set or modify any flags, although the calculator should be in the symbolic mode for things to work. Example 1: Enter 2: [ 0 1 4 9 ] # The array [y_1,y_2,...,y_n] 1: [0 3] # The interval [a,b] and press SPLINE to get: 1: '(-1/10*BSPL(1+X)) +1/10*BSPL(-1+X)... + 3/5*BSPL(-2+X) +3/2*BSPL(-3+X) +12/5*BSPL(-4+X)' The graph of this function passes through the coordinate points (0,0), (1,1), (2,4), (3,9). Plot it and compare with the graph of x^2 on the interval [0,3]. Example 2: Enter 3: [ 0 1 4 9 ] 2: [0 3] 1: 'T2' and press SPLINE to get the same function but with the independent variable "T2": 1: '(-1/10*BSPL(1+T2)) + 1/10*BSPL(-1+T2)... +3/5*BSPL(-2+T2) +3/2*BSPL(-3+T2) +12/5*BSPL(-4+T2)' Example 3: Enter 2: [ 0 1 4 9 ] # The array [y_1,y_2,...,y_n] 1: [0 1] # The interval [a,b] and press SPLINE to get: 1: '-(1/10*BSPL((1/3+X)*3)) +1/10*BSPL((-(1/3)+X)*3) ... +3/5*BSPL((-(2/3)+X)*3) +3/2*BSPL((-1+X)*3) ... +12/5*BSPL((-(4/3)+X)*3)' The graph now passes through (0,0), (1/3,1), (2/3,4), (1,9). Compare with the graph of 9x^2. The function BSPL (and its derivative derBSPL) are included in the program below. Although the interpolating function is defined for all values of of its argument, its values outside the specified interval [a,b] have little meaning. [ Note: The result shown above were obtained after applying "6 FIX" and "->Q"; normally the coefficients are presented as decimal expansions.] It is possible to differentiate the interpolating function. Therefore the f'(x) and EXTR routines in the graphic menu work just like they do with the built-in functions. Example 3: Let's look at the derivative of the interpolant: 3: '(-1/10*BSPL(1+X)) + 1/10*BSPL(-1+X)... + 3/5*BSPL(-2+X) + 3/2*BSPL(-3+X) + 12/5*BSPL(-4+X)' 2: 'X' 1: (insert the derivative symbol here) the result is (sorry about this :-) ): 1: '-(1/10*(((SIGN(ABS(1+X))-SIGN(ABS(1+X)-1))*(9*(1+X)^2-12*ABS(1+X)) +(SIGN(ABS(1+X)-1)-SIGN(ABS(1+X)-2))*-(3*(2-ABS(1+X))^2))*SIGN(1+X)/2)) +1/10*(((SIGN(ABS(-1+X))-SIGN(ABS(-1+X)-1))*(9*(-1+X)^2-12*ABS(-1+X)) +(SIGN(ABS(-1+X)-1)-SIGN(ABS(-1+X)-2))*-(3*(2-ABS(-1+X))^2))*SIGN(-1+X)/2) +3/5*(((SIGN(ABS(-2+X))-SIGN(ABS(-2+X)-1))*(9*(-2+X)^2-12*ABS(-2+X)) +(SIGN(ABS(-2+X)-1)-SIGN(ABS(-2+X)-2))*-(3*(2-ABS(-2+X))^2))*SIGN(-2+X)/2) +3/2*(((SIGN(ABS(-3+X))-SIGN(ABS(-3+X)-1))*(9*(-3+X)^2-12* ABS(-3+X)) +(SIGN(ABS(-3+X)-1)-SIGN(ABS(-3+X)-2))*-(3*(2-ABS(-3+X))^2))*SIGN(-3+X)/2) +12/5*(((SIGN(ABS(-4+X))-SIGN(ABS(-4+X)-1))*(9*(-4+X)^2-12*ABS(-4+X)) +(SIGN(ABS(-4+X)-1)-SIGN(ABS(-4+X)-2))*-(3*(2-ABS(-4+X))^2))*SIGN(-4+X)/2)' -- Rouben Rostamian Telephone: (301) 455-2458 Department of Mathematics and Statistics e-mail: University of Maryland Baltimore County bitnet: rostamian@umbc.bitnet Baltimore, MD 21228, U.S.A. internet: rouben@math9.math.umbc.edu ==================== CUT HERE CUT HERE CUT HERE ===================== %%HP: T(3)A(R)F(.); DIR SPLINE @ Checksum: #28772d @ 424.5 \<< DUP IF TYPE 6 \=/ THEN 'X' END 3 ROLLD OBJ\-> DROP ROT DUP SIZE OBJ\-> DROP SWAP OBJ\-> OBJ\-> DROP \->LIST { } { } 0 0 \-> X a b n y x c h h1 \<< b a - n 1 - DUP2 / 'h' STO SWAP / 'h1' STO -1 n FOR k a h k * + NEXT n 2 + \->LIST 'x' STO n CMAT INV { 0 } y + { 0 } + OBJ\-> \->ARRY * 'c' STO 0 1 n 2 + FOR k 'c' k GET 'x' k GET NEG X + h1 * 1 \->LIST 'BSPL' APPLY * + NEXT \>> \>> BSPL @ Checksum: #55554d @ 154.5 \<< \-> x \<< x ABS 'x' STO CASE x 2 \>= THEN 0 END x 1 < THEN '4-6 *x^2+3*x^3' EVAL END 2 x - 3 ^ END \>> \>> derBSPL @ Checksum: #35435d @ 201 \<< \-> x dx \<< '(SIGN(ABS(x))-SIGN(ABS(x)-1))*(9*x^2-12*ABS(x))' EVAL '(SIGN(ABS(x)-1)-SIGN(ABS(x)-2))*(-3*(2-ABS(x))^2)' EVAL + x SIGN * 2 / dx * \>> \>> CMAT @ Checksum: #49706d @ 217.5 \<< 2 + \-> n \<< n IDN 2 * 2 n FOR i i i 1 - 2 \->LIST 1 PUT NEXT DUP TRN + { 1 1 } 6 PUT { 1 2 } -12 PUT { 1 3 } 6 PUT n DUP 2 - 2 \->LIST 6 PUT n DUP 1 - 2 \->LIST -12 PUT n DUP 2 \->LIST 6 PUT \>> \>> END