Showing posts with label math module. Show all posts
Showing posts with label math module. Show all posts

Sunday, March 15, 2026

TI-84 Plus CE Python: Traceable Plots in Python

TI-84 Plus CE Python: Traceable Plots in Python


These two scripts are an easy way on how to create plots that are traceable. A traceable plot allows the user to trace along a scatter plot or functional plot by using the right and left arrow keys, just like if you made a plot in a graphing calculators’ graph mode.



The two scripts presented are:



TIRNDPLT: makes a scatter plot of ten random points between 0 and 1. The x axis is the plot marker while the y axis is the random point.

TIFXPLT: makes a plot of a single function y(x). Radians mode as assumed and the plot is made of 50 points. You specify the minimum and maximum values of x. Radians mode is assumed and the math module has imported, allowing for scientific functions. A caveat is to plot a function where it is defined for all of the domain given. For instance, for sqrt(x), no negative values should be included in the domain [xmin, xmax].



The two scripts can be downloaded here:

https://drive.google.com/file/d/1D3oB7Vrt9cd1e-wXrEkhZbr8VJ_7V4qB/view?usp=sharing



However I am going to list the code because there are things to be pointed out.



TIRNDPLT



Code:

# TI-84+: Traceable Plot

# Edward Shore 2026-02-20
# tirndplt.py

# Plot ten random elements

# import modules
from math import *
from random import *
from ti_system import *
# plot module is imported differently
import ti_plotlib as plt


# build a random list
x=[i for i in range(1,11)]
y=[round(random(),4) for i in range(1,11)]

# set up the key
# left (2) and right (1) is the trace, enter to exit (5)
k=0

# set the pointer
p=0

# constant elements
# clear the screen
plt.cls()
# set window to allow room for text
plt.window(0,11,-0.5,1.5)
plt.axes("axes")

# plot gray grid
plt.color(192,192,192)
plt.grid(1,0.1)

# main loop, faster?, redraw only replaceable elements
while k!=5:
  # scatter plot, filled dot default
  plt.color(0,0,0)
  plt.scatter(x,y)
  # text at line 1
  plt.text_at(1,"<-, ->, press enter to exit","left",1)
  # string at line 12
  s="x = {0:.0f}, y = {1:.4f}".format(x[p],y[p])
  plt.text_at(12,s,"left",1)
  # plot pointer with cross, blue
  plt.color(0,0,255)
  plt.plot(x[p],y[p],"x")
  # get key
  k=wait_key()
  # left and right keys
  if k==2:
    # clear
    plt.color(255,255,255)
    plt.plot(x[p],y[p],"x")
    # replace
    plt.color(0,0,0)
    plt.plot(x[p],y[p])
    p=(p-1)%10
  elif k==1:
    plt.color(255,255,255)
    plt.plot(x[p],y[p],"x")
    plt.color(0,0,0)
    plt.plot(x[p],y[p])
    p=(p+1)%10

# show draw at the end
plt.text_at(1,"DONE","left",1)
plt.show_plot()


Notes:


1. Modules used: math, random, ti_system, and ti_plotlib. The calling of math and random modules allow the user to include math and random functions. The ti_system has the command wait_key(), which calls for the user to press a key, and returns a specific code value when a key is pressed. The module ti_plotlib is for the specific graphics commands.

2. The y minimum and maximum values for the plot have a padding of 2 to allow room for the top and bottom lines which will contain information. The top line will give instructions for the keys: ← to trace left, → to trace right, and pressing the [ enter ] exits the program.

3. The script has plots in two sections. Outside the loop are elements that will stay permanent: the window, the axis, and the scatter plot. Inside the loop will be the pointer because it changes.

4. Every time an arrow key is processed, the pointer first must be “erased” off the previous position, then move to the next. The pointer is in blue. The variable p is used for the pointer and will take the value between 0 and 9.

5. In Python, the percent symbol is used as a modulus function. So, the expression (p-1)%10 means (p – 1) mod 10. Similarly, (p+1)%10 means (p + 1) mod 10. This is what keeps p in the range of [0, 9]. p is an integer.

6. The use of the format is required to make reading the coordinates readable.

7. Any script using ti_plotlib must have a plt.show_plot() command, at least at the end. It’s primary purpose is to show the final picture and freeze the screen in graphics mode.


TIFXPLT.py




Code:

# TI-84+: Traceable Plot
# Edward Shore 2026-02-22
# tifxplt.py


# Plot a traceable function

# import modules
from math import *
from random import *
from ti_system import *
# plot module is imported differently
import ti_plotlib as plt

# build f(x)
fx=input("y(x)? ")
xmin=eval(input("xmin? "))
xmax=eval(input("xmax? "))
chgx=(xmax-xmin)/50

xl=[]
yl=[]

x=xmin
while x<=xmax:
  y=eval(fx)
  xl.append(x)
  yl.append(y)
  x+=chgx

ymin=min(yl)-2
ymax=max(yl)+2

