{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\tHiroshi TAKEMOTO\n", "\t(take.pwave@gmail.com)\n", "\t

## 微分

\n", "\t

\n", "\t\t高校で習う微分の公式は、以下の２つです。\n", "\t\t

\n", "\t\t
• $\\frac{d}{dx} c = 0$
• \n", "\t\t
• $\\frac{d}{dx} x^n = n x^{n-1}$
• \n", "\t\t
\n", "\t

\n", "\t

\n", "\t\tsageで微分は、diff関数を使います。diff関数の使い方は以下の通りです。次数を省略すると1階微分となります。\n", "

\n",
"diff(関数, 微分する変数, 次数)\n",
"
\n", "\t\t上記公式をsageで実行すると以下のようになります。\t\t\n", "\t

\n", "" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%HTML\n", "" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# jupyter用のdisplayメソッド\n", "from IPython.display import display, Latex, HTML, Math, JSON\n", "# sageユーティリティ\n", "load('script/sage_util.py')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "n*x^(n - 1)\n" ] } ], "source": [ "# 変数と関数を生成\n", "x, c, n = var('x c n')\n", "# 微分\n", "print diff(c, x)\n", "print diff(x^n, x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

### Sageで微分公式を確認

\n", "\t

\n", "\tsageで変数xの関数fを以下のように定義します。\n", "\t

\n",
"x = var('x')\n",
"f = function('f', x)\n",
"\t
\n", "\tこの関数を微分すると、$f'$は以下のようにD[0](f)(x)のように返されます。\t\n", "\t

\n", "\n", "" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "D[0](f)(x)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 変数x, 関数fを定義\n", "c, x = var('c x')\n", "f(x) = function('f')(x)\n", "# 関数fを変数xで微分\n", "diff(f(x), x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

\n", "\t\t以下に示す微分の公式をSageで実行し、f'=D[0](f)(x)の置換を実行した結果を示します。\n", "\t\t

\n", "\t\t
• $(cf(x))' = cf'(x)$
• \n", "\t\t
• $(f(x) + g(x))' = f'(x) + g'(x)$
• \n", "\t\t
• $(f(x)g(x))' = f'(x)g(x) + f(x)g'(x)$
• \n", "\t\t
• $(f(x)/g(x))' = (f(x)'g(x) - g'(x)f(x))/g(x)^2$
• \n", "\t\t
• $(1/g(x))' = -g'(x)/g(x)^2$
• \n", "\t\t
• $(f(g(x)))'= f'(g(x))g'(x)$
• \n", "\t\t
\n", "\t

\n", "\n", "" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# df/dx = f', dg/dx = g'の置き換え関数の定義\n", "repStr = lambda f : str(f).replace(\"D[0](f)\", \"f'\").replace(\"D[0](g)\", \"g'\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "c*f'(x)\n", "f'(x) + g'(x)\n", "g(x)*f'(x) + f(x)*g'(x)\n", "(g(x)*f'(x) - f(x)*g'(x))/g(x)^2\n", "-g'(x)/g(x)^2\n", "f'(g(x))*g'(x)\n" ] } ], "source": [ "# 関数gを定義\n", "g(x) = function('g')(x)\n", "# 微分公式をsageを使って実行\n", "print repStr(diff(c*f(x), x))\n", "print repStr(diff(f(x)+g(x), x))\n", "print repStr(diff(f(x)*g(x),x))\n", "print repStr(diff(f(x)/g(x), x).simplify_full())\n", "print repStr(diff(1/g(x),x))\n", "print repStr(diff(f(g(x)), x))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

### いろんな関数の微分

\n", "\t

\n", "\t\t以下に主な関数を微分した結果を示します。\t\t\n", "\t

\n", "" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "

c0
x^nn*x^(n - 1)
sin(x)cos(x)
cos(x)-sin(x)
e^xe^x
log(x)1/x
" ], "text/plain": [ "<__main__.Table2Html instance at 0x7fb2983e4680>" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "var('x c n')\n", "sts=[c, x^n, sin(x), cos(x), exp(x), log(x)]\n", "Table2Html( [(st, diff(st, x)) for st in sts], header=[\"関数\", \"微分結果\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

### 微分の応用

\n", "\t

\n", "\t\t微分の応用として、接線の方程式とその法線を計算してみましょう。\n", "\t\t関数f(x)を以下のように定義します。\n", "$$\n", "\t\tf(x) = x^3 - x^2 - 2*x\n", "$$\t\t\t\n", "\t\tこのとき、$x = x_1$での接線の式は、以下のようになります。\n", "$$\n", "\t\ty - y_{1} = f'(x_{1})(x - x_{1})\n", "$$\t\t\n", "\t\tまた、法線は接線と直交することから、以下のようになります。\n", "$$\n", "\t\ty - y_{1} = -\\frac{1}{f'(x_{1})}(x - x_{1})\n", "$$\t\t\t\t\n", "\t\tf(x)の微分した式をg(x)とし、x=0での接線と法線を計算し、プロットしてみます。\t\t\n", "\t

## 積分

\n", "\t

\n", "\t\tsageでは積分は、integrate関数で計算します。\n", "\t\t

\n",
"\t\tintegrate(被積分関数, 積分変数)\n",
"\t\tまた\n",
"\t\tintegrate(被積分関数, 積分変数, 積分範囲)\n",
"\t\tとすると定積分を計算します。\n",
"\t\t
\n", "\t

\n", "\t

\n", "\t\t例として、$\\int x\\, dx$を計算した後、その微分を求めています。\n", "\t\t元のxが返されています。\t\t\n", "\t

\n", "" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "1/2*x^2" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "f(x) = integrate(x, x)\n", "show(f(x))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "

cc*x
1/xlog(x)
e^xe^x
log(x)x*log(x) - x
sin(x)-cos(x)
f'(x)/f(x)log(f(x))
" ], "text/plain": [ "<__main__.Table2Html instance at 0x7fb29812b2d8>" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# いろんな関数の積分\n", "var('x c')\n", "f(x) = function('f')(x)\n", "sts=[c, 1/x, exp(x), log(x), sin(x), diff(f(x), x)/f(x)]\n", "Table2Html( [(repStr(st), integrate(st, x)) for st in sts], header=[\"関数\", \"積分結果\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

### 定積分

\n", "\t

\n", "\t\t次に定積分$\\int_{0}^{\\pi/2}sin(x)\\, dx$の例を示します。\t\n", "\t

\n", "\t

\n", "\t\t$sin(x)$を0から$\\pi/2$の範囲でプロットすると以下のようになります。\n", "\t

\n", "\t\tsinの積分は、-cosですから、定積分は以下のようになります。\n", "$$\n", "\t\t[ -cos(x) ]_0^\\pi = \\{ 0 - (-1) \\} = 1\n", "$$\t\t\n", "\t

\n", "\t

\n", "\t\tそれでは、Sageを使って、$sin(x)$を0から$\\pi/2$の範囲で積分してみましょう。\n", "\t

\n", "" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "integrate(sin(x), x, 0, pi/2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

\n", "\t\tもう一つ、以下の関数を積分した例をみてみましょう。\n", "$$\n", "\t\t\\int_{0}^{3}x^2 + 2sin(x)\\,dx\n", "$$\t\t\n", "\t

\n", "\t

\n", "\t\t先ほどと同様に、積分範囲をプロットしてみます。\n", "\t

\n", "\t\t積分の結果は、関数で返されるのでN(_)を使って数値に変換します。\n", "\t

\n", "\t

\n", "\t\tこのように関数のプロット結果から推定される概算と積分の結果をみながら、\n", "\t\t計算結果を確認しながら、進められるのもSageの特徴です。\n", "\t

\n", "" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "-2*cos(3) + 11" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "integrate(x^2+2*sin(x), x, 0, 3)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "12.9799849932009" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "N(_)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

#### 数値積分

\n", "\t

\n", "\t\tすべての関数が定積分できるとは限りません、そこでSageには数値積分を行う\n", "\t\tnumerical_integral関数が用意されています。\n", "\t

\n", "\t

\n", "\t\tnumerical_integral関数の使い方は、以下の通りです。\n", "\t\t関数の戻り値は、積分結果と誤差のタプルが返されます。\n", "\t

\n", "\t
\n",
"\tnumerical_integral(被関分館数, 積分範囲)\n",
"\t
\n", "\t

\n", "\t\t例として、$sin(x)$を0から$\\pi/2$で数値積分した結果を以下に示します。\n", "\t

\t\n", "" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(1.0, 1.1102230246251565e-14)" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sigma, error = numerical_integral(sin(x), 0, pi/2); (sigma, error)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

### 積分の応用

\n", "\t

\n", "\t\t積分の応用例として、サイクロイドの計算をしてみます。\n", "\t

\n", "\t

\n", "\t\tサイクロイドは、tをパラメータとして、以下のような式で表されます。\n", "$$\n", "\t\t\\left\\{\\begin{eqnarray}\n", "\t\t\tx &=& 2(t-sin(t)) \\\\\n", "\t\t\ty &=& 2(1-cos(t))\n", "\t\t\\end{eqnarray}\\right.\n", "$$\t\t\n", "\t

\n", "\t

\n", "\t\tこの曲線の長さは、以下の積分で計算することができます。\n", "$$\n", "\t\t\\int_{0}^{2\\pi}\\sqrt{(\\frac{dx}{dt})^2+(\\frac{dy}{dt})^2}dt\n", "$$\t\n", "\t\t解析解では、この結果は16となります。\t\n", "\t