Skip to content

Commit e6f4792

Browse files
authored
sin() 함수 적분 계산
1 parent 1353769 commit e6f4792

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

integration_sin.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import math
4+
5+
dx = 0.1
6+
xvals = []
7+
yvals = []
8+
9+
# 적분 근사화 함수
10+
def integral_approximation(f, a, b):
11+
return (b-a)*np.mean(f)
12+
13+
# 적분 f(x) = sin(x)
14+
def f(x):
15+
global xvals, yvals
16+
y = math.sin(x)
17+
xvals.append(x)
18+
yvals.append(y)
19+
return y
20+
21+
# 적분의 범위 정의
22+
a = 0
23+
b = 1
24+
25+
# 함수 값 생성
26+
x_range = np.arange(a, b, dx)
27+
fx = np.vectorize(f)(x_range)
28+
29+
# 근사 적분
30+
approx = integral_approximation(fx, a, b)
31+
print(approx)
32+
33+
plt.plot(xvals, yvals)
34+
plt.fill_between(xvals[:], yvals[:], alpha=0.2)
35+
plt.xlabel("x")
36+
plt.ylabel("sin(x)")
37+
plt.grid()
38+
plt.show()

0 commit comments

Comments
 (0)