# set up the key
# left (2) and right (1) is the trace, enter to exit (5)
k=0

# set the pointer
p=0

# constant elements
# clear the screen
plt.cls()
# set window to allow room for text
plt.window(xmin,xmax,ymin,ymax)
plt.color(16,16,16)
plt.axes("on")

plt.color(0,0,0)
plt.plot(xl,yl,".")

# main loop, faster?, redraw only replaceable elements
while k!=5:
  # scatter plot, filled dot default
  # text at line 1
  plt.text_at(1,"<-, ->, press enter to exit","left",1)
  # string at line 12
  s="x = {0:.1f}, y = {1:.4f}".format(xl[p],yl[p])
  plt.text_at(12,s,"left",1)
  # plot pointer with cross, blue
  plt.color(0,0,255)
  plt.plot(xl[p],yl[p],"x")
  # get key
  k=wait_key()
  # left and right keys
  if k==2:
    # clear
    plt.color(255,255,255)
    plt.plot(xl[p],yl[p],"x")
    # replace
    plt.color(0,0,0)
    plt.plot(xl[p],yl[p])
    p=(p-1)%50
  elif k==1:
    plt.color(255,255,255)
    plt.plot(xl[p],yl[p],"x")
    plt.color(0,0,0)
    plt.plot(xl[p],yl[p])
    p=(p+1)%50

# show draw at the end
plt.text_at(1,"DONE","left",1)
plt.show_plot()



Notes:

1. Modules used: math, random, ti_system, and ti_plotlib. The calling of math and random modules allow the user to include math and random functions. The ti_system has the command wait_key(), which calls for the user to press a key, and returns a specific code value when a key is pressed. The module ti_plotlib is for the specific graphics commands.

2. The y minimum and maximum values for the plot have a padding of 2 to allow room for the top and bottom lines which will contain information. The top line will give instructions for the keys: ← to trace left, → to trace right, and pressing the [ enter ] exits the program.

3. The script has plots in two sections. Outside the loop are elements that will stay permanent: the window, the axis, and the plot of the function. Inside the loop will be the pointer because it changes.

4. Every time an arrow key is processed, the pointer first must be “erased” off the previous position, then move to the next. The pointer is in blue. The variable p is used for the pointer and will take the value between 0 and 49.

5. In Python, the percent symbol is used as a modulus function. So, the expression (p-1)%50 means (p – 1) mod 50. Similarly, (p+1)%50 means (p + 1) mod 50. This is what keeps p in the range of [0, 49]. p is an integer.

6. The use of the format is required to make reading the coordinates readable.

7. Any script using ti_plotlib must have a plt.show_plot() command, at least at the end. It’s primary purpose is to show the final picture and freeze the screen in graphics mode.



I hope you enjoy these programs as I did making them,



Eddie


All original content copyright, © 2011-2026. 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 4, 2026

Casio fx-CG 100 Python: Infinite Series and Fresnel Integrals

Casio fx-CG 100 Python: Infinite Series and Fresnel Integrals



Introduction



Many functions, such as the Fresnel Integrals, the Zeta function, and the error function, can be calculated using infinite polynomials by the sum:



f(x) = Σ( g(x, n) from n = 0 to n = ∞) or

f(x) = Σ( g(x, n) from n = 1 to n = ∞)



where:

f(x): the function to be calculated

g(x, n): the infinite series representative of (x)



g(x, n) often includes the factorial function n!, where n starts at either 0 or 1 to infinity. While I was programming on fx-CG 100, I did not find a factorial function in the math module that is included in the MicroPython. So I included a definition of the factorial function as:



# factorial function

def fact(n):

  f=1

  if n<=1:

    return 1

  else:

    for i in range(2,n+1):

      f*=i

  return f



While this function does not check for negative integers, it gives the default values 0! = 1! = 1.


The main program sets up a sum, ask for initial values, and set up a check variable w = g(x,n), with a high initial value to “fail” the loop test. The loop template is set up as:


w=initial value

n=0 (start value of n)

while abs(w)>=1e-20: (1e-20 is a tolerance value, which can be adjusted)

  w=g(x,n)

  s+=w

  n+=1



An Example: Cosine of x Squared



Radians mode is used and is the default angle mode in Python.



cos(x²) = 1 – x^4 ÷ 2 + x^8 ÷ 24 – x^12 ÷ 720 + x^16 ÷ 40320 - …

= Σ( x^(4*n) * (-1)^n ÷ (2n)!, n = 0 to n = ∞)



In this case, g(x,n) = x^(4*n) * (-1)^n ÷ (2 * n)!

I set accuracy tolerance of 1 * 10^-20 but formatted the answer to 12 decimal places.


Casio fx-CG 100 Micropython: cossq.py



# cosine of x squared by series

# template of infinite series

# math module so pi is allowed



from math import *



# factorial function

def fact(n):

  f=1

  if n<=1:

    return 1

  else:

    for i in range(2,n+1):

      f*=i

    return f



