Hint 0.4 for Python

A simple graph takes a bit of work in Python, but you can copy this file and adjust it as needed.
  • This file graphs the function in two different ranges
  • Note that the curves are defined as lines with colors
  • The functional forms of the graphs are defined by the position assigned to the graph "extension" of the function
  • The range of the for loops is defined by indention; there is no end statement

    from visual.graph import *

    #Define constants
    k=10
    a=3;

    #Set graph parameters - must be before gcurve is used,
    # or it creates a second graph
    graph1=gdisplay(ymin=-1,ymax=+5)

    #Define curves
    tancurve=gcurve(color=color.blue)
    linecurve=gcurve(color=color.green)

    #plot
    for x in arange (0,2,0.01):
        tancurve.plot(pos=(x,tan(k*x)))
        linecurve.plot(pos=(x,a*x))

    #Now do another graph with a narrow range.
    #I've changed the functions to plot...
    #One peculiarity of VPython is that xmin and ymin cannot be > 0,
    #...so we define offsets for x
    xoffset=0.40
    graph2=gdisplay(x=0,y=400,ymin=-0.1,ymax=+0.1)

    #Define curves
    fcurve=gcurve(color=color.red)
    zerocurve=gcurve(color=color.cyan)

    #plot
    for x in arange (0.40,0.41,0.001):
        fcurve.plot(pos=(x-xoffset,a*x-tan(k*x)))
        zerocurve.plot(pos=(x-xoffset,0))
     


    Paste these lines into Notepad and save as SampleGraph.py (or any name with a .py extension). Either double-click on the file to run it, or open it in IDLE, the Python GUI, to edit and run.


    Click here to close