-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApproxFunction.py
62 lines (51 loc) · 1.52 KB
/
ApproxFunction.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
58
59
60
61
62
import math
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
def approx_sin(x, n):
result = 0
for i in range(n):
term = ((-1) ** i) * (x ** (2*i + 1)) / factorial(2*i + 1)
result += term
return result
def approx_cos(x, n):
result = 0
for i in range(n):
term = ((-1) ** i) * (x ** (2*i)) / factorial(2*i)
result += term
return result
def approx_sinh(x, n):
result = 0
for i in range(n):
term = (x ** (2 * i + 1)) / factorial(2 * i + 1)
result += term
return result
def approx_cosh(x, n):
result = 0
for i in range(n):
term = (x ** (2 * i)) / factorial(2 * i)
result += term
return result
def exercise4():
x = input("Enter the value of x (in radians): ")
n = input("Enter the number of terms (positive integer): ")
try:
x = float(x)
n = int(n)
if n <= 0:
raise ValueError("The number of terms must be a positive integer")
except ValueError as e:
print(f"Invalid input: {e}")
return
sin_result = approx_sin(x, n)
cos_result = approx_cos(x, n)
sinh_result = approx_sinh(x, n)
cosh_result = approx_cosh(x, n)
print(f"approx_sin(x={x}, n={n}) = {sin_result}")
print(f"approx_cos(x={x}, n={n}) = {cos_result}")
print(f"approx_sinh(x={x}, n={n}) = {sinh_result}")
print(f"approx_cosh(x={x}, n={n}) = {cosh_result}")
if __name__ == "__main__":
exercise4()