วันจันทร์ที่ 26 กรกฎาคม พ.ศ. 2553

VBA ,Excel format : To Calculte Pi (Machin’s series)

This article aims to present The command to check conditions. Under the command to reproduce While I, for example, to calculate the PI using a case study For a description of this command. Finally, provides a set text format in VBA in order to display the calculation.

From previous posts. We have presented methods to calculate the PI using the function PI () in excel or using trigonometric relationships ATAN function through the end of the article. I leave recipes for your Machin the PI consider the formula for the PI in the form of infinite series. As shown in Equation 1.

PI Value

Figure 1 Machin Formular

From the equation, we see that the PI can be determined by the sum of each value of k until the resolution of the PI to the totals for repeated this Easily through computer programming a few lines. Let's see the code written in VBA and description below.

subrutene series

Sub series()
Dim p As Double
Dim q As Double
Dim tol As Double
Dim bfinish As Boolean
p = 0
q = 0
bfinish = True
tol = 0.000000000000001
Dim k As Integer
Dim j As Integer
Dim i As Integer
k = 0
j = 0
i = 0

While bfinish
k = (-1) ^ i
j = 2 * i + 1
p = p + (16 * k) / (j * (5 ^ j))
q = q + (4 * k) / (j * (239 ^ j))
'check condition 15 digit

If Abs(16*k)/(j*(5^j)) <tol And Abs(4*k)/(j * (239 ^ j))<tol Then
bfinish = False
End If
i = i + 1 ' increase i
Wend
Range("A1").Select

‘define format cell (text format)
ActiveCell.NumberFormat = "0.000000000000000"

ActiveCell.Value = p - q
End Sub

From the vba code has shown that Calculated the series for the PI command only two major commands that are used in a while ... wend reproduce and use the IF .. THEN check the terms of the resolution, while the PI of. Details of cell to use the command NumberFormat which enabled simple observations in code of the calculated PI I picked up the calculation in the variable temp. To facilitate the reading code and prevent errors in calculations.

แท็กของ Technorati: {กลุ่มแท็ก},

ไม่มีความคิดเห็น:

แสดงความคิดเห็น

Yahoo bot last visit powered by  Ybotvisit.com