-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample_16.py
More file actions
60 lines (50 loc) · 1.88 KB
/
sample_16.py
File metadata and controls
60 lines (50 loc) · 1.88 KB
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
58
59
60
import math
class TaylorSeries(object):
def __init__(self, num_terms: int):
self.num_terms = num_terms
def compute_series(self, x: float) -> float:
raise NotImplementedError
def __call__(self, x: float) -> float:
# make taylor series callable
return self.compute_series(x)
class Sine(TaylorSeries):
def compute_series(self, x: float) -> float:
x = math.radians(x)
approximation = 0
for index in range(self.num_terms):
coefficient = ((-1) ** index)
numerator = x ** (2 * index + 1)
denominator = math.factorial(2 * index + 1)
approximation += (coefficient * (numerator / denominator))
return approximation
class Exponential(TaylorSeries):
def compute_series(self, x: float) -> float:
approximation = 0
for index in range(self.num_terms):
numerator = x ** index
denominator = math.factorial(index)
approximation += (numerator / denominator)
return approximation
class Cosine(TaylorSeries):
def compute_series(self, x: float) -> float:
x = math.radians(x)
approximation = 0
for index in range(self.num_terms):
coefficient = ((-1) ** index)
numerator = x ** (2 * index)
denominator = math.factorial(2 * index)
approximation += (coefficient * (numerator / denominator))
return approximation
def main():
x = float(input("Enter value of x: "))
sine = Sine(num_terms=10)
print(f"sin{x} = {sine(x)}")
x = float(input("Enter value of x: "))
cosine = Cosine(num_terms=10)
print(f"cosin{x} = {cosine(x)}")
x = float(input("Enter value of x: "))
exponential = Exponential(num_terms=10)
Exponential_value = exponential.compute_series(x)
print(f"exponential{x} = {Exponential_value}")
if __name__ == "__main__":
main()