Monday, July 29, 2019

TI Nspire CX and Casio Micropython: Recursion Polynomials

TI Nspire CX and Casio Micropython:  Recursion Polynomials

Introduction

This blog entry deals with how to calculate functions defined as a recursion polynomial.  An example:

f_n(x) = 2 * f_n-1(x) + 1    where f_0(x) = x^2

We'll cover the TI Nspire CX (which is programmed in Basic) and the Casio fx-CG50's Micropython Module.

TI Nspire CX

The programs for the TI Nspire are in Basic (or some flavor of it), so it can easily be adapted for many programming and graphing calculators.  A possible template could be:

Input "X: ", x
Input "N: ", n
Local f, g, h, i 
//  f represents the function f_n
// g represents f_n-1
// h represents f_n-2
If n = 0: Then Return f_0(x):  Stop: IfEnd
If n = 1: Then Return f_1(x):  Stop: IfEnd
Else // if n > 1
For i =2 to n do
f := f_(x,g,h,i)   // i is used in placed for n
h:= g   // move values for the next loop
g:= f
Next
Return f  // final result

This is just one example on how to tackle this. 

Example 1: 
f_n = f_n-1 * (x + 1) - f_n-2
f_0 = 1
f_1 = x

Define recur1(n,x)=
Prgm
:Local f,g,h,i
:If n=0 Then
:  Disp 1
:ElseIf n=1 Then
:   Disp x
:ElseIf n>1 Then
:   h:=1
:   g:=x
:   For i,2,n
:     f:=g*(x+1)-h
:     h:=g
:     g:=f
:   EndFor
:   Disp f
:EndIf
:EndPrgm

Example 2:  Hermite Polynomial
H_n = 2 * x * H_n-1 - (2 * n - 2) * H_n-2
H_0 = 1
H_1 = 2 * x

Define numhermite(n,x)=
Prgm
:© numerical hermite polynomial
:Local f,g,h,i
:If n=0 Then
:  Disp 1
:ElseIf n=1 Then
:  Disp 2*x
:ElseIf n>1 Then
:  h:=1
:  g:=2*x
:  For i,2,n
:    f:=2*x*g-(2*i-2)*h
:    h:=g
:    g:=f
:  EndFor
:  Disp f
:EndIf
:EndPrgm

Example 3:  Parabolic Cylinder Function (aka Weber Function)
D_v+2 = x * D_v+1 - v * D_v
D_0 = e^(-x^2/4)
D_1 = x * e^(-x^/4)

Define parabcylin(v,x)=
Prgm
:© parabolic cylinder polynomial
:Local f,g,h,i
:If v=0 Then
:  Disp e^(((−x^(2))/(4)))
:ElseIf v=1 Then
:  Disp x*e^(((−x^(2))/(4)))
:ElseIf v>1 Then
:  h:=e^(((−x^(2))/(4)))
:  g:=x*e^(((−x^(2))/(4)))
:  For i,2,v
:    f:=x*g-(i-1)*h
:    h:=g
:    g:=f
:  EndFor
:  Disp f
:EndIf
:EndPrgm

Casio fx-CG 50 Micropython

Here is an approach using Python:

import math
N = float(input("N = "))
X = float(input("X = "))
if N==0:
  print(f_0)
elif N==1:
  print(f_1)
elif N>1:
  H = f_0
  G = f_1
  for i in range (2,N+1):
     F = f_n
     H = G
     G = F
 print(F)

Example 1: 
f_n = f_n-1 * (x + 1) - f_n-2
f_0 = 1
f_1 = x

recursio.py:
import math
N=float(input("N = "))
X=float(input("X = "))
if N==0:
  print(1)
elif N==1:
  print(X)  
else:
  H=1
  G=X
  for i in range(2,N+1):
    F=G*(X+1)-H
    H=G
    G=F
  print(F)
   
Example 2:  Hermite Polynomial
H_n = 2 * x * H_n-1 - (2 * n - 2) * H_n-2
H_0 = 1
H_1 = 2 * x

hermite.py:
import math
N=float(input("N = "))
X=float(input("X = "))
if N==0:
  print(1)
elif N==1:
  print(2*X)
else:
  H=1
  G=2*X
  for i in range(2,N+1):
    F=2*X*G-(2*i-2)*H
    H=G
    G=F
  print(F) 


Example 3:  Parabolic Cylinder Function (aka Weber Function)
D_v+2 = x * D_v+1 - v * D_v
D_0 = e^(-x^2/4)
D_1 = x * e^(-x^/4)

import math
N=float(input("N = "))
X=float(input("X = "))
if N==0:
  print(math.exp(-X**2/4))
elif N==1:
  print(X*math.exp(-X**2/4))
else:
  H=math.exp(-X**2/4)
  G=X*math.exp(-X**2/4)
  for i in range(2,N+1):
    F=X*G-(i-1)*H
    H=G
    G=F
  print(F) 
 

Numeric Examples to Try:

Example 1:
n = 2, x = 0.5,  result: -0.25
n = 3, x = -1.7, result: 1.567

Example 2:
n = 2, x = 2.2, result:  17.36
n = 4, x = -0.8, result: -12.1664

Example 3:
n = 0, x = 6.7, result: 1.3369921208E-5
n = 2, x = 0.3, result: 5.86807637482E-4

Happy Programming!

Source:

Keith Oldham, Jan Myland, Jerome Spainer.  An Atlas of Functions.  2nd Edition Springer:  New York.  2009  ISBN 978-0-387-48806-6

Eddie

All original content copyright, © 2011-2019.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.