## 2020年5月23日土曜日

### 数学 - Pytyhon - 解析学 - 多変数の関数 - 合成微分律と勾配ベクトル - 保存律 - ベクトル場、原点からの距離の累乗、ポテンシャル関数、対数関数、勾配ベクトル、偏微分係数

1. 求めるポテンシャル関数の1つは、

$\begin{array}{l}k\ne 2\\ \varphi \left(X\right)\\ =-\frac{1}{\left(k-2\right){r}^{k-2}}\\ k=2\\ \varphi \left(X\right)=\mathrm{log}r\end{array}$

実数に勾配ベクトル、偏微分係数を計算してみる。

$\begin{array}{l}X=\left({x}_{1},\dots ,{x}_{n}\right)\\ k\ne 2\\ \frac{\partial }{\partial {x}_{i}}\varphi \left(X\right)\\ =\frac{1}{k-2}·\frac{\left(k-2\right){r}^{k-3}}{{r}^{2k-4}}·\frac{{x}_{i}}{r}\\ =\frac{{x}_{i}}{{r}^{k}}\\ grad\varphi \left(X\right)=\frac{X}{{r}^{k}}\\ k=1\\ \frac{\partial }{\partial {x}_{i}}\mathrm{log}r\\ =\frac{1}{r}\frac{{x}_{i}}{r}\\ =\frac{{x}_{i}}{{r}^{2}}\\ grad\varphi \left(X\right)=\frac{X}{{r}^{2}}\end{array}$

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, Matrix, log
from sympy.plotting import plot3d

print('3.')

xs = symbols('x:2', real=True)
X = Matrix(xs)
k = symbols('k')
r = X.norm()
f = X / r ** k
phi = -1 / ((k - 2) * r ** (k - 2))
phi2 = log(r)

grad_phi = Matrix([phi.diff(xi, 1) for xi in xs])
grad_phi2 = Matrix([phi2.diff(xi, 1) for xi in xs])

class TestPotential(TestCase):
def test(self):

def test2(self):
self.assertEqual(f.subs({k: 2}).simplify(),

p = plot3d(phi.subs({k: 1}),
show=False)
p.save('sample3_1.png')
p = plot3d(phi2, show=False)
p.save('sample3_2.png')

for k0 in range(3, 6):
p = plot3d(phi.subs({k: k0}),
show=False)
p.save(f'sample3_{k0}.png')
p.show()

if __name__ == "__main__":
main()


% ./sample3.py -v
3.
test (__main__.TestPotential) ... ok
test2 (__main__.TestPotential) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.325s

OK
%