แสดงบทความที่มีป้ายกำกับ PI แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ PI แสดงบทความทั้งหมด

วันจันทร์ที่ 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: {กลุ่มแท็ก},

วันอังคารที่ 20 กรกฎาคม พ.ศ. 2553

ค่า PI in Engineering

บทความนี้อยากจะกล่าวถึงค่าคงที่ค่าหนึ่งที่ engineer ทุกคนรู้จักและเคยนำไปใช้ในการคำนวณมาบ้างแล้วไม่มากก็น้อย นั่นคือ ค่า PI นั่นเองครับ ค่าคงที่ PI ถูกนิยามว่า เป็นอัตราส่วนระหว่างความยาวเส้นรอบวงของวงกลมต่อความยาวเส้นผ่านศูนย์กลางของวงกลมนั้น จะเห็นได้ว่าค่าคงที่ PI ถูกนิยามขึ้นจากลักษณะรูปร่างของวัตถุ ดังนั้นจึงอาจกล่าวได้ว่า หากเราพบเจอค่าคงที่ PI ในสูตรการคำนวณใดๆ แล้ว ขอให้เราวิเคราะห์ได้เบื้องต้นว่า เรากำลังแก้ปัญหาที่เกี่ยวข้องกับรูปร่างอยู่ เช่น สูตรการหาพื้นที่วงกลมก็คือ PI *r^2 สูตรการคำนวณหาแรงกดวิกฤตในแนวแกนของเสาที่มีการยึดแบบ pinned end ก็จะเท่ากับ Pcr = PI ^2*E*I/l^2 หรือค่ามุมในหน่วยเรเดียนก็ยังเกี่ยวข้องกับ PI เนื่องด้วยการนิยามค่ามุมจะเกี่ยวข้องกับความยาวส่วนโค้งที่รองรับมุมที่กวาดไปรอบจุดศูนย์กลางของวงกลม เช่น มุม 90 องศาก็จะเท่ากับ PI /2 เป็นต้น

เอาหล่ะครับทีนี้มาเข้าเรื่องกันเลยครับ แล้ว PI มีค่าเท่าไหร่ ใน Excel หรือ VBA Excel จะกำหนดค่าอย่างไร ตอนเด็กๆเราคงจำได้และท่องขึ้นใจนะครับว่า PI มีค่าเท่ากับ 22/7 หรือประมาณ 3.14 แต่ทีนี้เราจะพบว่าการใช้ค่าประมาณในการคำนวณซ้ำๆกันเช่น การคูณ การหาร หรือ ยกกำลัง จะทำให้ผลลัพธ์ของเราเกิดความผิดพลาดขึ้นมาได้ แล้ว PI มีค่าเท่าไหร่ หล่ะ ครับ ใน Excel มีวิธีการหาค่า PI สองวิธีครับคือ

1. ใช้ฟังก์ชั่นที่เรียกใช้ค่า PI ได้เลยครับ โดยเราจะเรียกใช้ผ่านฟังก์ชั่น PI() ครับ ซึ่งฟังก์ชันนี้จะคืนค่า PI มาเท่ากับ 3.14159265358979 (ความละเอียด15 หลัก)

2. หาค่าโดยอ้อมผ่านฟังก์ชัน ATAN (ค่าอาร์กแทนเจนต์) ครับ ทุกท่านคงทราบดีนะครับว่า ATAN(1) จะคืนค่ามุมในหน่วยเรเดียนกลับ ซึ่งเราทราบอยู่แล้วว่า ATAN(1) = PI /4 ดังนั้น PI = 4*ATAN(1)

ครับที่กล่าวมาเป็นการเรียกใช้ฟังก์ชันใน Excel หากต้องใช้ใน VBA เราสามารถหาค่า PI ได้ตามวิธีการที่สองเท่านั้นครับ แต่ฟังก์ชันที่คืนค่าอาร์กแทนเจนต์ใน VBA จะไม่เหมือนกับฟังก์ชันใน Excel โดยจะสามารถเขียนได้ดังนี้ PI = 4*Atn(1) เมื่อ Atn คือ ฟังก์ชันที่คืนค่าอาร์กแทนเจนต์

จากที่ผมได้อธิบายมาหวังว่าท่านผู้อ่านจะนำเอาค่า PI ที่มีความละเอียดสูงนี้ไปใช้ในการคำนวณเพื่อลดความผิดพลาดที่เกิดขึ้นครับ ซึ่งจะเห็นว่าการเรียกใช้ค่า PI ก็ไม่ได้ยุ่งยากแต่อย่างใด

สรุปสุดท้ายครับ ผมมีวิธีการคำนวณค่า PI ให้ท่านผู้อ่านได้พิจารณาครับเผื่อว่าจะต้องการความละเอียดมากกว่า 15 หลักครับ

โดยจะเขียนอยู่ในรูปแบบของอนุกรมได้ดังนี้ครับ (นำเสนอโดยคุณ John Machin ในปี คศ 1706)

PI Value

เดี๋ยวบทความต่อไปเรามาลองเขียน VBA code คำนวณอนุกรมนี้เพื่อหาค่า PI กันครับ

Yahoo bot last visit powered by  Ybotvisit.com