積分計算



### 分数関数の積分（置換と三角関数への変換）

#### 問題：

$$
\int_0^1 \frac{1}{x^2 - x + 1} dx
$$

#### 解法概要：

1. **平方完成**：

   $$
   x^2 - x + 1 = \left(x - \frac{1}{2}\right)^2 + \frac{3}{4}
   $$

2. **置換**：

   $$
   x - \frac{1}{2} = \frac{\sqrt{3}}{2} \tan \theta \quad \Rightarrow \quad dx = \frac{\sqrt{3}}{2\cos^2\theta} d\theta
   $$

3. **被積分関数の変形**：

   $$
   \frac{1}{(x - \frac{1}{2})^2 + \frac{3}{4}} = \frac{4}{3} \cos^2\theta
   $$

4. **積分区間の変換**（x = 0 → θ = -π/6, x = 1 → θ = π/6）

5. **積分実行**：

   $$
   \int_{-\frac{\pi}{6}}^{\frac{\pi}{6}} \frac{4}{3} \cos^2\theta \cdot \frac{\sqrt{3}}{2\cos^2\theta} d\theta
   = \frac{2\sqrt{3}}{3} \int_{-\frac{\pi}{6}}^{\frac{\pi}{6}} d\theta
   = \frac{2\sqrt{3}}{3} \cdot \frac{\pi}{3}
   = \frac{2\pi\sqrt{3}}{9}
   $$

---

### 積分：積×三角関数（部分積分または公式）

#### 問題：

$$
\frac{1}{2} \int x e^x \sin 2x \, dx
$$

#### 解法概要：

積分公式（積×三角関数×指数関数）に基づき、部分積分2回または公式によって直接：

$$
\frac{1}{2} \int x e^x \sin 2x \, dx
= \frac{1}{50} (5x + 3)e^x \sin 2x + \frac{1}{25} (2 - 5x) e^x \cos 2x + C
$$

---

### 三角関数の積分（置換と対称性）

#### 問題：

$$
\int_0^{\frac{\pi}{2}} \frac{\sin x}{\sin x + \cos x} dx
$$

#### 解法概要：

1. **置換**：

   $$
   x + \frac{\pi}{4} = t \quad \Rightarrow \quad dx = dt
   $$

   すると分母が：

   $$
   \sin x + \cos x = \sqrt{2} \sin(x + \frac{\pi}{4})
   $$

2. **区間変換**：
   $x = 0 \Rightarrow t = \frac{\pi}{4}, \quad x = \frac{\pi}{2} \Rightarrow t = \frac{3\pi}{4}$

3. **新しい形の積分**：

   $$
   \int_{\frac{\pi}{4}}^{\frac{3\pi}{4}} \frac{\sin(t - \frac{\pi}{4})}{\sqrt{2} \sin t} dt
   $$

   分子：和差公式で展開 → 分母の sin t と整理

4. **最終的に**：

   $$
   = \frac{1}{2} \int_{\frac{\pi}{4}}^{\frac{3\pi}{4}} \left(1 - \frac{(\sin t)'}{\sin t} \right) dt
   = \frac{1}{2} \left[ t - \log|\sin t| \right]_{\frac{\pi}{4}}^{\frac{3\pi}{4}} = \frac{\pi}{4}
   $$

---

### Weierstrassの置換（タンジェントの半角公式）

#### 問題：

$$
\int \frac{1}{\sin x + \cos x + 1} dx
$$

#### 解法概要：

1. **Weierstrass置換（tan(x/2) = t）**

   $$
   \sin x = \frac{2t}{1 + t^2}, \quad \cos x = \frac{1 - t^2}{1 + t^2}, \quad dx = \frac{2}{1 + t^2} dt
   $$

2. **式をすべてtで表す**：
   分母：

   $$
   \frac{2t}{1 + t^2} + \frac{1 - t^2}{1 + t^2} + 1 = \frac{2t + 1 - t^2 + 1 + t^2}{1 + t^2} = \frac{2t + 2}{1 + t^2}
   $$

3. **積分式に代入**：

   $$
   \int \frac{1}{\sin x + \cos x + 1} dx = \int \frac{1}{\frac{2t + 2}{1 + t^2}} \cdot \frac{2}{1 + t^2} dt
   = \int \frac{1}{t + 1} dt = \log|t + 1| + C
   $$

   もとに戻して：

   $$
   = \log \left| \tan\frac{x}{2} + 1 \right| + C
   $$

---

### 補足（下部の公式）：

Weierstrassの置換で使う三角関数の式変形：

* $\tan x = \frac{2t}{1 - t^2}$
* $\sin x = \frac{2t}{1 + t^2}$
* $\cos x = \frac{1 - t^2}{1 + t^2}$
* $dx = \frac{2}{1 + t^2} dt$

---

```Python
# Program Name: integral_analysis_all.py
# Creation Date: 20250604
# Overview: Evaluate and visualize 4 symbolic integrals using sympy and scipy
# Usage: Run in Jupyter Notebook or Python environment with required libraries installed

# -------------------- Install Required Libraries --------------------
!pip install numpy matplotlib sympy scipy

# -------------------- Import Libraries --------------------
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import quad
import sympy as sp

# -------------------- 1. Quadratic Denominator Integral --------------------
def f1(x):
    return 1 / (x**2 - x + 1)

area1, _ = quad(f1, 0, 1)
print("① ∫₀¹ 1 / (x² - x + 1) dx =", area1)  # ≈ 2π√3 / 9

# -------------------- 2. x * exp(x) * sin(2x) --------------------
x = sp.symbols('x')
expr2 = (1/2) * x * sp.exp(x) * sp.sin(2*x)
result2 = sp.integrate(expr2, x)
print("\n② ∫ x e^x sin(2x) dx / 2 =")
sp.pprint(result2, use_unicode=True)

# -------------------- 3. sin(x)/(sin x + cos x) from 0 to π/2 --------------------
def f3(x):
    return np.sin(x) / (np.sin(x) + np.cos(x))

area3, _ = quad(f3, 0, np.pi/2)
print("\n③ ∫₀^{π/2} sin x / (sin x + cos x) dx =", area3)  # ≈ π/4

# -------------------- 4. Weierstrass Substitution Integral --------------------
def f4(x):
    return 1 / (np.sin(x) + np.cos(x) + 1)

area4, _ = quad(f4, 0, np.pi/2)
print("\n④ ∫ 1 / (sin x + cos x + 1) dx from 0 to π/2 =", area4)

# -------------------- Visualization --------------------
x_vals = np.linspace(0.01, np.pi/2 - 0.01, 400)

plt.figure(figsize=(10, 6))
plt.plot(x_vals, f1(x_vals), label='① $1 / (x^2 - x + 1)$')
plt.plot(x_vals, f3(x_vals), label='③ $\\sin x / (\\sin x + \\cos x)$')
plt.plot(x_vals, f4(x_vals), label='④ $1 / (\\sin x + \\cos x + 1)$')
plt.title("Comparison of Integrand Shapes")
plt.xlabel("x")
plt.ylabel("f(x)")
plt.grid(True)
plt.legend()
plt.tight_layout()
plt.show()

```