我对 Python 完全陌生,我正在尝试打印递归序列的解决方案:
#Input sequence variables
a = float(raw_input("type in a = "))
n0 = int(raw_input("type in n_0 = "))
n1 = int(raw_input("type in n_1 = "))
y0 = float(raw_input("type in y_0 = "))
#Define function y_n (forward iteration)
def yn(n):
if (n==0):
return y0
else:
return (1/n)-a*yn(n-1)
#backward iteration
def yn_back(n):
return (1/a)*((1/n)-yn(n-1))
if(n1>=n0):
for i in range(n0,n1+1):
print(yn(i))
else:
for i in range(n0,n1+1):
print(yn_back(i))
但是,如果我使用,运行此脚本a=5
,并且解决方案非常高(从到),并且即使 n 的值也是负数。该解决方案适合. 对于 other ,的定义中的似乎被忽略了。n0=1
n1=30
y0=log(5/6)=0.182322
0.08839
3.29e+18
n=1
n
(1/n)
yn(n)
有人能帮我吗?
非常感谢!