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.