<jupyter_start><jupyter_text>1차 적분First Order Numerical Integration [![Trapezoidal sums | Accumulation and Riemann sums | AP Calculus AB | Khan Academy](https://i.ytimg.com/vi/1p0NHR5w0Lc/hqdefault.jpg)](https://www.youtube.com/watch?v=1p0NHR5w0Lc) 다시 면적이 1인 반원을 생각해 보자.Again, let's think about the half circle with area of 1. $$\begin{align}    \pi r^2 &= 2 \\    r^2 &= \frac{2}{\pi} \\    r &= \sqrt{\frac{2}{\pi}}\end{align}$$<jupyter_code>r = py.sqrt(2.0 / py.pi)


def half_circle(x):
    return py.sqrt(np.abs(r**2 - x**2))<jupyter_output><empty_output><jupyter_text>$$y = \sqrt{r^2 - x^2}$$<jupyter_code>x_array = py.linspace(-r, r)
y_plus = half_circle(x_array)

py.fill_between(x_array, y_plus)

py.axis('equal')
py.grid(True)<jupyter_output><empty_output><jupyter_text>이번에는 사다리꼴 규칙을 이용해서 구해 보기로 하자.This time, let's use the trapezoid rule to find its area.  사다리꼴 규칙Trapezoid Rule 다음과 같은 사다리꼴을 생각해 보자.Let's think about a trapezoid as follows.<jupyter_code>x_array = (0, 1)
y_array = (1, 2)

py.fill_between(x_array, y_array)
py.axis('equal')
py.axis('off')

py.text(-0.25, 0.5, '$y_i$')
py.text(1.15, 1, '$y_{i+1}$')
py.text(0.5, -0.3, '$\Delta x$')<jupyter_output><empty_output><jupyter_text>사다리꼴의 면적은 다음과 같다.Area of a trapezoid is as follows. $$a_i=\frac{1}{2} \left( y_i + y_{i+1} \right) \Delta x$$  1차 적분First order numerical integration 마찬가지로 일정 간격으로 $x$ 좌표를 나누어 보자.Same as before, let's divide $x$ coordinates in a constant interval.<jupyter_code>d = r * 2.0

n = 10

x_interval = d / n

x_array = py.linspace(-r, r)
y_plus = half_circle(x_array)

x_array_bar = py.arange(-r, r+x_interval*0.1, x_interval)
y_array_bar = half_circle(x_array_bar)
x_interval = x_array_bar[1]-x_array_bar[0]

py.fill_between(x_array, y_plus)

xp, yp = x_array_bar[0], y_array_bar[0]
for x, y in zip(x_array_bar.tolist()[1:], y_array_bar.tolist()[1:]):
    py.fill_between((xp, x), (yp, y), alpha=0.5, color=py.random((1, 3)))
    xp, yp = x, y

py.axis('equal')
py.grid(True)<jupyter_output><empty_output><jupyter_text>사다리꼴의 면적을 하나씩 구해서 더해보자.Let's accumulate the area of trapezoids. $$    Area = \sum_{k=0}^{n-1} F_k$$ $$    F_k = \frac{\Delta x}{2}\left[f(x_k)+f(x_{k+1})\right]$$<jupyter_code>def num_int_1(f, xi, xe, delta_x):
    x_array = py.arange(xi, xe+delta_x*0.1, delta_x)
    
    integration_result = 0.0
    xp = x_array[0]
    yp = f(xp)

    for x_i in x_array[1:]:
        
        y_i = f(x_i)
        area_i = 0.5 * (yp + y_i) * (x_i - xp)
        xp, yp = x_i, y_i
        integration_result += area_i
    
    return integration_result


n = 10
result = num_int_1(half_circle, -r, r, 2*r/n)
print('result =', result)<jupyter_output><empty_output><jupyter_text>예상한 값 1에 더 비슷한 값을 얻기 위해 더 잘게 나누어 보자To obtain the result closer to the expected value of 1, let's divide with a narrower interval.<jupyter_code>n = 100
result = num_int_1(half_circle, -r, r, 2*r/n)
print('result =', result)<jupyter_output><empty_output><jupyter_text>도전 과제 1 : 다른 조건이 같을 때 0차 적분과 사다리꼴 적분의 오차를 비교해 보시오. 필요하면 해당 파이썬 함수를 복사하시오.Try this 1 : Compare the errors of the zeroth and first order integrations of the half circle example above using the same conditions. Duplicate the python function if necessary. 도전 과제 2 : 길이 $L=3[m]$ 인 외팔보가 분포 하중 $\omega=50sin\left(\frac{1}{2L}\pi x\right)[N/m]$을 받고 있을 때 전단력과 굽힘모멘트를 구하시오.Try this 2 : Calculate shear force and bending moment of a cantilever with length $L=3m$ under distributed load $\omega=50sin\left(\frac{1}{2L}\pi x\right)[N/m]$. (ref : C 4.4, Pytel, Kiusalaas & Sharma, Mechanics of Materials, 2nd Ed, SI, Cengage Learning, 2011.)  Final Bell마지막 종<jupyter_code># stackoverfow.com/a/24634221
import os
os.system("printf '\a'");<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1539918809201624, 'fraction_numerical': 0.030852503382949932, 'mean_word_length': 4.341040462427745, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '495943', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1823, 'n_tokens_neox': 1870, 'n_words': 387, 'path': '30_num_int/01_first_order.ipynb', 'repo_name': 'cv2316eca19a/nmisp', 'size': '3695'}