【发布时间】:2019-09-27 23:07:20
【问题描述】:
当我将数字传递给函数时,我似乎无法获得输出。我需要得到计算值并从精确值中减去它。有什么我做错了吗?
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
【问题讨论】:
-
您能否具体说明您将参数传递给哪个函数无法正常工作?
-
您已经定义了一个名为
f1()的函数,但您从未调用它... -
第一部分代码中定义的梯形函数
-
我称之为@f1。同样的问题
-
这里不止一个问题 - 请努力阅读定义和调用函数的基础知识。另外,我建议安装一个好的 Python 编辑器(我自己也喜欢 PyCharm,而且它是免费的),因为它会自动指出您的代码中的一些错误并帮助您更快地学习。
标签: python loops numpy error-handling