1. maskot1977

    Posted

    maskot1977
Changes in title
+Sympyを用いた演算
Changes in tags
Changes in body
Source | HTML | Preview
@@ -0,0 +1,1170 @@
+
+# Sympy を用いた演算
+
+
+
+```python
+# 現在の Google Colab では Sympy 1.1.1 が入っている。
+# Sympy 1.1 では後述の「特殊解」が計算できないので、1.3 にアップグレードする。
+!pip install sympy==1.3
+```
+
+ Collecting sympy==1.3
+ [?25l Downloading https://files.pythonhosted.org/packages/dd/f6/ed485ff22efdd7b371d0dbbf6d77ad61c3b3b7e0815a83c89cbb38ce35de/sympy-1.3.tar.gz (5.9MB)
+  |████████████████████████████████| 5.9MB 4.1MB/s
+ [?25hRequirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.6/dist-packages (from sympy==1.3) (1.1.0)
+ Building wheels for collected packages: sympy
+ Building wheel for sympy (setup.py) ... [?25l[?25hdone
+ Created wheel for sympy: filename=sympy-1.3-cp36-none-any.whl size=5199947 sha256=bc14a07ac6744969566fce4c541612a22adecb5bc83223ccc225ed28f415c38d
+ Stored in directory: /root/.cache/pip/wheels/6c/59/86/478e3c0f298368c119095cc5985dedac57c0e35a85c737f823
+ Successfully built sympy
+ Installing collected packages: sympy
+ Found existing installation: sympy 1.1.1
+ Uninstalling sympy-1.1.1:
+ Successfully uninstalled sympy-1.1.1
+ Successfully installed sympy-1.3
+
+
+
+```python
+import sympy as sym
+from sympy.plotting import plot
+sym.init_printing(use_unicode=True)
+%matplotlib inline
+```
+
+
+```python
+# Google Colab 使用の場合、SympyによるTeX表示をサポートするために実行する
+def custom_latex_printer(exp,**options):
+ from google.colab.output._publish import javascript
+ url = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default"
+ javascript(url=url)
+ return sym.printing.latex(exp,**options)
+
+sym.init_printing(use_latex="mathjax",latex_printer=custom_latex_printer)
+```
+
+## シンボルの定義
+
+指定した文字をシンボル(変数を表す文字)として扱います。
+
+
+```python
+a = sym.Symbol('a')
+```
+
+まとめて定義したいときは次のように。
+
+
+```python
+a, b, c, x, y = sym.symbols("a b c x y")
+```
+
+指定した文字を関数として扱います。
+
+
+```python
+f = sym.Function('f')
+g = sym.Function('g')
+```
+
+## 簡単な多項式
+
+
+```python
+# 数式は英語で numerial expression または numerical formula
+expr = x**2-12*x+8
+expr
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$x^{2} - 12 x + 8$$
+
+
+
+
+```python
+# 得られた関数を図示
+plot(expr, (x, -20, 20))
+```
+
+
+![png](output_12_0.png)
+
+
+
+
+
+ <sympy.plotting.plot.Plot at 0x7faf57ced080>
+
+
+
+## 因数分解する
+
+
+```python
+# 因数分解
+expr = x**2 + 2*x + 1
+sym.factor(expr)
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\left(x + 1\right)^{2}$$
+
+
+
+## 方程式を解く
+
+
+```python
+# 等式は英語で equation または equality
+eq = sym.Eq(expr)
+eq
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$x^{2} - 12 x + 8 = 0$$
+
+
+
+
+```python
+# このように右辺を明記しても良い
+eq = sym.Eq(x**2-12*x, -8)
+eq
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$x^{2} - 12 x = -8$$
+
+
+
+
+```python
+# 方程式を解く
+sym.solve(eq)
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\left [ - 2 \sqrt{7} + 6, \quad 2 \sqrt{7} + 6\right ]$$
+
+
+
+
+```python
+# 代数を使った式も取り扱える
+eq = sym.Eq(a * x ** 2 + b * x + c)
+eq
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$a x^{2} + b x + c = 0$$
+
+
+
+
+```python
+# xについて解く
+sym.solve(eq, x)
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\left [ \frac{- b + \sqrt{- 4 a c + b^{2}}}{2 a}, \quad - \frac{b + \sqrt{- 4 a c + b^{2}}}{2 a}\right ]$$
+
+
+
+
+```python
+# 連立方程式
+eq1 = 3 * x + 5 * y - 29
+eq2 = x + y - 7
+
+sym.solve([eq1, eq2])
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\left \{ x : 3, \quad y : 4\right \}$$
+
+
+
+## 微分
+
+
+```python
+expr = 2 * x ** 2 + 5 * x - 3
+expr
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$2 x^{2} + 5 x - 3$$
+
+
+
+
+```python
+# 微分
+sym.Derivative(expr)
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\frac{d}{d x} \left(2 x^{2} + 5 x - 3\right)$$
+
+
+
+
+```python
+# 微分を計算する
+sym.Derivative(expr).doit()
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$4 x + 5$$
+
+
+
+
+```python
+# このように書いても同じ
+sym.diff(expr)
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$4 x + 5$$
+
+
+
+
+```python
+# sym.Eq を使って等式として表記
+sym.Eq(sym.Derivative(expr), sym.diff(expr))
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\frac{d}{d x} \left(2 x^{2} + 5 x - 3\right) = 4 x + 5$$
+
+
+
+
+```python
+# 上と同じ意味だが、変数が1つだけなら上のように省略できる。
+# 変数が2つ以上の場合は何で微分するか明記しなければいけない
+sym.Eq(sym.Derivative(expr, x), sym.diff(expr, x))
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\frac{d}{d x} \left(2 x^{2} + 5 x - 3\right) = 4 x + 5$$
+
+
+
+
+```python
+# 2階微分
+sym.Eq(sym.Derivative(expr, x, 2), sym.diff(expr, x, 2))
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\frac{d^{2}}{d x^{2}} \left(2 x^{2} + 5 x - 3\right) = 4$$
+
+
+
+
+```python
+# xについて微分して x=1を代入
+sym.diff(expr).subs(x, 1)
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$9$$
+
+
+
+
+```python
+# x について微分
+expr = a * x ** 2 + b * x * y + c * y ** 2
+sym.diff(expr, x)
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$2 a x + b y$$
+
+
+
+
+```python
+# xについて微分して x=1を代入
+sym.diff(expr, x).subs(x, 1)
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$2 a + b y$$
+
+
+
+### 微分の公式
+
+
+```python
+expr = x ** a
+sym.Eq(sym.Derivative(expr, x), sym.diff(expr, x))
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\frac{\partial}{\partial x} x^{a} = \frac{a x^{a}}{x}$$
+
+
+
+
+```python
+expr = sym.sin(x)
+sym.Eq(sym.Derivative(expr, x), sym.diff(expr, x))
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\frac{d}{d x} \sin{\left (x \right )} = \cos{\left (x \right )}$$
+
+
+
+
+```python
+expr = sym.cos(x)
+sym.Eq(sym.Derivative(expr, x), sym.diff(expr, x))
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\frac{d}{d x} \cos{\left (x \right )} = - \sin{\left (x \right )}$$
+
+
+
+
+```python
+expr = sym.exp(x)
+sym.Eq(sym.Derivative(expr, x), sym.diff(expr, x))
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\frac{d}{d x} e^{x} = e^{x}$$
+
+
+
+
+```python
+expr = a**x
+sym.Eq(sym.Derivative(expr, x), sym.diff(expr, x))
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\frac{\partial}{\partial x} a^{x} = a^{x} \log{\left (a \right )}$$
+
+
+
+
+```python
+expr = sym.log(x)
+sym.Eq(sym.Derivative(expr, x), sym.diff(expr, x))
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\frac{d}{d x} \log{\left (x \right )} = \frac{1}{x}$$
+
+
+
+
+```python
+expr = sym.sqrt(x)
+sym.Eq(sym.Derivative(expr, x), sym.diff(expr, x))
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\frac{d}{d x} \sqrt{x} = \frac{1}{2 \sqrt{x}}$$
+
+
+
+
+```python
+expr = sym.tan(x)
+sym.Eq(sym.Derivative(expr, x), sym.diff(expr, x))
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\frac{d}{d x} \tan{\left (x \right )} = \tan^{2}{\left (x \right )} + 1$$
+
+
+
+
+```python
+expr = sym.sin(x)**a
+sym.Eq(sym.Derivative(expr, x), sym.diff(expr, x))
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\frac{\partial}{\partial x} \sin^{a}{\left (x \right )} = \frac{a \sin^{a}{\left (x \right )} \cos{\left (x \right )}}{\sin{\left (x \right )}}$$
+
+
+
+
+```python
+expr = sym.cos(x)**a
+sym.Eq(sym.Derivative(expr, x), sym.diff(expr, x))
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\frac{\partial}{\partial x} \cos^{a}{\left (x \right )} = - \frac{a \sin{\left (x \right )} \cos^{a}{\left (x \right )}}{\cos{\left (x \right )}}$$
+
+
+
+
+```python
+expr = sym.tan(x)**a
+sym.Eq(sym.Derivative(expr, x), sym.diff(expr, x))
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\frac{\partial}{\partial x} \tan^{a}{\left (x \right )} = \frac{a \left(\tan^{2}{\left (x \right )} + 1\right) \tan^{a}{\left (x \right )}}{\tan{\left (x \right )}}$$
+
+
+
+
+```python
+expr = sym.log(x)**a
+sym.Eq(sym.Derivative(expr, x), sym.diff(expr, x))
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\frac{\partial}{\partial x} \log{\left (x \right )}^{a} = \frac{a \log{\left (x \right )}^{a}}{x \log{\left (x \right )}}$$
+
+
+
+
+```python
+expr = f(x) * g(x)
+sym.Eq(sym.Derivative(expr, x), sym.diff(expr, x))
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\frac{d}{d x} f{\left (x \right )} g{\left (x \right )} = f{\left (x \right )} \frac{d}{d x} g{\left (x \right )} + g{\left (x \right )} \frac{d}{d x} f{\left (x \right )}$$
+
+
+
+
+```python
+expr = f(x) / g(x)
+sym.Eq(sym.Derivative(expr, x), sym.diff(expr, x))
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\frac{d}{d x} \frac{f{\left (x \right )}}{g{\left (x \right )}} = - \frac{f{\left (x \right )} \frac{d}{d x} g{\left (x \right )}}{g^{2}{\left (x \right )}} + \frac{\frac{d}{d x} f{\left (x \right )}}{g{\left (x \right )}}$$
+
+
+
+## 常微分方程式
+
+2 f'(x) + 5 f(x) = 0 を解く
+
+
+```python
+# 常微分方程式
+eq = sym.Eq(2 * f(x).diff(x,1) + 5 * f(x))
+eq
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$5 f{\left (x \right )} + 2 \frac{d}{d x} f{\left (x \right )} = 0$$
+
+
+
+
+```python
+# 一般解
+ans = sym.dsolve(eq)
+print(ans)
+ans
+```
+
+ Eq(f(x), C1*exp(-5*x/2))
+
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$f{\left (x \right )} = C_{1} e^{- \frac{5 x}{2}}$$
+
+
+
+
+```python
+# 特殊解
+ans = sym.dsolve(eq, ics={f(0):1})
+print(ans)
+ans
+```
+
+ Eq(f(x), exp(-5*x/2))
+
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$f{\left (x \right )} = e^{- \frac{5 x}{2}}$$
+
+
+
+
+```python
+plot(ans.rhs, (x, -10, 10)) # rhs は右辺(Right-hand side)の意味
+```
+
+
+![png](output_52_0.png)
+
+
+
+
+
+ <sympy.plotting.plot.Plot at 0x7faf54dd4320>
+
+
+
+
+```python
+# 特殊解で x = 2 のとき
+print(ans.subs(x, 2))
+ans.subs(x, 2)
+```
+
+ Eq(f(2), exp(-5))
+
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$f{\left (2 \right )} = e^{-5}$$
+
+
+
+
+```python
+# evalf というメソッドを使えば浮動小数点まで展開してくれる
+ans.subs(x, 2).evalf()
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$f{\left (2 \right )} = 0.00673794699908547$$
+
+
+
+## 演習問題1
+
+* 常微分方程式 f''(x) + f'(x) + 4 f(x) = 0 の一般解を求めなさい。
+
+* f(0) = 1 のときの特殊解を求めなさい。
+
+* その特殊解に x = 2 を代入したときの値を求めなさい。
+
+# 積分
+
+
+```python
+# 積分の式
+expr = x ** a
+integ = sym.Integral(expr, x)
+print(integ)
+integ
+```
+
+ Integral(x**a, x)
+
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\int x^{a}\, dx$$
+
+
+
+
+```python
+# 積分の実行
+integ.doit()
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\begin{cases} \frac{x^{a + 1}}{a + 1} & \text{for}\: a \neq -1 \\\log{\left (x \right )} & \text{otherwise} \end{cases}$$
+
+
+
+
+```python
+# このように書いても同じ
+sym.integrate(expr, x)
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\begin{cases} \frac{x^{a + 1}}{a + 1} & \text{for}\: a \neq -1 \\\log{\left (x \right )} & \text{otherwise} \end{cases}$$
+
+
+
+
+```python
+# sym.Eq を使って等式として表記
+eq = sym.Eq(sym.Integral(expr, x), sym.integrate(expr, x))
+print(eq)
+eq
+```
+
+ Eq(Integral(x**a, x), Piecewise((x**(a + 1)/(a + 1), Ne(a, -1)), (log(x), True)))
+
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\int x^{a}\, dx = \begin{cases} \frac{x^{a + 1}}{a + 1} & \text{for}\: a \neq -1 \\\log{\left (x \right )} & \text{otherwise} \end{cases}$$
+
+
+
+## 積分の公式
+
+
+```python
+expr = 1/x
+eq = sym.Eq(sym.Integral(expr, x), sym.integrate(expr, x))
+print(eq)
+eq
+```
+
+ Eq(Integral(1/x, x), log(x))
+
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\int \frac{1}{x}\, dx = \log{\left (x \right )}$$
+
+
+
+
+```python
+expr = a ** x
+eq = sym.Eq(sym.Integral(expr, x), sym.integrate(expr, x))
+print(eq)
+eq
+```
+
+ Eq(Integral(a**x, x), Piecewise((a**x/log(a), Ne(log(a), 0)), (x, True)))
+
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\int a^{x}\, dx = \begin{cases} \frac{a^{x}}{\log{\left (a \right )}} & \text{for}\: \log{\left (a \right )} \neq 0 \\x & \text{otherwise} \end{cases}$$
+
+
+
+
+```python
+expr = sym.exp(x)
+eq = sym.Eq(sym.Integral(expr, x), sym.integrate(expr, x))
+print(eq)
+eq
+```
+
+ Eq(Integral(exp(x), x), exp(x))
+
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\int e^{x}\, dx = e^{x}$$
+
+
+
+
+```python
+expr = sym.sin(x)
+eq = sym.Eq(sym.Integral(expr, x), sym.integrate(expr, x))
+print(eq)
+eq
+```
+
+ Eq(Integral(sin(x), x), -cos(x))
+
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\int \sin{\left (x \right )}\, dx = - \cos{\left (x \right )}$$
+
+
+
+
+```python
+expr = sym.cos(x)
+eq = sym.Eq(sym.Integral(expr, x), sym.integrate(expr, x))
+print(eq)
+eq
+```
+
+ Eq(Integral(cos(x), x), sin(x))
+
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\int \cos{\left (x \right )}\, dx = \sin{\left (x \right )}$$
+
+
+
+
+```python
+expr = sym.tan(x)
+eq = sym.Eq(sym.Integral(expr, x), sym.integrate(expr, x))
+print(eq)
+eq
+```
+
+ Eq(Integral(tan(x), x), -log(cos(x)))
+
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\int \tan{\left (x \right )}\, dx = - \log{\left (\cos{\left (x \right )} \right )}$$
+
+
+
+
+```python
+expr = sym.log(x)
+eq = sym.Eq(sym.Integral(expr, x), sym.integrate(expr, x))
+print(eq)
+eq
+```
+
+ Eq(Integral(log(x), x), x*log(x) - x)
+
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\int \log{\left (x \right )}\, dx = x \log{\left (x \right )} - x$$
+
+
+
+
+```python
+expr = 1/sym.cos(x)**2
+eq = sym.Eq(sym.Integral(expr, x), sym.integrate(expr, x))
+print(eq)
+eq
+```
+
+ Eq(Integral(cos(x)**(-2), x), sin(x)/cos(x))
+
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\int \frac{1}{\cos^{2}{\left (x \right )}}\, dx = \frac{\sin{\left (x \right )}}{\cos{\left (x \right )}}$$
+
+
+
+
+```python
+expr = 1/sym.sin(x)**2
+eq = sym.Eq(sym.Integral(expr, x), sym.integrate(expr, x))
+print(eq)
+eq
+```
+
+ Eq(Integral(sin(x)**(-2), x), -cos(x)/sin(x))
+
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\int \frac{1}{\sin^{2}{\left (x \right )}}\, dx = - \frac{\cos{\left (x \right )}}{\sin{\left (x \right )}}$$
+
+
+
+
+```python
+expr = (x-a)**b
+eq = sym.Eq(sym.Integral(expr, x), sym.integrate(expr, x))
+print(eq)
+eq
+```
+
+ Eq(Integral((-a + x)**b, x), Piecewise(((-a + x)**(b + 1)/(b + 1), Ne(b, -1)), (log(-a + x), True)))
+
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\int \left(- a + x\right)^{b}\, dx = \begin{cases} \frac{\left(- a + x\right)^{b + 1}}{b + 1} & \text{for}\: b \neq -1 \\\log{\left (- a + x \right )} & \text{otherwise} \end{cases}$$
+
+
+
+
+```python
+expr = 1/sym.sin(x)
+eq = sym.Eq(sym.Integral(expr, x), sym.integrate(expr, x))
+print(eq)
+eq
+```
+
+ Eq(Integral(1/sin(x), x), log(cos(x) - 1)/2 - log(cos(x) + 1)/2)
+
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\int \frac{1}{\sin{\left (x \right )}}\, dx = \frac{\log{\left (\cos{\left (x \right )} - 1 \right )}}{2} - \frac{\log{\left (\cos{\left (x \right )} + 1 \right )}}{2}$$
+
+
+
+## 定積分
+
+
+```python
+expr = (x-a)*(b-x)
+eq = sym.Eq(sym.Integral(expr, (x, a, b)), sym.integrate(expr, (x, a, b))).factor()
+print(eq)
+eq
+```
+
+ Eq(-Integral((-a + x)*(-b + x), (x, a, b)), -(a - b)**3/6)
+
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$- \int_{a}^{b} \left(- a + x\right) \left(- b + x\right)\, dx = - \frac{\left(a - b\right)^{3}}{6}$$
+
+
+
+
+```python
+expr = x/(x**2 + 1)
+eq = sym.Eq(sym.Integral(expr, (x, 1, 2)), sym.integrate(expr, (x, 1, 2)))
+print(eq)
+eq
+```
+
+ Eq(Integral(x/(x**2 + 1), (x, 1, 2)), -log(2)/2 + log(5)/2)
+
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$\int_{1}^{2} \frac{x}{x^{2} + 1}\, dx = - \frac{\log{\left (2 \right )}}{2} + \frac{\log{\left (5 \right )}}{2}$$
+
+
+
+
+```python
+sym.integrate(expr, (x, 1, 2)).evalf()
+```
+
+
+<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>
+
+
+
+
+
+$$0.458145365937078$$
+
+
+
+## 演習問題2
+
+* f(x) = 4/(x**2 + 1) を区間[0, 1]で定積分しなさい。