Showing posts with label TI-Nspire CX II. Show all posts
Showing posts with label TI-Nspire CX II. Show all posts

Saturday, November 9, 2024

TI-84 Plus CE Python: Drawing Bars

TI-84 Plus CE Python: Drawing Bars



My inspiration for this post was from a class I was taking at brilliant.org. A free plug: brillant.org is a great web service that offers easy to follow and interactive classes in mathematics, physics, and programming. Classes are offered at every level.



Introduction


The following set of scripts draw a set of bars. The blue bar is the base bar, while orange bars are added to the right of the base bar. The user specifies the length of the base (b) and orange bars, known as the increment (c). The length is in pixels. The screen is 320 pixels long.





Each of the scripts uses the TI-specific module TI-draw module. If you have another calculator or platform, another similar drawing module is needed. The scripts were typed on a TI-84 Plus CE Python Edition, but should work on the TI-83 Premium CE Python Edition and TI-Nspire CX II (I haven’t tested either).



BAR1: Static


This script asks the user for the length of the base bar, increment bar, and the number of bars.


Note that show_draw() command end the execution of the script with the drawing on the screen. Press [clear] to exit the screen.


# static bar


from ti_draw import *

print("Positive Integers Only")
b=int(input("base? "))
c=int(input("increment? "))
n=int(input("# of bars? "))

# total
t=b+c*n

# draw
clear()

# base
set_color(0,120,245)
fill_rect(0,80,b,40)

# increment
set_color(255,135,10)
for i in range(n):
  fill_rect(b+i*c,80,c,40)

# text
set_color(0,0,0)
draw_text(0,160,"Total: "+str(t)+" = "+str(b)+" + "+str(n)+" * "+str(c))
draw_text(0,180,"Press [clear] to exit.")

# draw
show_draw()


BAR2: Animate


This script asks the user for the length of the base bar, increment bar, and the number of bars. Only this time the drawing is animated as the number of increment bars is increased from 0 to n.


This script uses another module, time. This is needed for the sleep(s) command, where s is the number of seconds.



# animate bar


from ti_draw import *
from time import *

print("Positive Integers Only")
b=int(input("base? "))
c=int(input("increment? "))
n=int(input("# of bars? "))

# total
t=b+c*n

# range starts at 0
for i in range(n+1):
  clear()
  # base
  set_color(0,120,245)
  fill_rect(0,80,b,40)
  # increment
  set_color(255,135,10)
  for j in range(i):
    fill_rect(b+j*c,80,c,40)
  t=b+c*i
  # text
  set_color(0,0,0)
  draw_text(0,160,"Total: "+str(t)+" = "+str(b)+" +        "+str(n)+" * "+str(c))
  # draw
  sleep(0.5)

# for the screen to stay on the bars at the end
set_color(255,0,0)
draw_text(0,180,"Press [clear] to exit.")
show_draw()


BAR3: Control


Instead of giving a number of increment bars, the user controls the number of bars by pressing the right [ → ] and left [ ← ] keys. Exit by pressing the [ enter ] key.


This script uses the ti_system module. This allows for the wait_key() command, which stops execution until a key is pressed.


Key codes for TI-84 Plus CE Python and TI-83 CE Premium Python Edition:

[ → ]: right key, code 1

[ ← ]: left key, code 2

[ enter ]: enter key, code 5




# bar with get key

from ti_draw import *
from ti_system import *

def drawsub(b,c,n):
  # total
  t=b+c*n
  # draw
  clear()
  # base
  set_color(0,120,245)
  fill_rect(0,80,b,40)
  set_color(255,135,10)
  for i in range(n):
    fill_rect(b+i*c,80,c,40)
  set_color(0,0,0)
  draw_text(0,160,"Total: "+str(t)+" = "+str(b)+" +        "+str(n)+" * "+str(c))
  draw_text(0,180,"<- or ->, [enter] to quit")


print("Positive Integers Only")
b=int(input("base? "))
c=int(input("increment? "))
print("Press <- or ->")


# default value of n
n=0

# max number of incr
m=int((320-b)/c)

# key
k=0

while k!=5:
  k=wait_key()
  # left key
  if k==2 and n>0:
    n-=1
  # right key
  if k==1 and n<m:
    n+=1
  drawsub(b,c,n)

set_color(255,0,0)
draw_text(0,20,"DONE")
show_draw()



