如何解决我用Gregory-Liebniz方程得到的Python错误?

时间:2016-10-26 20:20:29

标签: python math syntax-error

n = 10  
pi =0

for i in n:  
    pi = pi + (4i - (1**i))/(2i+1)

print(pi)

for循环中有一个缩进,无法显示。我在pi = pi ... line处出现语法错误。有什么好办法解决这个问题?我只是学习Python,并不完全确定我是否使用了正确的语法。非常感谢你!

3 个答案:

答案 0 :(得分:2)

尝试使用此代码:

n = 10  
pi = 0

for i in range(n): # replaced 'n' with range(n)  
    pi += (4*i - ((-1)**i))/(2*i+1)  # a = a + b, is same as: a += b
    #              ^ Correction as per Gregory–Leibniz formula    
    # for multiplication, you need to explicitly mention `*` operator
    # replaced '4i' and '2i' with '4*i' and '2*i'

print(pi)

答案 1 :(得分:1)

这是实际公式:

pi/4

公式返回* 4,这就是1**i部分的原因。

此外,1始终会产生(-1)**i,您实际需要// creating the frame JFrame frame; frame = new JFrame("Test"); frame.setVisible(true); frame.setLayout(new BorderLayout()); frame.setSize(600, 400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // creating labels JLabel label1 = new JLabel("TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT"); JLabel label2 = new JLabel("TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2"); JLabel label3 = new JLabel("TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3"); JLabel label4 = new JLabel("TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT"); JLabel label5 = new JLabel("TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2"); JLabel label6 = new JLabel("TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3"); // creating the main and menu panel JPanel menupanel,topPanel,botPanel,mainpanel; mainpanel = new JPanel(); menupanel = new JPanel(new BorderLayout()); topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); botPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); topPanel.setPreferredSize(new Dimension(300, 100)); botPanel.setPreferredSize(new Dimension(300, 100)); topPanel.add(label1); topPanel.add(label2); topPanel.add(label3); botPanel.add(label4); botPanel.add(label5); botPanel.add(label6); topPanel.setBackground(Color.RED); botPanel.setBackground(Color.BLACK); mainpanel.setBackground(Color.BLUE); JScrollPane scrollT,scrollB; scrollT = new JScrollPane(topPanel); scrollT.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scrollB = new JScrollPane(botPanel); scrollB.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); menupanel.add(scrollT); menupanel.add(scrollB); menupanel.add( topPanel, BorderLayout.NORTH ); menupanel.add( botPanel, BorderLayout.SOUTH ); frame.add(mainpanel, BorderLayout.CENTER); frame.add(menupanel, BorderLayout.NORTH); frame.pack();

答案 2 :(得分:1)

row.Cells[10]4i的语法错误外,您还没有正确实现pi的Gregory-Leibniz公式。这是修复后的版本。

2i

<强>输出

n = 50000
pi = 0
for i in range(n):
    pi += 4 * (-1)**i / (2*i + 1)

print(pi)

然而,3.1415726535897814 是一种计算每个术语符号变化的非常浪费的方法。这是一个更好的方法。将乘法移出循环4并使(-1)**i仅提供奇数也更有效。

range

Gregory-Leibniz公式计算n = 50000 pi = 0 s = 1 for i in range(1, 2*n, 2): pi += s / i s = -s pi *= 4 print(pi) 。您可以通过使用基于小于1的数字的反正切关系来加速收敛。例如,

pi = 4 * arctan(1)

arctan(1) = arctan(1/2) + arctan(1/3) 

使用逆余切函数可能更方便,因为arctan(1) = 4 * arctan(1/5) - arctan(1/239)

arctan(1/n) == arccot(n)

<强>输出

def arc_cot(n, lim=1E-15):
    ''' Inverse cotangent of n '''
    x = 1.0 / n
    y = -x * x
    a = x
    i = 1
    while True:
        i += 2
        x *= y
        t = x / i
        a += t
        if abs(t) < lim:
            break
    return a

a = arc_cot(2) + arc_cot(3) 
print(4 * a)

a = 4 * arc_cot(5) - arc_cot(239) 
print(4 * a)

有关推导这些arccot公式的信息,请参阅Machin-like formulaComputing Pi: Lists Of Machin-Type (Inverse Cotangent) Identities For Pi/4