TM2 – Scientific Computing Lab

Nama : Ryo Hadinata
NIM    : 1601250023
04PAW

IT-Math
Bina Nusantara University

from graphics import *
import math

def function(n):
 result = ((-1.0)**(n+1)) * (4.0/(2*n-1))
 return result

def main():
 result = 0

 win = GraphWin("Pi Approximation", 400, 400)
 Text(Point(100,75), "Number of Terms:").draw(win)
 Text(Point(75,200), "The Result Data").draw(win)
 Text(Point(90,220), "================").draw(win)
 Text(Point(100,250), "The Value of math.pi : ").draw(win)
 Text(Point(95,275), "Approximation : ").draw(win)
 Text(Point(105,300), "Math.pi - Approximation: ").draw(win)

 rect = Rectangle(Point(120, 175), Point(280,125))
 rect.setFill("black")
 input = Entry(Point(300,75), 10)
 input.setText("1")
 input.setFill("white")
 button = Text(Point(200,150),"Approximate It!")
 button.setStyle("bold")
 button.setFill("white")

 math_pi = Text(Point(275,250),"")
 approximation = Text(Point(275,275),"")
 difference = Text(Point(275,300),"")
 math_pi.setStyle("bold")
 approximation.setStyle("bold")
 difference.setStyle("bold")

 rect.draw(win)
 input.draw(win)
 button.draw(win)
 approximation.draw(win)
 math_pi.draw(win)
 difference.draw(win)

 win.getMouse()

 n = eval(input.getText())
 for x in range(1, n+1):
     result = result + function(x)

 math_pi.setText(str(math.pi))
 approximation.setText(str(result))
 difference.setText(str(math.pi - result))

 win.getMouse()
 win.close()

main()

Click here to download code (*.py)

This entry was posted in Scientific Computing Lab Assignments and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *