Tugas1 Scientific Computing Lab

NAMA   : Antonius Ivan
NIM      : 1601215140
04PAW TI-Math Bina Nusantara University

CASE : 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 math.pi to see how accurate it is.

Download Code in Python 2.7.5 disini: 2014 03 15 PR SCL blog Antonius Ivan

import math
trueValue = math.pi

print(“Antonius Ivan”)
print(“1601215140”)
print(“04PAW Binus University\n”)
print(“Program Approximates nilai dari pi”)
print(“================================================================================”)
def pi(n):
result = “”
apx = 0.0
for i in range(n):
apx += ((-1)**i)*4./((2*i)+1)
result += str(((-1)**i)*4)+”/”+str((2*i)+1)+” ”
if(i != n – 1):
if(i%2==1):
result += “+”
else:
result += “= ”
result += str(apx)
return apx, result
n = input(“Masukkan jumlah suku pertama approximation pi: “)
approx, res = pi(n)
print “\nThe Result Data:”
print “================================================================================”
print “Nilai dari math.pi  = ” + str(math.pi)
print “Hasil approximation pi dengan”,n,”suku pertama \n=” ,res
print “\nApproximate         =” ,approx
print “True Value          =” ,trueValue
trueError = trueValue – approx
print “True Error          =” ,trueError
print “Relative Error      =” ,(trueError/trueValue)
print “Percent Relative Error =” ,(trueError/trueValue *100),”%”

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.