简体   繁体   English

Python Turtle Graphics,如何在一定间隔内绘制函数?

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

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]; 函数为f1,如下代码所示,间隔为[-7,-3]; [-1, 1]; [-1,1]; [3, 7] with a step of .01. [3,7],步长为0.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

I think because f1 returns an imaginary value. 我认为是因为f1返回一个虚数值。 I literally just started learning python like 15 minutes ago and i'm starting with the turtle. 我实际上只是在15分钟前开始学习python,而我是从乌龟开始的。 So i might not completely understand and be off...but in general i understand code... 所以我可能不完全理解并离开...但是总的来说,我理解代码...

But yeah when you defined it; 是的,当您定义它时, this bit; 这一点 your second term. 你的第二个学期。

sqrt((-abs(abs(x)-1)) sqrt((-abs(abs(x)-1))

you're trying to get the squareroot of a negative number. 您正在尝试获取负数的平方根。 That's not on the cartesian plane. 那不在笛卡尔平面上。 No idea how math interprets that and no idea how turtle interprets it either but that's my first guess... 不知道数学是如何解释的,也不知道乌龟是如何解释的,但这是我的第一个猜测...

You're trying to plot a buggy function using buggy plotting code. 您正在尝试使用越野车绘图代码来绘制越野车功能。 You need to debug them separately, not together. 您需要分别调试它们,而不是一起调试。 Let's start with the function. 让我们从函数开始。 If we look at the starts of the three intervals in question, -7, -1, and 3 and call f1() on them, we get: 如果我们查看有问题的三个时间间隔的起点,即-7,-1和3并对其调用f1() ,我们将得到:

-7 -> 0.0
-1 -> division by zero
 3 -> division by zero

In the first, ploting never starts as f1(startf11) is > stopf11 aka -3 before the loop begins: 首先,绘图永远不会开始,因为在循环开始前f1(startf11) > stopf11 aka -3:

y = f1(startf11)
while y <= stopf11:

In the other two cases, there is no y due to the divide by zero as the 在其他两种情况下,由于除以零,所以没有y

except ZeroDivisionError:
    continue

doesn't fix that problem. 不能解决这个问题。 So no plots. 所以没有情节。 Which probably doesn't matter as the plotting code itself doesn't work. 这可能无关紧要,因为绘图代码本身不起作用。 All three f1* functions (which are identical BTW which makes no sense) do: 所有三个f1*函数(它们是完全相同的BTW,没有意义)可以:

while y <= stop:

but y never changes so it's an infinite loop. 但是y永远不会改变,所以这是一个无限循环。 The commented out y += step would help, but in a different position in the code. 注释掉的y += step会有所帮助,但在代码中的位置不同。 Also, it attempts to plot 100 values for every individual screen pixel! 此外,它尝试为每个屏幕像素绘制100个值! That can be accomodated by changing the coordinate system but I won't go into that here, just reduce this to 10 values for every screen pixel to speed up the result. 可以通过更改坐标系来解决这个问题,但是在这里我不再赘述,只需将其减少到每个屏幕像素的10个值以加快结果。

Let's start over with the plotting code using a simpler test function: 让我们使用一个更简单的测试函数重新开始绘制代码:

def f1(x):
    return x

And get that to plot that successfully. 并将其成功绘制出来。 Here's my rework of the plotting code: 这是我对绘图代码的修改:

from turtle import Turtle, Screen

def f1(x):
    return x  # dummy test plot

wn = Screen()

mypen = Turtle(visible=False)
mypen.speed('fastest')
mypen.penup()

def f11(start, stop, step):

    y = f1(start)

    while y <= stop:

        try:
            mypen.setpos(f1(start) * 25, y * 25)
            mypen.dot()
            y += step

        except ZeroDivisionError:
            continue

step = 0.1  # increased from 0.01 for debugging/speed

startf11 = -7
stopf11 = -3

f11(startf11, stopf11, step)

startf12 = -1
stopf12 = 1

f11(startf12, stopf12, step)

startf13 = 3
stopf13 = 7

f11(startf13, stopf13, step)

wn.exitonclick()

Which gives us the test result: 这给了我们测试结果:

在此处输入图片说明

Which seems reasonable for our test function. 这对于我们的测试功能似乎是合理的。 Now you're in a postion to change the coordinate system for a higher resolution plot and/or debug your f1() function using a working plotter. 现在,您可以更改坐标系以获得更高分辨率的绘图,并且/或者可以使用工作的绘图仪调试f1()函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM