## 2018年1月18日木曜日

### 数学 - Python - 解析学 - 多変数の関数 - 微分可能性と勾配ベクトル(方向微分係数、向きベクトル、単位ベクトル、グラディエント(gradient)、内積(ドット積、スカラー積))

1. $\begin{array}{}grad\left(f\left(x,y\right)\right)=\left(2xy,{x}^{2}\right)\\ grad\left(f\left(2,-1\right)\right)·\frac{\left(1,1\right)}{\sqrt{1+1}}\\ =\left(-4,4\right)·\left(1,1\right)·\frac{1}{\sqrt{2}}\\ =\frac{-4+4}{\sqrt{2}}\\ =0\end{array}$

2. $\begin{array}{}\frac{\partial f}{\partial x}\\ =\frac{\frac{1}{2}{\left({x}^{2}+{y}^{2}+{z}^{2}\right)}^{-\frac{1}{2}}}{\sqrt{{x}^{2}+{y}^{2}+{z}^{2}}}·2x\\ =\frac{x}{{x}^{2}+{y}^{2}+{z}^{2}}\end{array}$
$\begin{array}{}grad\left(f\left(x,y,z\right)\right)\\ =\frac{1}{{x}^{2}+{y}^{2}+{z}^{2}}\left(x,y,z\right)\end{array}$
$\begin{array}{}\frac{1}{1+1+4}\left(-1,1,2\right)·\left(1,2,2\right)·\frac{1}{\sqrt{1+4+4}}\\ =\frac{1}{6}\left(-1+2+4\right)·\frac{1}{3}\\ =\frac{5}{18}\end{array}$

3. $\begin{array}{}\frac{\partial f}{\partial x}\\ =2\left(x+y\right)+2\left(z+x\right)\\ =2\left(2x+y+z\right)\\ grad\left(f\left(x,y,z\right)\right)\\ =2\left(2x+y+z,x+2y+z,x+y+2z\right)\end{array}$
$\begin{array}{}grad\left(f\left(2,2,-1\right)\right)=2\left(5,5,2\right)\\ 2\left(5,5,2\right)·\left(5,5,2\right)\frac{1}{\left|\left(5,5,2\right)\right|}\\ =2\sqrt{25+25+4}\\ =2\sqrt{54}\\ =6\sqrt{6}\end{array}$

コード(Emacs)

Python 3

#!/usr/bin/env python3
from sympy import pprint, symbols, log, sqrt, Matrix, Derivative

x, y, z = symbols('x, y, z')
xs = [x, y, z]
f = x ** 2 * y
g = log(sqrt(x ** 2 + y ** 2 + z ** 2))
h = (x + y) ** 2 + (y + z) ** 2 + (z + x) ** 2

fs = [(f, 2), (g, 3), (h, 3)]
gradfs = [Matrix([Derivative(f0, xi, 1).doit() for xi in xs][:i])
for f0, i in fs]
points = [((2, -1), (1, 1)),
((-1, 1, 2), (1, 2, 2)),
((2, 2, - 1), (5, 5, 2))]

for i, ((f, _), gradf, (p1, p2)) in enumerate(zip(fs, gradfs, points)):
print(f'({chr(ord("a") + i)})')
p1 = Matrix(p1)
p2 = Matrix(p2) / Matrix(p2).norm()
for t in [f, gradf.T, p1.T, p2.T, gradf.subs(dict(zip(xs, p1))).dot(p2)]:
pprint(t)
print()
print()


$./sample11.py (a) 2 x ⋅y ⎡ 2⎤ ⎣2⋅x⋅y x ⎦ [2 -1] ⎡√2 √2⎤ ⎢── ──⎥ ⎣2 2 ⎦ 0 (b) ⎛ ______________⎞ ⎜ ╱ 2 2 2 ⎟ log⎝╲╱ x + y + z ⎠ ⎡ x y z ⎤ ⎢──────────── ──────────── ────────────⎥ ⎢ 2 2 2 2 2 2 2 2 2⎥ ⎣x + y + z x + y + z x + y + z ⎦ [-1 1 2] [1/3 2/3 2/3] 5/18 (c) 2 2 2 (x + y) + (x + z) + (y + z) [4⋅x + 2⋅y + 2⋅z 2⋅x + 4⋅y + 2⋅z 2⋅x + 2⋅y + 4⋅z] [2 2 -1] ⎡5⋅√6 5⋅√6 √6⎤ ⎢──── ──── ──⎥ ⎣ 18 18 9 ⎦ 6⋅√6$