## Tuesday, August 2, 2022

### Python - Lambda Week: Plotting Functions

Python - Lambda Week: Plotting Functions

Welcome to Python Week!  This we we're going to cover calculus and the keyword lambda.

Note:  All Python scripts presented this week were created using a TI-NSpire CX II CAS.   As of June 2022, the lambda keyword is available on all calculators (in the United States) that have Python.   If you are not sure, please check your calculator manual.

Plotting Functions

We can use the line:

f=eval("lambda x:"+input("f(x) = "))

for multiple applications.   Remember, lambda functions are not defined so that they can be used outside of the Python script it belongs to but it lambda functions are super useful!

This code is specific to the Texas Instruments calculators (TI-Nspire CX II (CAS), TI-84 Plus CE Python, TI-83 CE Premium Python Edition, but NOT the TI-82 Advanced Python).    For other calculators, HP Prime, Casio fx-CG 50, Casio fx-9750GIII/9860GIII, Numworks, or computer Python, apply similar language.

plotlam.py:  Plotting with Lambda

from math import *

import ti_plotlib as plt

# this is for the TI calcs

# other calculators will use their own plot syntax

print("The math module is imported.")

# input defaults to string

# use the plus sign to combine strings

f=eval("lambda x:"+input("f(x) = "))

# set up parameters

x0=eval(input("x min = "))

x1=eval(input("x max = "))

# we want n to be an integer

n=int(input("number of points = "))

# calculate step size

h=(x1-x0)/n

# calculate plot lists

x=[]

y=[]

i=x0

while i<=x1:

x.append(i)

y.append(f(i))

i+=h

# choose color (not for Casio fx-9750/9850GIII)

# colors are defined using tuples

colors=((0,0,0),(255,0,0),(0,128,0),(0,0,255))

print("0: black \n1: red \n2: green \n3: blue")

c=int(input("Enter a color code: "))

# plot f(x)

# auto setup to x and y lists

plt.auto_window(x,y)

# plot axes

plt.color(128,128,128)

plt.axes("axes")

# plot the function

plt.color(colors[c])

plt.plot(x,y,".")

plt.show_plot()

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.

### HP 20S: Gamma Function Approximation (Stirling's Formula)

HP 20S:  Gamma Function Approximation (Stirling's Formula) Introduction The gamma function uses the approximation sequence: Let t = x + ...