2012-05-05 26 views
1

我试图写一个程序,以x 的形式迭代为我的n + 1 = X Ñ + C。我该怎么做呢?的Python:一种迭代函数

例如,当c = 1和x = 1/2

(1/2) + 1 = 5/4,
(5/4) + 1 = 25/16 + 1 =一十六分之四十一
...等

我的代码不能在这里工作:

def main(): 
    import math 
    print("This program iterates a function of the form x^2 + c:") 
    x=complex(input("Enter a seed value:")) 
    c=complex(input("Enter a value for c")) 

for i in range(50): 
    f(0) = x*x + c 
    f(i) = f(i-1)*f(i-1) + c 
    print(f(i)) 
main() 

P ython不允许我使用f(0)f(i)像matlab,有什么替代呢?

+0

你得到哪些错误? –

+0

我得到“无法分配函数调用”。这是我第一次使用Python,所以我不知道在这里使用什么。我需要知道我可以使用哪些工具。 –

+1

我认为您需要更多地了解python基础知识。这里有很多错误。你有没有通过任何python教程工作? – Daenyth

回答

2

Tabbing对python很关键,请尝试下面的代码。这是一个长镜头,去猜测你的意图是什么,但它带给你更接近最终结果

def main(x): 
    import math 
    print("This program iterates a function of the form x^2 + c:") 
    c = complex(input("Enter a value for c")) 

    print "C : %s" % c 

    f = [x * x + c] 

    for i in range(1, 50): 
     f.append(f[i - 1] * f[i - 1] + c) 

    print f 

main(2) 
+3

除f(i)= f(i-1)+ c'外没有任何意义。 – Johnsyweb

+1

通过代码阅读后,我实际上只是想说...你在尝试做什么世界大声笑 – Bryan

+0

我很抱歉布莱恩这是我的错。我已经根据应该发生的事情修改了我的代码。 –

2

如果你不保持所有的部分结果intereseted,你可以重复这样的:

import math 
print("This program iterates a function of the form x^2 + c:") 
x = complex(input("Enter a seed value:")) 
c = complex(input("Enter a value for c")) 

f = x * x + c 
for _ in range(50): 
    f = f * f + c 
    print(f) 
+0

非常简单,gracias Rik –

+0

@RikPoggi:没有。我的迭代,就像你的:)唯一的区别是,我已经从'main()'提取数学函数到'f()',使其更容易阅读,测试和维护。 – Johnsyweb

+1

@Johnsyweb:哦,没错。我给它一个太快的样子。无论如何,我喜欢它,因为它是干的:) –

1

让我们把你的函数f

def f(x, c): 
    return x * x + c 

然后在main(),你可以这样调用:

def main(): 
    n = complex(input("Enter a seed value: ")) 
    c = complex(input("Enter a value for c: ")) 

    print n 
    for term in range(50): 
     n = f(n, c) 
     print n 

每次迭代,n重新分配给f用的n于前值的返回值。

+0

Creative,哇。我现在绝对可以尊重计算机科学专业。 –

0

美好的一天。

# x_(n+1) = x_(n)^2 + c 

def f(n,c): 
    a=0 
    while a < n: 
     a +=1 
     yield a * a + c 

现在你可以使用f作为发电机

if __name__ == '__main__': 
    for element in f(100, 0): 
     print element 
1

在Python中一种特殊的机制被称为发生器功能。它返回一个记录最后状态的对象,并用前一次调用(迭代器)的值来执行函数体。

它只是通过使用yield命令而不是return而在语法上与正常功能不同。通常可以使用在其中迭代器被预期的地方,即,对于环,容器构造函数等这样的发电机的功能见的代码:

def f(x, c, n=5): 
    while n > 0: 
     yield x  # returns x0 as first value 
     x = x * x + c # this value is to be returned next time 
     n -= 1   # decrement the sequence counter 


# Using the generator function in a for loop. 
for value in f(1/2, 1, 14): # I want 14 members of the sequence 
    print(value) 

# Using the generator function to build a list of the values. 
print('----------------------------------') 
lst = list(f(1/2, 1, 10)) # 10 members wanted here 
print(lst) 


# Using the standard module called fractions for the same function. 
print('==================================') 
from fractions import Fraction as frac 

# Using the generator function in a for loop. 
for value in f(frac(1, 2), 1): # default number of loop used here 
    print(value) 

# Using the generator function to build a list of the values. 
print('----------------------------------') 
lst = list(f(frac(1, 2), 1, 10)) # 10 members wanted here 
print(lst) 

# Generating Mandelbrot set values. 
print('==================================') 
# Using the generator function in a for loop. 
for value in f(complex(0), complex(0, 1)): # default number of loop used here 
    print(value) 

Python不计算表达式象征性,作为MATLAB一样。但是,它具有标准模块分数,它具有Fraction类来表示分数。由于它定义了自己的乘法和加法,因此也可以为该类型使用相同的生成器函数。由于Python整数不如浮点数有限,因此您可能会用Fractions得到更大的结果(如果它有意义的话)。但是你可能想要生成一个Mandelbrot集合,对吧?

这表明我的控制台上(换行):

0.5 
1.25 
2.5625 
7.56640625 
58.25050354003906 
3394.1211626681034 
11520059.466871478 
132711770120256.16 
1.7612413928451715e+28 
3.1019712438712e+56 
9.62222559780384e+112 
9.258722545503146e+225 
inf 
inf 
---------------------------------- 
[0.5, 1.25, 2.5625, 7.56640625, 58.25050354003906, 3394.1211626681034, 11520059. 
466871478, 132711770120256.16, 1.7612413928451715e+28, 3.1019712438712e+56] 
================================== 
1/2 
5/4 
41/16 
1937/256 
3817505/65536 
---------------------------------- 
[Fraction(1, 2), Fraction(5, 4), Fraction(41, 16), Fraction(1937, 256), Fraction 
(3817505, 65536), Fraction(14577639392321, 4294967296), Fraction(212507588699293 
047863318657, 18446744073709551616), Fraction(4515947525478824258458249067737177 
3490882293292495105, 340282366920938463463374607431768211456), Fraction(20393782 
05287831607501825305820853979646214602081433287459323242821411496740800167480972 
336912829578600961, 115792089237316195423570985008687907853269984665640564039457 
584007913129639936), Fraction(41590634642030170391815210870941032988232016881852 
69467060076200306147021769623813726880369383179868466442311933392597163786089664 
61843166606956164602440721448188927878363156181727061624343416854647005907620761 
7, 13407807929942597099574024998205846127479365820592393377723561443721764030073 
546976801874298166903427690031858186486050853753882811946569946433649006084096)] 

================================== 
0j 
1j 
(-1+1j) 
-1j 
(-1+1j)