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

问题描述 投票:-1回答:1

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

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 
python loops numpy error-handling
1个回答
0
投票

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

我建议您使用增量编程:写几行;在继续执行代码中的下一个迷你任务之前,先对它们进行测试。在发布中,您已经编写了大约30行活动代码,而没有意识到实际上它的none

实际上是在执行。这可能还存在其他几个错误;您可能会很难解决所有这些问题以获取预期的输出。

从小处开始,逐步成长。

© www.soinside.com 2019 - 2024. All rights reserved.