-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask8.py
57 lines (39 loc) · 1.4 KB
/
task8.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#Name: Russell Sammut-Bonnici
#ID: 0426299(M)
#Task: 8
import math #for pi
#recursive function for finding the factorial of a number x
def _factorial(x):
#base case, factorial of 0 is 1
if(x == 0):
return 1.0
# calls recursively until base case
return x * _factorial(x-1)
#function for calculating sin(x) through sum of first t terms
def sin(x,t):
sum=0.0 #initialised sum
#if x is out of the range from -2PI to 2PI, x modulo 2PI
if(x< -(2 * math.pi) or x>(2 * math.pi)):
x = x%(2*math.pi)
#for loop for evaluating sin(x) using MacLaurin's Theorem
for n in range(0,t+1):
numerator = ((-1)**n) * (x**((2*n)+1))
denominator = _factorial((2*n)+1)
sum += numerator/denominator
return sum #returns answer
#function for calculating cos(x) through sum of first t terms
def cos(x,t):
sum=0.0 #initialised sum
# if x is out of the range from -2PI to 2PI, x modulo 2PI
if (x < -(2 * math.pi) or x > (2 * math.pi)):
x = x % (2 * math.pi)
# for loop for evaluating cosx using MacLaurin's Theorem
for n in range(0,t+1):
numerator = ((-1)**n)*(x**(2*n))
denominator = _factorial(2*n)
sum += numerator/denominator
return sum #returns answer
a=50.0 #for x in radians
t=90 #for t trials
print("sin(%.4f) with %d trials = %.9f"%(a,t,sin(a,t)))
print("cos(%.4f) with %d trials = %.9f"%(a,t,cos(a,t)))