plotfunction.py

Created by ews31415

Created on December 27, 2020

561 Bytes

Template for plotting functions in the form f(x).


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()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.