file:/~/python/doing_math_with_python/differential.ipynb
cc by Shigeto R. Nishitani 2017

# 微分(Diff)-I¶

## 単純な微分(diff)¶

In [2]:
from sympy import *
x,y = symbols('x y')

diff(x**2-3*x+2,x)

Out[2]:
2*x - 3

In [2]:
diff(sin(x),x,x)

Out[2]:
-sin(x)

さらに高階微分は次のように．これはxについての3次微分を表わす．

In [3]:
diff(x**4,x,3)

Out[3]:
24*x

## 偏微分(PartialDiff)¶

In [7]:
eq1=(x+y)/(x*y)
pprint(diff(eq1,x))

 1    x + y
─── - ─────
x⋅y     2
x ⋅y


## 例題:関数の微分と増減表¶

$$\frac {x}{{x}^{2}-2x+4}$$

### 解答例¶

In [1]:
from sympy import *
from sympy.plotting import plot
x = symbols('x')

f0 = x/(x**2 -2*x + 4)
pprint(f0)
pprint(diff(f0,x))

df0 = diff(f0,x)

p = plot(f0,df0, show=False)
p[0].line_color = 'b'
p[1].line_color = 'r'
p.show()

     x
────────────
2
x  - 2⋅x + 4
x⋅(-2⋅x + 2)         1
─────────────── + ────────────
2    2
⎛ 2          ⎞    x  - 2⋅x + 4
⎝x  - 2⋅x + 4⎠


## 例題:接線(Tangent)¶

$$y={x}^{3}-2\,{x}^{2}-35\,x$$

### 解答例¶

In [8]:
from sympy import *
from sympy.plotting import plot
x = symbols('x')

f0 = x**3 -2*x**2 - 35*x
f0

Out[8]:
$$x^{3} - 2 x^{2} - 35 x$$

In [9]:
pprint(diff(f0,x))
df0 = diff(f0,x)

   2
3⋅x  - 4⋅x - 35


In [10]:
x0 = 3
a = df0.subs({x:x0})
f1 = a*(x-x0)+f0.subs({x:x0})
pprint(f1)

-20⋅x - 36


2つの関数を同時にプロット．

In [11]:
p = plot(f0,f1, show=False)
p[0].line_color = 'b'
p[1].line_color = 'r'
p.show()


# 極限(limit)¶

In [5]:
from sympy import *
x = symbols('x')
print(limit(x, x, 0))
print(limit(x, x, oo))
print(limit(1/x, x, oo))
print(limit(x/sin(x), x, 0))

0
oo
0
1

In [6]:
d = symbols('d')
f = x**2
df = diff(f,x)
print(df.subs({x:2}))

limit((f.subs({x:x+d})-f.subs({x:x}))/d,d,0)

4

Out[6]:
$$2 x$$

# 微分(Diff)-II¶

## 級数展開(series)¶

Taylor級数は以下のようにして，中心点(x=a)，次数(4次)を指定する．

In [1]:
from sympy import *
from sympy.plotting import plot
init_printing()

x,a = symbols('x a')

sin(x).series(x,a,4)

Out[1]:
$$\sin{\left (a \right )} + \left(- a + x\right) \cos{\left (a \right )} - \frac{1}{2} \left(- a + x\right)^{2} \sin{\left (a \right )} - \frac{1}{6} \left(- a + x\right)^{3} \cos{\left (a \right )} + \mathcal{O}\left(\left(- a + x\right)^{4}; x\rightarrowa\right)$$

Oはどれくらいのエラーがあるかの表示ですが，関数として取り込むにはOのところが邪魔です．これはremoveO(まんまやん！)で取り除けます．

In [2]:
fs = series(sin(x),x,a,4)

f0 = fs.removeO()
pprint(f0)
f1 = f0.subs({a:pi})
pprint(f1)

          3                  2
(-a + x) ⋅cos(a)   (-a + x) ⋅sin(a)
- ──────────────── - ──────────────── + (-a + x)⋅cos(a) + sin(a)
6                  2
3
(x - π)
-x + ──────── + π
6

In [4]:
y = symbols('y')
p = plot(sin(x),f1, (x,0,2*pi), ylim=[-1,1], show=False)
p[0].line_color = 'b'
p[1].line_color = 'r'
p.show()


## 全微分(D)¶

sympyでは全微分(total derivatives)は用意されてないようです．

maple
> f:=unapply(x^4*exp(-y^2),(x,y));
> D(f(x,y));
> ([email protected]@2)(f(x,y));
$$f\, := \,( {x,y} )\mapsto {x}^{4}\exp(-{y}^{2}) \notag \\ 4\, {D} \left( x \right) {x}^{3}\exp(-{y}^{2})+{x}^{4} {D} \left( \exp(-{y}^{2}) \right) \notag \\ 4\, \left( D^{ \left( 2 \right) } \right) \left( x \right) {x}^{3}\exp(-{y}^{2})+12\, \left( {D} \left( x \right) \right) ^{2}{x}^{2}\exp(-{y}^{2})+8\, {D} \left( x \right) {x}^{3} {D} \left( \exp(-{y}^{2}) \right) +{x}^{4} \left( D^{ \left( 2 \right) } \right) \left( \exp(-{y}^{2}) \right) \notag$$

ここで，D(x)などはxの全微分を表わす．これは，x,yを変数としているので

maple
> diff(x,x);
> diff(exp(-y^2),y);
$$1 \notag \\ -2\,y\exp(-{y}^{2}) \notag$$

であるがMapleには分からない．そこで全微分の最終形を得るには，あらかじめD(x)などの結果を求めておき，subsで明示的に代入する必要がある．

