Archive for the ‘TM Scientific Computing Lab’ Category
Approximation of Pi Value
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))
Posted in TM Scientific Computing Lab
