Tiffany Kassandra

My Answer To You

Archive for March, 2014

(GUI) Approximation of PI Value

No Comments »

March 28th, 2014 Posted 7:00 pm

Here’s the code :

———————————————————————
from graphics import *
import math

def main():
win=GraphWin(“Approximation of PI”, 400,400)
win.setBackground(“yellow”)

# Draw the interface
title = Text(Point(200,75), “APPROXIMATION OF PI”)
title.draw(win)
title.setFill(“blue”)
title.setFace(“courier”)
title.setSize(20)
Text(Point(100,150), “Number of terms”).draw(win)
Text(Point(100,280), ” Approximated PI :”).draw(win)
Text(Point(120,310), ” Math PI – Approximation :”).draw(win)
Text(Point(100,340), ” Percentage of error :”).draw(win)
input = Entry(Point(200,150), 5)
input.setText(“0”)
input.draw(win)
app = Text(Point(250,280), “”)
app.setFill(“blue”)
app.setStyle(“bold”)
app.draw(win)
diff = Text(Point(250,310), “”)
diff.setFill(“blue”)
diff.setStyle(“bold”)
diff.draw(win)
percent = Text(Point(250,340), “”)
percent.setFill(“blue”)
percent.setStyle(“bold”)
percent.draw(win)

button = Text(Point(200,200), “Get The Result”)
button.setStyle(“bold”)
button.setSize(10)
button.setFace(“times roman”)
button.setFill(“red”)
button.draw(win)
ov = Oval(Point(120,180),Point(280,220))
ov.draw(win)

win.getMouse()
button.setText(“Quit”)
x = eval(input.getText())
approx=float(0)
sign = 0

for i in range(1, 2*x, 2):
y=(4./i)*((-1)**sign)
approx=approx+y
sign=sign+1

true_error = math.pi – approx

if(true_error > 0) :
relative_error = true_error / math.pi * 100
else :
relative_error = (-1)*true_error / math.pi * 100
app.setText(“%0.2f” % approx)
diff.setText(“%0.2f” %true_error)
percent.setText(“%0.2f %%” %relative_error)

win.getMouse()
win.close()

main()

———————————————————————————

And here’s the screenshot of the program :

gui PI

Posted in Uncategorized

Approximation of Pi Value

No Comments »

March 14th, 2014 Posted 5:40 pm

Tiffany Kassandra

1601214655

04PAW TI-Math Bina Nusantara University

Question :

_______________________________________________________________

Write a program that approximates the value of π by summing the terms
of this series: 4/1 – 4/3 + 4/5 – 4/7 + 4/9 – 4/11 + … The program should
prompt the user for n, the number of terms to sum, and then output the
sum of the first n terms of this series. Have your program subtract the
approximation from the value of  to see how accurate it is.

Code in Python :
_______________________________________________________________

import math
print(“APPROXIMATION OF PI VALUE\n\n\n”)
x = input(“Input the number of terms to sum : “)
approx=float(0)
sign = 0

for i in range(1, 2*x, 2):
y=(4./i)*((-1)**sign)
approx=approx+y
sign=sign+1

true_error = math.pi – approx

relative_error = true_error / math.pi * 100

print(“\n\n\nThe Result”)
print(“———-“)
print(“The value of Pi (True Value)\t\t\t\t: %f”)%(math.pi)
print(“The sum of the first %d terms of this series\t\t: %f”)%(x, approx)
print(“The difference between the series and Pi (True Error)\t: %f”%(true_error))
print(“True Percent Relative Error (in percent)\t\t: %.2f”%(relative_error))

Hello world!

1 Comment »

March 14th, 2014 Posted 4:14 pm

Welcome to Binusian blog.
This is the first post of any blog.binusian.org member blog. Edit or delete it, then start blogging!
Happy Blogging 🙂

Posted in Uncategorized