我试图通过梯形方法整合多项式函数(我可以在以后更改为更精确的方法)。我的代码并不完美,我想知道为什么它不起作用。我遇到的一个问题是while循环没有结束。到目前为止,我的代码如下。
def Integrate_Trapezoidal(x_LoBound,x_HiBound,N):
"""
INPUT :
x_LoBound -- lower bound of integral
x_HiBound -- upper bound of integral
N -- number of slices (N --> inf ==> integral)
OUTPUT :
-- approximate value of integral
"""
## CREATE ALPHABET
alphabet = [chr(i) for i in range(ord('a'),ord('z')+1)]
## alphabet = ['a','b','c',...,'z'] ##
## WOULD LOVE TO TRY FLOATING INPUTS VIA ARRAY COMPREHENSION
a = float(input("What is the coefficient of the lowest order term: "))
CoeffList = []
CoeffNumList = []
LengthCoeffList = [] ## [1,2,3,...,max] where max = coefficient of highest-order term
for letter in alphabet:
AddOne = int(1)
AddOne += int(1)
for i in range(int(1),int(AddOne)):
letter = alphabet[int(i)]
while letter in alphabet:
CoeffList.append(letter)
LengthCoeffList.append(len(CoeffList))
# alphabet[i]
# i = i + 1
letter = float(input("What is the coefficient of the next-order term: ")) ## GO FROM a = ___ TO b = ___ TO c = ___ ...
CoeffNumList.append(letter)
if float(input("What is the coefficient of the next-order term: ")) == '0':
print("Type 'Y for YES and 'N' for NO")
YESorNO = str(input("Is that the last term of the polynomial: "))
endterm = YESorNO[-1] ## look at last character of string
if endterm == 'N' or endterm == 'n' or endterm == 'no' or endterm == 'NO' or endterm == 'No':
pass
elif endterm == 'Y' or endterm == 'y' or endterm == 'YES' or endterm == 'yes' or endterm == 'Yes':
break
def f(x):
"""
INPUT :
x -- variable of function
EX: x = x_LoBound OR x = x_HiBound
OUTPUT :
function -- f(x) = a x^0 + b x^1 + ...
EX: f(x_LoBound) OR f(x_HiBound)
"""
for expval in LengthCoeffList and CoeffNum in CoeffNumList:
# function = 0
function += CoeffNum * x**expval
return function
letter = alphabet[int(i+1)] ## GO FROM a TO b TO c ...
## TRAPEZOIDAL RULE
# def f(x):
# return x**4 - 2*x + 1
ht = (x_HiBound - x_LoBound) / N
ss = 0.5 * f(x_LoBound) + 0.5 * f(x_HiBound)
for num in range(1,N):
ss += f(x_LoBound + num*ht)
return ht*ss
checkanswer = Integrate_Trapezoidal(0,2,10)
print(checkanswer)
答案 0 :(得分:0)
我已经去查看你的代码并找到了我觉得有用的东西,检查了我下载的几个大学讲义。正如你在评论中所说的,有很多额外的列表是不必要的,所以我在那里减少了很多代码。
特别是,如果假设每个系数从最低到最高顺序依次添加,并且为任何不存在的系数添加0,那么您只需要知道列表中元素的编号x的力量。
我还移动了f()
的定义来创建帮助函数solve_point()
,它的工作方式和我想的一样。特别是,内置sum
和enumerate
,enumerate
遍历coeff_list
并返回计数以提供权力(0向上)。
get_coefficients()
来自您原来的Integrate_Trapezoidal()
,但更专注于一件事 - 这就是为什么它会在最后返回CoeffList
进行最终处理。
def solve_point(x, coeff_list):
return sum(coeff * x**e for e, coeff in enumerate(coeff_list))
def get_coefficients():
CoeffList = []
while True:
# GO FROM a = ___ TO b = ___ TO c = ___ ...
coeff = float(input("What is the coefficient of the next-order term: "))
CoeffList.append(coeff)
if coeff == 0:
YESorNO = raw_input("Is that the last term of the polynomial: [Y/N] ")
if YESorNO.upper() == 'Y':
return CoeffList[:-1]
lo, hi, n = 0, 2, 6
coeff_list = get_coefficients()
ht = (hi - lo) / float(n)
ss = 0.5 * solve_point(lo, coeff_list) + 0.5 * solve_point(hi, coeff_list)
for num in range(1,n):
ss += solve_point(lo + num*ht, coeff_list)
checkanswer = ht*ss
print(checkanswer)
我认为是对的 - 我做了几次检查。希望它可能有助于你的重写!如果您有任何不起作用的示例,那么最好知道,或者您可以看到任何错误......