gpt4 book ai didi

python - 我正在将一组N值传递给循环,但无法获取它以打印输出

转载 作者:行者123 更新时间:2023-12-03 09:05:26 24 4
gpt4 key购买 nike

将数字传递给函数时,似乎无法获得输出。我需要获取计算值,然后从精确值中减去它。我有什么不对的地方吗?

def f1(x):
f1 = np.exp(x)
return f1;
def trapezoid(f,a,b,n):
'''Computes the integral of functions using the trapezoid rule
f = function of x
a = upper limit of the function
b = lower limit of the function
N = number of divisions'''
h = (b-a)/N
xi = np.linspace(a,b,N+1)
fi = f(xi)
s = 0.0
for i in range(1,N):
s = s + fi[i]
s = np.array((h/2)*(fi[0] + fi[N]) + h*s)
print(s)
return s
exactValue = np.full((20),math.exp(1)-1)
a = 0.0;b = 1.0 # integration interval [a,b]
computed = np.empty(20)
E=np.zeros(20)
exact=np.zeros(20)
N=20
def convergence_tests(f, a, b, N):
n = np.zeros(N, 1);
E = np.zeros(N, 1);
Exact = math.exp(1)-1
for i in range(N):
n[i] = 2^i
computed[i] = trapezoid(f, a, b, n[i])
E = abs(Exact - computed)
print(E, computed)
return E, computed

最佳答案

您已经定义了几个函数,但是您的主程序从不调用其中任何一个。实际上,您的“父”函数convergence_test无法调用,因为它是在程序底部定义的。

我建议您使用增量编程:写几行;在继续执行代码中的下一个迷你任务之前,先对它们进行测试。在帖子中,您已经编写了大约30行 Activity 代码,却没有意识到实际上没有任何代码可以执行。这可能还存在其他几个错误;您可能会很难解决所有这些问题,以获得预期的输出。

从小处着手,逐步成长。

关于python - 我正在将一组N值传递给循环,但无法获取它以打印输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58142117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com