Showing posts with label evaluation. Show all posts
Showing posts with label evaluation. Show all posts

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. 


Saturday, March 14, 2020

TI-36X Pro: Using f(x) in Calculus

TI-36X Pro: Using f(x) in Calculus

The TI-36X Pro Calculator has a table function where you can store a function in the form f(x) and use it for build a short table.  But did you know the function can be used in outside calculations like calculating the derivative and the integral?

It's fairly easy to do. 

1.  Press the [ table ] key and select 2: Edit Function
2.  Enter f(x).  Use the variables key [ x ytzabcd ] to enter x. 
3.  Press [ enter ], [ 2nd ] [ mode ] (quit)

To recall f(x):

1.  Press the [ table ] key and select 1: 1(. 
2.  The function call f( is inserted.   You can use this to make calculations on the home screen.

Below are screen shots of an example, with f(x) set as 3x^2 + 3.






Eddie

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

Wednesday, October 25, 2017

Adventures in Python: User-Entry Function

Adventures in Python:  User-Entry Function

The following script allows the user to enter a function, including math functions (math.sqrt, math.sin, etc).  The function is entered as string and when calculation is needed, the eval function is called. 

Script:

# Program 010:  Asking for a function - Using Eval
# To generate a table

# allow for math functions
import math


# ask for the parameters
print('Generate f(x): You can use math. functions')
print('Such as math.sqrt, math.sin, math.exp, etc.')
print('Remember the default angle measure is radians.')
print('Use math.pi for \u03C0')
# f(x) and parameters will be entered as a string
fx = input('Please enter f(x): ')
# use float for end marks
beg = input('Beginning point:  ')
end = input('Ending point:  ')
step = input('Step: ')
# use eval to evaluate the parameters, turning them into floating numbers
beg = eval(beg)
end = eval(end)
step = eval(step)


print('BEGIN')

# since a floating number with begin, end, and step may be used,
# we must use a while loop instead of a for loop
k = beg
while k<=end:
    # function evaluation
    x = k
    y = eval(fx)
    #  print the data to six decimal places
    x = round(x,6)
    y = round(y,6)
    print(x,',',y)
    print('-----')
    # use += for storage arithmetic
    k += step

# end of table
print('END')

Example:

f(x) = √(x^2 + 1) from x = 0 to x = 4, step 0.5

Result:

Generate f(x): You can use math. functions
Such as math.sqrt, math.sin, math.exp, etc.
Remember the default angle measure is radians.
Use math.pi for π
Please enter f(x): math.sqrt(x**2+1)
Beginning point:  0
Ending point:  4
Step: 0.5
BEGIN
0 , 1.0
-----
0.5 , 1.118034
-----
1.0 , 1.414214
-----
1.5 , 1.802776
-----
2.0 , 2.236068
-----
2.5 , 2.692582
-----
3.0 , 3.162278
-----
3.5 , 3.640055
-----
4.0 , 4.123106
-----
END

Pretty straight forward.  I hope you are enjoying this series on Python.  Until next time,

Eddie


This blog is property of Edward Shore, 2017

DM42 and HP 42S: Quadratic Equation, Characteristic Polynomial, and Eigenvalues

DM42 and HP 42S: Quadratic Equation, Characteristic Polynomial, and Eigenvalues The programs are listed for the Swiss Micros DM42 an...