Download the scripts here: https://drive.google.com/file/d/1SKCXBm6lYi5cYurm7nqAqI-868lxvqpz/view?usp=sharing



Until next time and in good health,


Eddie


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


Friday, August 5, 2022

Python - Lambda Week: Solving Differential Equations with Runge Kutta 4th Order Method

Python - Lambda Week: Solving Differential Equations with Runge Kutta 4th Order Method


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. 


Solving Differential Equations


This following script solves the differential equation:


y' = dy/dx = f(x,y)

with initial condition y(x0) = y0


Repeat the steps for each step size h:

f1 = h * f(x0, y0)

f2 = h * f(x0 + h/2, y0 + f1/2)

f3 = h * f(x0 + h/2, y0 + f2/2)

f4 = h * f(x0 + h, y0 + f3)

x0 = x0 + h   (update x)

y0 = y0 + (f1 + 2*f2 + 2*f3 + f4)/6   (update y)


The small h is, the more accurate the calculated coordinates are.  


rk4lam.py:  Runge Kutta 4th Order Method


All answers are stored in the nested list t.  


from math import *

print("Runge Kutta 4th Order")

print("Math Module imported")

f=eval("lambda x,y:"+input("dy/dx = "))


# must call for float numbers one at a time

x0=eval(input("x0 = "))

y0=eval(input("y0 = "))

h=eval(input("h = "))


# ask for an integer

n=int(input("number of steps: "))


# set up table

t=[[x0,y0]]


# main loop

for i in range(n):

  f1=h*f(x0,y0)

  f2=h*f(x0+h/2,y0+f1/2)

  f3=h*f(x0+h/2,y0+f2/2)

  f4=h*f(x0+h,y0+f3)

  x0=x0+h

  y0=y0+(f1+2*f2+2*f3+f4)/6

  print([x0,y0])

  t.append([x0,y0])


print("Done.  Recall t for table.")


Examples


Results are rounded to five digits.  


Example 1:

dy/dx = 2*x*y + x,  y(0) = 0, h = 0.1, 5 steps

(Real solution:  y = 1/2 * (e^(x^2) - 1))


Results (which matches the exact results):

x = 0.1, y ≈ 0.00503

x = 0.2, y ≈ 0.02041

x = 0.3, y ≈ 0.04709

x = 0.4, y ≈ 0.08676

x = 0.5, y ≈ 0.14201


Example 2:

dy/dx = ln x + y, y(10) = 1

(Real Solution:  y = [∫(ln t * e^(-t) dt, t = 10 to x) + e^(-10)] * e^x


Exact Results:

x = 11, y ≈ 6.74551

x = 12, y ≈ 22.51732

x = 13, y ≈ 65.53659

x = 14, y ≈ 182.60824

x = 15, y ≈ 500.96552


Runge Kutta with h = 1, 5 steps:

x = 11, y ≈ 6.71066

x = 12, y ≈ 22.33376

x = 13, y ≈ 64.78988

x = 14, y ≈ 179.90761

x = 15, y ≈ 491.80768



Runge Kutta with h = 0.1, 50 steps:

x = 11, y ≈ 6.74551   (recall t[10])

x = 12, y ≈ 22.51728   (t[20])

x = 13, y ≈ 65.53643   (t[30])

x = 14, y ≈ 182.60766  (t[40])

x = 15, y ≈ 500.96358  (t[50])


This ends Python week for now, I hope you find this week helpful and resourceful.


Until next time,


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. 


Thursday, August 4, 2022

Python - Lambda Week: Integration by Simpson's Rule

Python - Lambda Week: Integration by Simpson's Rule



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. 


Simpson's Rule


The Simpson's Rule estimates numeric integrals by:


∫( f(x) dx, x = a to b) ≈

(b - a) /(3 * n) * (f(a) + 4 * f1 + 2 * f2 + 4 * f3 + .... + 2 * f_n-2 + 4 * f_n-1 + f(b))


n must be an even number of partitions.  The more partitions, the higher the accuracy and the higher computation time.


integrallam.py:  Numeric Integer


from math import *


print("The math module is imported.")

print("Integra of f(x), 6 places")

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


# input parameters

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

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

n=int(input("even parts: "))


# checksafe, add 1 if n is odd

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

  n=n+1


# integral calculus

s=f(a)+f(b)

w=1

# 1 to n-1

for i in range(1,n):

  w=f(a+i*(b-a)/n)

  s+=(2*w) if (i/2-int(i/2)==0) else (4*w)

s*=(b-a)/(3*n)

print("Integral: "+str(round(s,6)))


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. 

Wednesday, August 3, 2022

Python - Lambda Week: Derivatives and Newton's Method

Python - Lambda Week: Derivatives and Newton's Method



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. 


Derivative


The Five Stencil Method is used.  Due to the approximate nature, results are rounded to 5 digits.


f'(x) ≈ (-f(x+2*h) + 8*f(x+h) - 8*f(x-h) + f(x-2*h)) / (12 * h)


h is set to 0.0001 to allow for a wide range of functions and to hopefully prevent float point overflows or underflows.  You can modify h or have the user input a value if you so wish.  


derivlam.py:  Derivative Using the Five Stencil Method


# Math Calculations

#================================

from math import *

#================================


print("The math module is imported.")

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


# input x0

x=eval(input("d/dx at x0: "))

h=.0001


# derivative, 5 stencil

d=(-f(x+2*h)+8*f(x+h)-8*f(x-h)+f(x-2*h))/(12*h)

print("round to 5 decimal points")

print("d/dx = "+str(round(d,5)))


Newton's Method


The next script finds the root of f(x) (solve f(x) = 0) with a guess.  


x_n+1 = x_n - f(x_n) / f'(x_n)


The derivative is calculated using the Five Stencil Method.   


I put a limit of 100 iterations because Newton's Method is not always perfect nor this script finds solutions in the complex plane, just the real numbers.  


newtonlam.py


# Math Calculations

#================================

from math import *

#================================

print("The math module is imported.")

print("Solve f(x)=0 to 6 places")

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


# input x0

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

h=.0001


w=1

n=1

while fabs(w)>10**(-7):

  d=(-f(x+2*h)+8*f(x+h)-8*f(x-h)+f(x-2*h))/(12*h)

  w=f(x)/d

  x-=w

  n+=1

  if n>100:

    print("iterations exceeded")

    break


if n<101:

  print("x = "+str(round(x,6)))

  print("iterations used: "+str(n))



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. 


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. 


Monday, August 1, 2022

Python - Lambda Week: Building and Asking for Functions

Python - Lambda Week: Building and Asking for 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. 



Lambda - Introduction


The key word lambda allows us to define a one-line function in Python program for internal use.  Keep in mind, this is different from the define (def-return) structure, where we can define multiline functions and can be used to be imported into other programs or the shell.


I briefly introduced lambda last September:  https://edspi31415.blogspot.com/2021/09/calculator-python-lambda-functions.html



The syntax for lambda is for one argument:


fx=lambda var:define f(var) here


And we can use fx(var) to calculate the lambda function. 




We can use more than one argument, and the syntax looks something like this:


fx=lambda var1, var2, ...:define f(var1, var2, ...)


We use fx(var1,var2,...) to recall and calculate.



Keep in mind, a lambda function can accept many arguments, but can only return one answer.   The script lambdabuild.py shows a short demonstration of the lamdba key word:



lambdabuild.py:   Build a lambda function


from math import *

#================================

# build a lambda function


fx=lambda x:x**2+1

print("x=1, ",str(fx(1)))

print("x=2, ",str(fx(2)))


# lambda can have more than 1 input, but 

# must have only 1 output


gxy=lambda x,y:sqrt(x**2+y**2)

print("x=3, y=4",str(gxy(3,4)))

print("x=6, y=10",str(gxy(6,10)))


Getting User Input


We can ask for a user function by the lines:

fs=input("text here")

fx=eval("lambda var:"+fs)


This can be combined in one line:

fx=eval("lambda var:"+input("prompt"))


For example:

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


The eval function changes a string to an expression to be evaluated.  This is great because we can use eval to change strings to make lambda functions and ask for input of numerical expressions including pi (assuming the math module is imported).


lambda2.py:   Asking for a function


# Math Calculations

#================================

from math import *

#================================

# ask the user to define a function

# input defaults as a string


print("The math module is imported.")

print("Use eval for allow for numeric expressions,")

print("including pi.")

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


# ask for three inputs

# use eval to allow for expressions

x1=eval(input("x1? "))

x2=eval(input("x2? "))

x3=eval(input("x3? "))


# calculate

y1=f(x1)

y2=f(x2)

y3=f(x3)


# print results

print("Here are your results:")

print(x1, y1)

print(x2, y2)

print(x3, y3)




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. 


Friday, June 3, 2022

Quick TI Thoughts

Quick TI Thoughts


Calculator Succession


TI-86 ->  TI-NSpire CX -> TI-NSpire CX II


TI-89 -> TI-NSpire CX CAS -> TI-NSpire CX II CAS


What do you think?   I think the TI-Nspire has more than a TI-84 Plus, at least in complex numbers and Boolean conversions.  


The CX II adds Python.  The non-CAS does neither have a computer algebra system nor an exact answer engine (can return answers in terms of pi or factor square roots).


------------------------------------


Also:  why do the TI-Nspire non-CAS and TI-84 Plus (CE) does not have an exact answer engine but the TI-36X Pro does?  


Future Dream Calculator?   The TI-84 Solar Edition:  basically the TI-36X Pro plus graphing, programming (at least TI-Basic).  The TI-36X Pro already has a battery backup. 


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, November 6, 2021

TI-92 Plus and TI-Nspire CX II: List Filters

TI-92 Plus and TI-Nspire CX II:  List Filters




For this particular post, I also have a TI-Nspire CX II CAS document which can be downloaded here:  https://drive.google.com/file/d/1Rm5vIWdUMg5OEyXi4VecNfuIGdiGRtan/view?usp=sharing


The functions are public and are available in the catalog.  Examples and syntax are included.  


The Filters


filters(list, criteria): filters out a list 

sumfilt(list, criteria): sum of selected elements of a list

avgfilt(list, criteria): arithmetic average of elements of a list

prdlist(list, criteria): product of selected elements of a list

dimfilt(list, criteria): returns a count of list elements that fit a criteria


This is similar to Excel functions filter, sumifs, averagefits, and countifs; there is no productifs in Excel.  


The criteria is a string, using x as a variable.  Examples:

"x<10": select elements less than 10

"x≥20": select elements greater than or equal to 20

"10<x and x<20": select elements between 10 and 20, not including 10 or 20

"10≤x and x≤20": select elements between 10 and 20, including 10 or 20


TI-92 Plus Functions: Filters


Note:  I am using the symbol [| for comment; in the program editor, select F2, option 9.  


TI-92 Plus Function:  filters


filters(list1,cr) 

Func

[| list, criteria string with x

[| EWS 2021-08-13

[| filter function

Local list2,d,i,t,x

{}→list2

dim(list1)→d

For i,1,d

expr(cr)|x=list1[i]→t

If t Then

augment(list2,{list1[i]})→list2

EndIf

EndFor

Return list2

EndFunc


TI-92 Plus Function: sumfilt


sumfilt(list1,cr) 

Func

[| list, criteria string with x

[| EWS 2021-08-14

[| sum-if filter function

Local list2,d,i,t,x

{}→list2

dim(list1)→d

For i,1,d

expr(cr)|x=list1[i]→t

If t Then

augment(list2,{list1[i]})→list2

EndIf

EndFor

Return sum(list2)

EndFunc


TI-92 Plus Function: avgfilt


avgfilt(list1,cr) 

Func

[| list, criteria string with x

[| EWS 2021-08-14

[| average-if filter function

Local list2,d,i,t,x

{}→list2

dim(list1)→d

For i,1,d

expr(cr)|x=list1[i]→t

If t Then

augment(list2,{list1[i]})→list2

EndIf

EndFor

Return sum(list2)/(dim(list2))

EndFunc


TI-92 Plus Function:  prdfilt


prdfilt(list1,cr) 

Func

[| list, criteria string with x

[| EWS 2021-08-14

[| product-if filter function

Local list2,d,i,t,x

{}→list2

dim(list1)→d

For i,1,d

expr(cr)|x=list1[i]→t

If t Then

augment(list2,{list1[i]})→list2

EndIf

EndFor

Return product(list2)

EndFunc


TI-92 Plus Function:  dimfilt


dimflt(list1,cr) 

Func

[| list, criteria string with x

[| EWS 2021-08-13

[| count-if filter function

Local list2,d,i,t,x

{}→list2

dim(list1)→d

For i,1,d

expr(cr)|x=list1[i]→t

If t Then

augment(list2,{list1[i]})→list2

EndIf

EndFor

Return dim(list2)

EndFunc


Examples:


list0 = {2,4,5,6,9,10,11,13,14,15,18}


filters(list0,"x≤10"):  {2,4,5,6,9}

filters(list0,"5≤x and x≤15"):  {5,6,9,10,11,13,14,15}


sumfilt(list0,"x≤10"):  36

sumfilt(list0,"5≤x and x≤15"):  83


avgfilt(list0,"x≤10"):  6

avgfilt(list0,"5≤x and x≤15"):  83/8


prdfilt(list0,"x≤10"):  21600

prdfilt(list0,"5≤x and x≤15"):  2700


dimfilt(list0,"x≤10"):  6

dimfilt(list0,"5≤x and x≤15"):  8


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


Monday, September 20, 2021

Calculator Python: Lambda Functions

Calculator Python: Lambda Functions


Introduction to Lambda Functions


Lambda functions are a quick, one expression, one line, python function.   Lambda functions do not require to be named though they can be named for future use if desired.  


The syntax for lambda functions are:


One argument:


lambda  argument : expression


Two or more arguments:


lambda  arg1, arg2, arg3, ...  : expression


The expression must return one result.  


The quick, versatile of lambda functions are make lambda functions one of the most popular programming tools.


Filter, Map, and Reduce


Filter:  uses a lambda function to filter out elements of a list and array using criteria.   For a list, the list command must be used to turn the result into an actual list. 

 

Syntax using Lambda and List:  


list(filter(lambda  arguments : expression))


Map:  uses a lambda function to apply a function to each element of a list.  Like filter, the list command must be used to turn the result into an actual list.


Syntax using Lambda and Map:


list(map(lambda arguments : expression))


Reduce:  uses a lambda function to use two or more arguments in a recursive function.


Syntax using Lambda:


reduce((lambda arguments : expressions), list)


Note, as of August 31, 2021, that the reduce command is NOT available on any calculator, only on full version of Python 3.  This may change with future updates.  


The following calculators have these commands in their Python programming (as of 8/30/2021):


HP Prime:  lambda, filter, map


Casio fx-CG 50 and fx-9750GIII: lambda, map


Numworks: lambda, filter, map


TI-84 Plus CE Python:  lambda, filter, map


TI-Nspire CX II Python:  lambda, filter, map


Nuwmorks Sample Python File: introlambda.py


from math import *

from random import *

# lambda test


n=randint(10,9999)

tens=lambda x:int(x/10)%10

hunds=lambda x:int(x/100)%10

thous=lambda x:int(x/1000)%10


print(n)

print(tens(n))

print(hunds(n))

print(thous(n))


print("List:")

l1=[1,2,3,4,5,6]

print(l1)


print("Filter demonstration")

print("Greater than 3")

l2=list(filter(lambda x:x>3,l1))

print(l2)

print("Odd numbers")

l3=list(filter(lambda x:x%2!=0,l1))

print(l3)


print("Map demonstration")

print("Triple the numbers")

l4=list(map(lambda x:3*x,l1))

print(l4)

print("exp(x)-1")

l5=list(map(lambda x:exp(x)-1,l1))

print(l5)


Sources


Maina, Susan "Lambda Functions with Practical Examples in Python"  Towards Data Science (membership blog with limited free access per month)  https://towardsdatascience.com/lambda-functions-with-practical-examples-in-python-45934f3653a8  Retrieved August 29, 2021


Simplilearn  "Learn Lambda in Python with Syntax and Examples"  April 28, 2021. https://www.simplilearn.com/tutorials/python-tutorial/lambda-in-python  Retrieved August 29, 2021


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

TI-Nspire CX II and TI-84 Plus CE: COUNTIF, SUMIF, AVERAGEIF

TI-Nspire CX II and TI-84 Plus CE:  COUNTIF, SUMIF, AVERAGEIF


Introduction


Three popular spreadsheet functions are operating on elements of a list contingent of a criteria.   


Let L be a list of numerical data.


COUNTIF(L, criteria):   returns a count of all the elements that fit a criteria


SUMIF(L, criteria):  returns the sum of all the elements that fit a criteria


AVERAGEIF(L, criteria):  returns the arithmetic average of all the elements that fit a criteria


Example:


L = {0, 1, 2, 3, 4, 5, 6}


COUNTIF(L, "≥4") = 3.  

Counts all the elements of L that are greater than or equal to 4.


SUMIF(L, "≥4") = 15

Sums all the element's of L that are greater than or equal to 4. 


AVERAGEIF(L, "≥4") = 5

Returns the arithmetic average of L that are greater than or equal to 4.


Note that:


AVERAGEIF(L, criteria) = SUMIF(L, criteria) ÷ COUNTIF(L, criteria)


TI-Nspire CX II:  The functions countif and sumif


The TI-Nspire has two built in functions countif and sumif.  The averageif can easily be defined in the equation from the last section.


You can download a tns demonstration document here:  

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


TI-84 Plus CE Programs:  LISTIF


The program LISTIF calculates all three functions COUNTIF, SUMIF, and AVERAGEIF.  There are two custom lists that are created as a result of this program:


List A:  The input list.


List B:  The list that meets the criteria.


To get the small "L" character, press [ 2nd ], [ stat ] (LIST), B* for the small L.   The small L must be the first character of your list name.  Once created, custom lists are shown under the LIST - NAMES menu.


Although I did not test this on previous calculators, this program should work on the TI-82, TI-83 family, and all of the TI-84 Plus family.


Program listing:


Download Link:

Eddie

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

TI-NSpire CX II and TI-84 Plus CE: Enhanced Graphing Table

TI-NSpire CX II and TI-84 Plus CE:  Enhanced Graphing Table


Introduction


The program TABLEYX allows the user to enter a function, not only store it for graphing, but also display an analytic table.  Results are stored in lists so they can be used for further analysis.  


Both the TI-NSpire CX II (.tns) and TI-84 Plus CE (.8xp) version can be downloaded here.


TI-NSpire CX Version






Table of contents:

1.1  The Notes Page 

1.2  Calc Page:  run tableyx() here

1.3  Graph Page:  it should update automatically each time tableyx() is run

14.  Table Page:  updates when tableyx() is executed

1.5  Program Listing


Lists:

xlist:  x coordinates

ylist:  y(x)

dislist:  Euclidean distance from (0,0) to (x,y)

arclist:  Arclength of y(x) from 0 to x

derlist:  Derivative at (x,y)

intlist:  Integral of y(x) from 0 to x


Program:


Define tableyx()=

Prgm

:© set approximate mode

:setMode(5,2)

:© main program

:Request "y(x)? ",y(x)

:Request "Δx? ",dx

:Request "Number of steps? ",n

:seq(i,i,0,dx*n,dx)→xlist

:seq(y(i),i,0,dx*n,dx)→ylist

:seq(∫(y(x),x,0,i),i,0,dx*n,dx)→intlist

:seq(nDerivative(y(x),x=i),i,0,dx*n,dx)→derlist

:seq(approx(arcLen(y(x),x,0,i)),i,0,dx*n,dx)→arclist

:√(xlist^(2)+ylist^(2))→dislist

:Disp "Done.  See the next page for results."

:EndPrgm


TI-84 Plus CE Version


When the program ends:


Press [ graph ] to see the graph.


Press [ stats ], select Edit... to see the lists in a list editing format.


Lists:

L1:  x coordinates

L2:  y(x)

L3:  Euclidean distance from (0,0) to (x,y)

L4:  Derivative at (x,y)

L5:  Integral of y(x) from 0 to x


Program:


Float

Radian

Input "Y(X)=",Str1

String>Equ(Str1,Y₁)

Input "CHG X? ",D

Input "NO OF STEPS? ",N

seq(I,I,0,D*N,D)→L₁

seq(Y₁(I),I,0,D*N,D)→L₂

√(L₁²+L₂²)→L₃

seq(nDeriv(Y₁(X),X,I),I,0,D*N,D)→L₄

seq(fnInt(Y₁(X),X,0,I),I,0,D*N,D)→L₅

ClrHome

Disp "RESULTS","L₁: X","L₂: Y","L₃: DIST FROM (0,0)","L₄: D/DX Y(X)","L₅: INTEGRAL FROM X=0","PRESS STAT, EDIT"

Pause 

SetUpEditor L₁,L₂,L₃,L₄,L₅



Eddie


All original content copyright, © 2011-2021.  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 48G Collection

 HP 48G Collection  Contents:  Intensity of Spherical Light Source  Speed of Light in Dry Air  Pressure of Air  Earth's Gravity a...