maple
> dd:=D(f(x,y)):
> eqs:={D(x)=diff(x,x),D(exp(-y^2))=diff(exp(-y^2),y)};
> subs(eqs,dd);
$${\it eqs}\, := \, \left\{ {D} \left( x \right) =1, {D} \left( \exp(-{y}^{2}) \right) =-2\,y\exp(-{y}^{2}) \right\} \notag \\ 4\,{x}^{3}\exp(-{y}^{2})-2\,{x}^{4}y\exp(-{y}^{2}) \notag$$

## 複合関数の微分¶

maple
> diff(f(x)*g(x),x);
> diff(f(g(x)),x);
$$\left( {\frac {d}{dx}}f \left( x \right) \right) g \left( x \right) +f \left( x \right) {\frac {d}{dx}}g \left( x \right) \notag \\ \mbox {D} \left( f \right) \left( g \left( x \right) \right) {\frac {d}{dx}}g \left( x \right) \notag$$
maple
> f:=x->exp(x);
> g:=x->cos(x);
> diff(f(x)*g(x),x);
> diff(f(g(x)),x);
$$f\, := \,x\mapsto \exp(x) \notag \\ g\, := \,x\mapsto \cos \left( x \right) \notag \\ \exp(x)\cos \left( x \right) -\exp(x)\sin \left( x \right) \notag \\ -\sin \left( x \right) \exp(\cos x ) \notag$$
In [9]:
f = exp(x)
g = cos(x)

pprint(diff(f*g, x))
pprint(diff(f.subs({x:g}), x))

   x           x
- ℯ ⋅sin(x) + ℯ ⋅cos(x)
cos(x)
-ℯ      ⋅sin(x)


## 課題¶

1. 次の関数を微分せよ．
1. ${x} \log x$,
2. $\frac{1}{ \left( 1+x \right) ^{3}}$,
3. $\sqrt{4\,x+3}$,
4. $\frac{1}{ a^2+ \left( x-x_0 \right)^2 }$
2. 次の関数の1次から5次導関数を求めよ．
1. $\sin^2 x$,
2. ${e}^{x}$
3. 以下の関数をx0まわりで３次までテイラー展開し，得られた関数ともとの関数をプロットせよ．さらに5次まで展開した場合はどう変化するか．
1. $y=\sin x, x_0=0$,
2. $y=\cos x, x_0=\frac{\pi}{2}$
4. (発展課題）$f \left( x,y \right) ={e}^{x}{\it log} \left( 1+y \right)$を$x=0,\,y=0$のまわりで3次まで展開せよ．

## 解答例¶

### 幾つかの関数の微分¶

maple
> diff(x*log(x),x);
> diff(1/(1 + x)^3,x);
> diff(sqrt(4*x + 3),x);
>diff(1/(a^2+(x-x0)^2),x);
$$\ln \left( x \right) +1 \notag \\ -3\, \left( 1+x \right) ^{-4} \notag \\ 2\, \left( \sqrt{4\,x+3} \right) ^{-1} \notag \\ -{\frac {2\,x-2\,{\it x0}}{ \left( {a}^{2}+ \left( x-{\it x0} \right) ^{2} \right) ^{2}}} \notag$$
In [20]:
from sympy import *
x,a,x0 = symbols('x a x0')

pprint(diff(x*log(x),x))
pprint(diff(1/(1+x)**3,x))
print(diff(sqrt(4*x+3),x))
pprint(diff(1/(a**2+(x-x0)**2),x))

log(x) + 1
-3
────────
4
(x + 1)
2/sqrt(4*x + 3)
-2⋅x + 2⋅x₀
─────────────────
2
⎛ 2           2⎞
⎝a  + (x - x₀) ⎠


### 高階微分¶

maple
> diff(sin(x)^2,x);
> diff(sin(x)^2,x\$2);
$$2\,\sin \left( x \right) \cos \left( x \right) \notag \\ 2\, \left( \cos \left( x \right) \right) ^{2}-2\, \left( \sin \left( x \right) \right) ^{2}\notag$$
In [26]:
from sympy import *
x = symbols('x')
for i in range(0,5):
print(i)
pprint(diff(sin(x)**2,x,i))

0
2
sin (x)
1
2⋅sin(x)⋅cos(x)
2
⎛     2         2   ⎞
2⋅⎝- sin (x) + cos (x)⎠
3
-8⋅sin(x)⋅cos(x)
4
⎛   2         2   ⎞
8⋅⎝sin (x) - cos (x)⎠

In [27]:
from sympy import *
x = symbols('x')
for i in range(0,5):
print(i)
pprint(diff(exp(x),x,i))

0
x
ℯ
1
x
ℯ
2
x
ℯ
3
x
ℯ
4
x
ℯ


テキストを参照して．

### 2次元関数の級数展開と3dplot¶

In [2]:
from sympy import *

x,y = symbols('x y')
f = exp(x)*log(1+y)
fx = f.series(x,0,3).removeO()
fxy = fx.series(y,0,3).removeO()

fxy

Out[2]:
y**2*(-x**2/4 - x/2 - 1/2) + y*(x**2/2 + x + 1)
In [3]:
from sympy.plotting import plot3d

p = plot3d(f, fxy, (x, -0.5, 2), (y, -0.5, 2), show = False)
p[0].surface_color  = 'r'
p[1].surface_color  = 'b'
p.show()


# きれいな微分表示¶

In [102]:
f = x**4*exp(-y**2)
pprint(f)

df = Derivative(f,x)
#df.doit()
df

      2
4  -y
x ⋅ℯ

Out[102]:
$$\frac{\partial}{\partial x}\left(x^{4} e^{- y^{2}}\right)$$
In [ ]: