Saturday, January 16, 2021

Numworks: Templates to Plot Functions, Parametric Equations, and Sequences

 Numworks:  Templates to Plot Functions, Parametric Equations, and Sequences


Introduction


The following are sample templates to plot functions in the form of y=f(x), parametric equations (x(t), y(t)), and one-level deep recurrence relations.


I used a while loop instead of a for loop because I tend to always use for loops, and working with integer objects can be quite difficult for non-Python experts like me.  


The equations will be defined inside the script instead of having the user enter the script.  If anyone knows how to use input to enter functions, please let me know.   I am using Numworks software version 14.4.  


As far as entering numbers at the input prompt, I have to enter approximations for π (which is approximately 3.1415926535).  The plot screen is made to fit the y-minimum and y-maximum values within the window.


The list.append(value) or list.append(list) adds the elements to the end of the list and the list is automatically saved.  


The named colors, which is required in matplolib.pyplot, available are:  'black', 'blue', 'brown', 'green', 'grey', 'orange', 'pink', 'purple', 'red', 'white', and 'yellow'.



Plotting Functions -   Numworks Script:  plotfunction.py


from math import *

from matplotlib.pyplot import *

# EWS 2020-12-26


# define function here

def f(x):

  y=1/(x**2+1)

  return y


# main routine

xa=float(input('start? '))

xb=float(input('stop? '))

n=float(input('n? '))

xc=(xb-xa)/n


# build

xp=xa

yp=f(xp)

xlist=[xp]

ylist=[yp]


while xp<xb:

  xp=xp+xc

  yp=f(xp)

  xlist.append(xp)

  ylist.append(yp)

  

# plot routine


# set axes

ya=min(ylist)

yb=max(ylist)

axis((xa,xb,ya,yb))

axis(True)

grid(True)


# select color, type color

ch="blue"


# plot points

plot(xlist,ylist,color=ch)

show()





Plotting Parametric Equations -   Numworks Script:  plotparametric.py


from math import *

from matplotlib.pyplot import *

# EWS 2020-12-26


# define parametric here

def x(t):

  x=t**2-3*t+1

  return x

def y(t):

  y=abs(2*sin(t))

  return y


# main routine

ta=float(input('start? '))

tb=float(input('stop? '))

n=float(input('n? '))

tc=(tb-ta)/n


# build

tp=ta

xp=x(tp)

yp=y(tp)

xlist=[xp]

ylist=[yp]


while tp<tb:

  tp=tp+tc

  xp=x(tp)

  yp=y(tp)

  xlist.append(xp)

  ylist.append(yp)

  

# plot routine


# set axes

xa=min(xlist)

xb=max(xlist)

ya=min(ylist)

yb=max(ylist)

axis((xa,xb,ya,yb))

axis(True)


# select color, type color

ch="red"


# plot points

plot(xlist,ylist,color=ch)

show()




Plotting a Recurrence Relation-   Numworks Script:  plotsequence.py


Use u for u_n-1.  You should also be able to include n without problems.  


from math import *

from matplotlib.pyplot import *

# EWS 2020-12-26


# define parametric here

# u: u(n-1)

def w(u):

  f=sqrt(3*u+1)

  return f


# main routine

ui=float(input('initial? '))

n=float(input('n? '))


# build

xlist=[0]

ylist=[ui]

k=0


while k<n:

  k=k+1

  f=w(k)

  xp=k

  yp=f

  xlist.append(xp)

  ylist.append(yp)

  

# plot routine


# set axes

ya=min(ylist)

yb=max(ylist)

axis((0,n,ya,yb))

axis(True)


# select color, type color

ch="green"


# plot points

plot(xlist,ylist,color=ch)

show()



On tomorrow's blog, January 17, 2021, I am going to present these scripts for the TI-Nspire CX II.   I used the CX CAS software, but this should work on the CX (non-CAS) calculator and software as well.  

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.