Python Turtle Graphics, how to plot functions over an interval?

Posted by TheDragonAce on Stack Overflow See other posts from Stack Overflow or by TheDragonAce
Published on 2012-11-09T22:46:21Z Indexed on 2012/11/09 23:00 UTC
Read the original article Hit count: 259

Filed under:
|

I need to plot a function over a specified interval. The function is f1, which is shown below in the code, and the interval is [-7, -3]; [-1, 1]; [3, 7] with a step of .01. When I execute the program, nothing is drawn. Any ideas?

import turtle
from math import sqrt

wn = turtle.Screen()
wn.bgcolor("white")
wn.title("Plotting")
mypen = turtle.Turtle()
mypen.shape("classic")
mypen.color("black")
mypen.speed(10)

while True:
try:
    def f1(x):
        return 2 * sqrt((-abs(abs(x)-1)) * abs(3 - abs(x))/((abs(x)-1)*(3-abs(x)))) * \
(1 + abs(abs(x)-3)/(abs(x)-3))*sqrt(1-(x/7)**2)+(5+0.97*(abs(x-0.5)+abs(x+0.5))-\
3*(abs(x-0.75)+abs(x+0.75)))*(1+abs(1-abs(x))/(1-abs(x)))

    mypen.penup()

    step=.01
    startf11=-7
    stopf11=-3
    startf12=-1
    stopf12=1
    startf13=3
    stopf13=7
    def f11 (startf11,stopf11,step):
        rc=[]
        y = f1(startf11)
        while y<=stopf11:
            rc.append(startf11)
            #y+=step
            mypen.setpos(f1(startf11)*25,y*25)
            mypen.dot()
    def f12 (startf12,stopf12,step):
        rc=[]
        y = f1(startf12)
        while y<=stopf12:
            rc.append(startf12)
            #y+=step
            mypen.setpos(f1(startf12)*25, y*25)
            mypen.dot()
    def f13 (startf13,stopf13,step):
        rc=[]
        y = f1(startf13)
        while y<=stopf13:
            rc.append(startf13)
            #y+=step
            mypen.setpos(f1(startf13)*25, y*25)
            mypen.dot()

    f11(startf11,stopf11,step)
    f12(startf12,stopf12,step)
    f13(startf13,stopf13,step)

except ZeroDivisionError:
    continue

© Stack Overflow or respective owner

Related posts about python

Related posts about turtle