我希望找到指数函数的近似和,我的代码如下所示:
import numpy as np
import matplotlib.pyplot as plt
import math
N = input ("Please enter an integer at which term you want to turncate your summation")
x = input ("please enter a number for which you want to run the exponential summation e^{x}")
exp_sum =0.0
for n in range (0, N):
factorial = math.factorial(n)
power = x**n
nth_term = power/factorial
exp_sum = exp_sum + nth_term
print exp_sum
现在我测试了一对 (x, N) = (1,20) 并返回 2.0,我想知道我的代码在这种情况下是否正确,如果是,那么得到 e = 2.71...,我应该将多少项视为 N?如果我的代码有误,请帮我解决这个问题。