Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python程序在输入输入时冻结并关闭?_Python_Numpy_Matplotlib_Anaconda_Freeze - Fatal编程技术网

Python程序在输入输入时冻结并关闭?

Python程序在输入输入时冻结并关闭?,python,numpy,matplotlib,anaconda,freeze,Python,Numpy,Matplotlib,Anaconda,Freeze,我目前正在编写一个程序,用辛普森的3/8法则绘制一个多项式,并对两个端点之间的曲线下的区域进行着色,然后将该信息打印在图形上。目前,该程序在两个端点(2和9)之间的一个多项式((x-3)*(x-5)*(x-7)+85)上运行正常。但是,当试图让程序使用input命令接受多项式或端点的输入时,程序会冻结并崩溃,而不会构建图形。即使重新输入当前数字,也会发生这种情况。代码如下: 下面是代码的基础 import numpy as np import matplotlib.pyplot as plt f

我目前正在编写一个程序,用辛普森的3/8法则绘制一个多项式,并对两个端点之间的曲线下的区域进行着色,然后将该信息打印在图形上。目前,该程序在两个端点(2和9)之间的一个多项式((x-3)*(x-5)*(x-7)+85)上运行正常。但是,当试图让程序使用input命令接受多项式或端点的输入时,程序会冻结并崩溃,而不会构建图形。即使重新输入当前数字,也会发生这种情况。代码如下:

下面是代码的基础

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
这里我将多项式定义为func(x)

在这里,我定义了一个函数,使用辛普森规则计算曲线下的面积

def simpson(function, a, b, n):
    """Approximates the definite integral of f from a to b by the
    composite Simpson's rule, using n subintervals (with n even)"""

    if n % 2:
        raise ValueError("n must be even (received n=%d)" % n)

    h = (b - a) / n #The first section of Simpson's 3/8ths rule
    s = function(a) + function(b) #The addition of functions over an interval

    for i in range(1, n, 2):
        s += 4 * function(a + i * h)
    for i in range(2, n-1, 2):
        s += 2 * function(a + i * h)

    return(s * h / 3)
在这里,我定义了要集成的端点

a, b = 2, 9  # integral limits
为了方便起见,这里还有一些定义

x = np.linspace(0, 10) #Generates 100 points evenly spaced between 0 and 10
y = func(x) #Just defines y to be f(x) so its ez later on

fig, ax = plt.subplots()
plt.plot(x, y, 'r', linewidth=2)
plt.ylim(ymin=0)

final_integral = simpson(lambda t:func(t), a, b, 100000)
这里我构造了阴影区域

# Make the shaded region
ix = np.linspace(a, b)
iy = func(ix)
verts = [(a, 0)] + list(zip(ix, iy)) + [(b, 0)]
poly = Polygon(verts, facecolor='0.9', edgecolor='0.5')
ax.add_patch(poly)
这里我在图上打印积分符号

plt.text(0.5 * (a + b), 30, r"$\int_a^b f(x)\mathrm{d}x$",
     horizontalalignment='center', fontsize=20)
ax.text(0.25, 135, r"Using Simpson's 3/8ths rule, the area under the curve is: ", fontsize=20) #r denotes a raw string
ax.text(0.25, 114, final_integral , fontsize=20) #prints the value of the 
integral defined using simpson's 3/8ths prior
plt.figtext(0.9, 0.05, '$x$')
plt.figtext(0.1, 0.9, '$y$')

ax.spines['right'].set_visible(False) #no dashes on axis
ax.spines['top'].set_visible(False) 
ax.xaxis.set_ticks_position('bottom')

ax.set_xticks((a, b))
ax.set_xticklabels(('$a$', '$b$'))
ax.set_yticks([])

plt.show()
在这里,我打印了根据辛普森的3/8法则计算出的曲线下的面积

plt.text(0.5 * (a + b), 30, r"$\int_a^b f(x)\mathrm{d}x$",
     horizontalalignment='center', fontsize=20)
ax.text(0.25, 135, r"Using Simpson's 3/8ths rule, the area under the curve is: ", fontsize=20) #r denotes a raw string
ax.text(0.25, 114, final_integral , fontsize=20) #prints the value of the 
integral defined using simpson's 3/8ths prior
plt.figtext(0.9, 0.05, '$x$')
plt.figtext(0.1, 0.9, '$y$')

ax.spines['right'].set_visible(False) #no dashes on axis
ax.spines['top'].set_visible(False) 
ax.xaxis.set_ticks_position('bottom')

ax.set_xticks((a, b))
ax.set_xticklabels(('$a$', '$b$'))
ax.set_yticks([])

plt.show()
在这里,我完成了图形的构建

plt.text(0.5 * (a + b), 30, r"$\int_a^b f(x)\mathrm{d}x$",
     horizontalalignment='center', fontsize=20)
ax.text(0.25, 135, r"Using Simpson's 3/8ths rule, the area under the curve is: ", fontsize=20) #r denotes a raw string
ax.text(0.25, 114, final_integral , fontsize=20) #prints the value of the 
integral defined using simpson's 3/8ths prior
plt.figtext(0.9, 0.05, '$x$')
plt.figtext(0.1, 0.9, '$y$')

ax.spines['right'].set_visible(False) #no dashes on axis
ax.spines['top'].set_visible(False) 
ax.xaxis.set_ticks_position('bottom')

ax.set_xticks((a, b))
ax.set_xticklabels(('$a$', '$b$'))
ax.set_yticks([])

plt.show()
然而,当我将定义端点的行更改为“a,b=int(输入(“以格式2,9输入端点”)#整数限制”时,程序


任何帮助都将不胜感激。我很难理解这个困境,因此我为没有提供更多信息而道歉。

这是运行时系统中的一个缺陷,因为它不会给您错误消息。撞车很少是可以接受的反应

我怀疑最接近的原因是无效的输入转换:int接受一个字符串参数,其中表示一个整数。当您尝试将其分配给两个变量时,应该会收到一条消息,告诉您没有足够的值来解包。。。但首先,如果试图将字符串(如“2,9”)转换为单个整数,将出现ValueError

请尝试以下方法:

str_in = input("enter your endpoints in the format 2,9")  # integral limits
fields = str_in.split(',')
a, b = [int(i) for i in fields]
您可以添加错误检查或将其折叠到一行,但我希望您现在可以看到所需的处理