Tampilkan postingan dengan label plot. Tampilkan semua postingan
Tampilkan postingan dengan label plot. Tampilkan semua postingan

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. 


TI-84 Plus CE, TI-Basic and Python: Dynamic and Goal Average

TI-84 Plus CE, TI-Basic and Python:  Dynamic and Goal Average 



Dynamic Average


The dynamic average is defined as:


Σx_i / m   from i = 1 to m, m ≤ n


A new list is created:


1st point has the value of the first data value,

2nd point has the value of the first two data values, 

3rd point has the value of the first three data values,

...

nth point is the arithmetic average of the entire list.


The TI-Basic Version plots the averages in a stat plot, while the Python version returns a list.


Remember:  In TI-Basic, the pointer starts at 1, while with Python, the pointer starts at 0.  Also, my Python programs will require you to enter the number of data points in advance. 


TI-84 Plus CE TI-Basic Program:  DYNAVG


"EWS 2022-04-18"

Input "DATA? ", L1   // [ 2nd ] [ 1 ]

seq(X,X,1,dim(L1),1)→L2   // [ 2nd ] [ 2 ]

cumSum(L1)/L2→L3   // [ 2nd ] [ 3 ]

Func: FnOff : PlotsOff

Plot1(Scatter,L2,L3)

ZoomStat


TI-84 Plus CE Python File: dynavg.py


# 2022-04-20 EWS


d=eval(input("# points? "))

x=[ ]

s=[ ]

a=[ ]


for i in range(d):

  w=eval(input("point "+str(i+1)+"? "))

  x.append(w)

  t=sum(x)

  s.append(t)

  v=t/(i+1)

  a.append(v)


print("cumulative sum:")

print(s)

print("dynamic average:")

print(a)


Example:


Input:  Data Points:  {4, 5.6, 3.9, 5.6, 5.7, 5.8, 7.2, 6.4}

Result: Dynamic Averages: {4, 4.8, 4.5, 4.775, 4.96, 5.1, 5.4, 5.525}





Goal Average


You have a goal, for example:  

*  reach a set number of steps

*  raise a set amount of revenue

*  score a set amount of points during a season of basketball


You have a set amount of times (days, months, years, etc.) (n) and you have a certain amount of data points (m).   Assuming you have time left in your program, what is the remaining amount needed and average per period left?  (This assumes that m < n).  


TI-84 Plus CE TI-Basic Program:  GOALAVG


"2022-04-18 EWS"

Input "GOAL? ", T

Input "TIMES? ", n

Input "DATA? ", L1

sum(L1)→S

dim(L1)→M

If S≥T

Then

Disp "GOAL MADE"

Else

T-S→D

D/(N-M)→A

Disp "TO GO =",D

Disp "AVG =",A

End


TI-84 Plus CE Python File: goalavg.py


# 2022-04-20 EWS


t=eval(input("Goal? "))

n=eval(input("# times? "))

m=eval(input("# data points? "))


s=0

x=[ ] 


for i in range(m):

  w=eval(input("point "+str(i+1)+"? "))

  x.append(w)

  s+=w


if s>=t:

  print("Goal made!")

else:

  d=t-s

  a=d/(n-m)

  print("To go: "+str(d))

  print("Average: "+str(a))


Example:


Input:  

Goal:  100,000

# Times:  14  (n)

# Data Points: 6  (m)

Data:  {5466, 8316, 8821, 9340, 6726, 8011}


Results:

To Go:  53539

Average:  6692.375




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. 


Backlink 9999 Traffic Super

Order Now...!!!!