# main program

# setup sum

s=0

# ask for x

print("cos(x**2) by series")

x=eval(input("x radians: "))

# series

# set term artificially high

w=100

# set counter at beginning

n=0

# series loop

while abs(w)>=1e-20:

  w=x**(4*n)*(-1)**(n)/fact(2*n)

  s+=w

  n+=1

# answer

print("{0:.12f}".format(s))



Examples:

Example 1: Input: x = 0.5, Result: 0.968912421711

Example 2: Input: x = 1.7, Result: -0.968517164228



Fresnel Integrals



There are two Fresnel integrals.



Cosine Fresnel Integral:

C(x) = ∫( cos( t^2) dt, t = 0 to t = x)

This integral is represented by the infinite series:

C(x) = Σ( x^(4*n + 1) * (-1)^n ÷ ((2* n)! * (4 * n + 1)), n = 0 to n = ∞)



Sine Fresnel Integral:

S(x) = ∫( sin( t^2) dt, t = 0 to t = x)

This integral is represented by the infinite series:

S(x) = Σ( x^(4*n + 3) * (-1)^n ÷ ((2* n + 1)! * (4 * n + 3)), n = 0 to n = ∞)



Casio fx-CG 100 Micropython: fresnel.py



# Fresnel Integrals

# template of infinite series

# Eddie W. Shore, 11/23/2025



from math import *



# factorial function

def fact(n):

  f=1

  if n<=1:

    return 1

  else:

    for i in range(2,n+1):

      f*=i

    return f



# main program

# c: cosine, s=sine

c=0

s=0

# ask for x

x=eval(input("x radians: "))

# series

# set term artificially high

w=100

# set counter at beginning

n=0

# series loop

while abs(w)>=1e-20:

  a=(-1)**n*x**(4*n+1)/(fact(2*n)*(4*n+1))

  b=(-1)**n*x**(4*n+3)/(fact(2*n+1)*(4*n+3))

  w=max(a,b)

  c+=a

  s+=b

  n+=1

# answer

print("x:{0:.12f}".format(x))

print("Fresnel Cosine:\n{0:.12f}".format(c))

print("Fresnel Sine:\n{0:.12f}".format(s))


Examples:

Example 1: x = 0.4,

C(0.4) ≈ 0.398977212913, S(0.4) ≈ 0.021294355570

Example 2: x = 3.1,

C(3.1) ≈ 0.605132097879, S(3.1) ≈ 0.785491219284

Eddie


All original content copyright, © 2011-2026. 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.

Saturday, February 5, 2022

TI-84 Plus CE Python: Evaluation and Integration

 TI-84 Plus CE Python: Evaluation and Integration


Introduction


The two scripts presented in today's blog will make use of Python's built in commands input and eval, to allow the user to enter equations and expressions as inputs.


Using input itself defaults whatever is entered as a string.   If the string is an equation, it will allow for the eval command to evaluate the formula.


Using the eval-input combination will allow the user to enter numerical expressions as well as numbers.   This allows pi as an input, something I wasn't able to do with the float-input combination.  


To allow for math functions including the use of π, I imported the standard math module.


You can download the app variables for the TI-84 Plus CE Python here:

https://drive.google.com/file/d/1Kzly9LjxQg0WrT0XLpdgL8yArGaggaCW/view?usp=sharing


The scripts are presented below.


evaluate.py




# evaluate.py

# 2021-11-29 EWS

from math import *

ch=0

fx=input("f(x)= ")

while ch==0:

  x=eval(input("x? "))

  y=eval(fx)

  print("f(x)= "+str(y))

  print("--------")

  print("Quit? ")

  ch=float(input("0: no,1: yes  "))


Example:

f(x) = exp(.5*sin(x))

x = 0.2, f(x) = 1.104435854

x = 0.6, f(x) = 1.326204677


integral.py





# integral.py

# 2021-11-29 EWS

# Simpson's Rule

from math import *

fx=input("f(x)= ")

a=eval(input("lower? "))

b=eval(input("upper? "))

n=eval(input("n (even)? "))


x=a

t=eval(fx)

x=b

t+=eval(fx)


h=(b-a)/n

for i in range(1,n):

  x=a+i*h

  if i/2-int(i/2)==0:

    t+=2*eval(fx)

  else:

    t+=4*eval(fx)


t*=h/3

print("integral = "+str(t))


Examples:

f(x) = 1/3*sin(x/pi)

lower = 0

upper = 2*pi

n = 10,  integral = 1.482985499


f(x) = (x+1)**(-1)*(x+3)**(-1)

lower = 1

upper = 5

n = 10,  integral = 0.2027325541


The eval command has opened up more possibilities,


Eddie 


All original content copyright, © 2011-2022.  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. 


First Look: HP 16C Collector's Edition

 First Look: HP 16C Collector's Edition I just got the HP 16C Collector's Edition.   This is the famous HP 16C that specializes in c...