Showing posts with label TI nSpire CX. Show all posts
Showing posts with label TI nSpire CX. Show all posts

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.

Sunday, January 20, 2013

Converting Polar Equations to Parametric Equations

Good Sunday!

This blog will show how to transform polar equations, in the form of r(θ) to a pair of parametric equations, x(t) and y(t).


Tools We Need

x = r * cos θ
y = r * sin θ

Let θ = t.


For each example, we will change each polar equation and display a graph for each form. The polar form, colored blue, is on top; the parametric form, in red, is on the bottom. (A TI nSpire-CX is used for the pictures)

Example (I):

r = 2*θ

Then θ = r/2 and let θ = t.

x = r * cos t
x = 2 * t * cos(r/2)
x = 2 * t * cos((2*t)/2)
x = 2 * t * cos t

Similarly,

y = r * sin t
y = 2 * t * sin(r/2)
y = 2 * t * sin t

To summarize:
r = 2*θ

is equivalent to

x = 2 * t * cos t
y = 2 * t * sin t

Example (II):

r = e^(2 * θ)

Then θ = 1/2 * ln r and let θ = t.

And...

x = r * cos t
x = e^(2*t) * cos(1/2 * ln r)
x = e^(2*t) * cos(1/2 * ln(e^(2*t)))
x = e^(2*t) * cos(1/2 * 2 * t)
x = e^(2*t) * cos t

y = r * sin t
y = e^(2*t) * sin(1/2 * ln r)
y = e^(2*t) * sin(1/2 * ln(e^(2*t)))
y = e^(2*t) * sin t



To summarize:
r = e^(2*θ)

is equivalent to:

x = e^(2*t) * cos t
y = e^(2*t) * sin t

Example (III):

r = 2 cos θ

Then:

θ = arccos(r/2) = cos⁻¹(r/2)

Then, with θ=t...

x = r * cos t
x = 2 * cos t * cos(cos⁻¹(r/2))
x = 2 * cos t * cos(cos⁻¹(2*cos(t)/2))
x = 2 * cos t * cos t
x = 2 * cos² t

And... (some trigonometric identities are required)

y = r * sin t
y = 2 * cos t * sin(cos⁻¹(r/2))

Note: sin(cos⁻¹ x) = √(1 - x²)

y = 2 * cos t * √((1 - (r/2)²)
y = 2 * cos t * √(1 - r²/4)
y = 2 * cos t * √(1 - (4 cos² t)/4)
y = 2 * cos t * √(1 - cos² t)

Note: sin² x + cos² x = 1

y = 2 * cos t * sin t

Note: sin(2*x) = 2 * cos x * sin x

y = sin (2*t)

To Summarize:
r = 2 cos θ

is equivalent to:

x = 2 * cos² t
y = sin (2*t)

Until next time, take care!

Eddie


This blog is property of Edward Shore. 2013

Tuesday, August 28, 2012

Repeated Operations - Revisited


Repeated Operations with the TI nSpire

What happens when you repeat an operation or a function on a number? I am sure we have all entered a number on a standard calculator and pressed the square root function a number of times. Eventually, each non-zero number goes towards 1.

The following graphs show are functions that are repeated. The curve in blue represents the function, the "curve" (really a Scatterplot) in red demonstrates what happens to a certain interval when I repeat a function 10 times. I used a TI nSpire CX CAS for this. That Ans function (last answer) comes in handy!

Some graphs "stabilize" after 10 iterations, and the result becomes a constant for each point in the domain. I will point that out when applicable.

Enjoy!

Eddie

This blog is property of Edward Shore. © 2012

The first set uses the domain x ∈ {0, 5}. The Scatterplot has points in this interval in increments of Δx = 0.25

The next set has the domain x ∈ [0, 10] with the Scatterplot in increments Δx = 0.1. The function is in blue, and the Scatterplot in red represents the function repeated ten times.

Spotlight: Akron Brass FireCalc Pocket Computer

Spotlight: Akron Brass FireCalc Pocket Computer Welcome to a special Monday Edition of Eddie’s Math and Calculator blog. Thi...