python and weierstrass function

python and weierstrass function

本文关键字:function weierstrass and python      更新时间:2023-10-16

基于此函数。我正在尝试创建两个空数组(一个用于 x,另一个用于 y),稍后我将用它们在 python 中绘制。但在此之前,这就是我迄今为止所拥有的......

    import math
    x1=-2.0 
    x2=2.0
    arr1 = []
    arr2 = []
    i=0
    n=10
    delta=(x2-x1)/n
    for i in range (0,n+1):
        x=x1+delta*i        
        arr1.append(x)         
    print arr1
     # I have not called the w function yet

上面的代码现在创建了一个包含 10 个数字的列表,以保持简单。然后它将数组的元素发送到下面的函数,并计算具有某些数字(无限循环)的方程。

    #This function will create the array list for y
    import math
    def w(x, limit):# the limit here to compare when the number is really small            
        suma = 0.0
        sumb = 0.0
        m=1
        x=0
        suma=suma+((1/(math.pow(2,m))*(math.sin(math.pow(2,m)*x)))
        sumb=suma+((1/(math.pow(2,m+1))*(math.sin(math.pow(2,m+1)*x))) # I'm having a 
                                                                       #syntax error  
                                                                        #here
       x+=0
       if (abs (suma-sumb)<limit):
            break:
       else m+=1:
       if (m<20):
            break:

我将不胜感激任何关于我的语法错误或任何建议的帮助。我只是希望我足够清楚。提前致谢

语法错误实际上在上一行,其中括号不平衡。您需要在该行的末尾加一个额外的)(顺便说一句,在您指出的那个也给出了错误)。

还有一些其他问题

  • suma设置为零,所以suma = suma + ...suma = ...相同,但我猜你仍然需要在此行之前添加while循环。
  • 在指示的行上,您有 sumb = suma + ,这可能是复制/粘贴错误。
  • x+=0 开始的代码块仅缩进 3 个空格,而不是 4 个空格。在你的实际代码中可能不是这种情况,但如果是这样,Python 也会抱怨这一点。
  • else m+=1:应该是else: m+=1(冒号紧跟在else之后,而不是在行尾。
  • break:应该只是break(没有冒号)。