## 2020年4月23日木曜日

### 数学 - Python - 解析学 - 多変数の関数 - 合成微分律と勾配ベクトル - 三角関数(正弦と余弦)、変数、記法、内積、等式

1. $\begin{array}{l}\frac{\partial z}{\partial r}\\ =\frac{\partial }{\partial r}f\left(x,y\right)\\ =\frac{\partial }{\partial r}f\left(r\mathrm{cos}\theta ,r\mathrm{sin}\theta \right)\\ =\left(\frac{\partial f}{\partial x},\frac{\partial f}{\partial y}\right)·\left(\mathrm{cos}\theta ,\mathrm{sin}\theta \right)\\ =\frac{\partial f}{\partial x}\mathrm{cos}\theta +\frac{\partial f}{\partial y}\mathrm{sin}\theta \\ \frac{1}{r}\frac{\partial z}{\partial \theta }\\ =\frac{1}{r}\left(\frac{\partial f}{\partial x},\frac{\partial f}{\partial y}\right)·\left(-r\mathrm{sin}\theta ,r\mathrm{cos}\theta \right)\\ =-\frac{\partial f}{\partial x}\mathrm{sin}\theta +\frac{\partial f}{\partial y}\mathrm{cos}\theta \end{array}$

2. $\begin{array}{l}{\left(\frac{\partial g}{\partial r}\right)}^{2}+\frac{1}{{r}^{2}}{\left(\frac{\partial r}{\partial \theta }\right)}^{2}\\ ={\left(\frac{\partial z}{\partial r}\right)}^{2}+{\left(\frac{1}{r}\frac{\partial z}{\partial \theta }\right)}^{2}\\ ={\left(\frac{\partial f}{\partial x}\right)}^{2}{\mathrm{cos}}^{2}\theta +2\frac{\partial f}{\partial y}\mathrm{sin}\theta \mathrm{cos}\theta +{\left(\frac{\partial f}{\partial y}\right)}^{2}{\mathrm{sin}}^{2}\theta \\ +{\left(\frac{\partial f}{\partial x}\right)}^{2}{\mathrm{sin}}^{2}\theta -2\frac{\partial f}{\partial y}\mathrm{sin}\theta \mathrm{cos}\theta +{\left(\frac{\partial f}{\partial y}\right)}^{2}{\mathrm{cos}}^{2}\theta \\ ={\left(\frac{\partial f}{\partial x}\right)}^{2}+{\left(\frac{\partial f}{\partial y}\right)}^{2}\end{array}$

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, Function, sin, cos, pprint
from sympy.plotting import plot3d
print('7.')

x, y, r, theta = symbols('x, y, r, θ')
f = Function('f')(x, y)
xy = {x: r * cos(theta), y: r * sin(theta)}
z = f.subs(xy)

class TestDerivativeChainRule(TestCase):
def test_a1(self):
self.assertEqual(
z.diff(r),
(f.diff(x, 1) * cos(theta) + f.diff(y, 1) * sin(theta)).subs(xy)
)

def test_a2(self):
self.assertEqual(
(1 / r * z.diff(theta, 1)).simplify(),
(-f.diff(x, 1).subs(xy) * sin(theta) +
f.diff(y, 1) * cos(theta)).subs(xy).simplify()
)

g = 2 * x ** 2 - y ** 3

for i, h in enumerate([g, g.subs(xy)]):
p = plot3d(h, show=False)
p.save(f'sample7_{i}.png')
p.show()

if __name__ == "__main__":
main()


% ./sample7.py -v
7.
test_a1 (__main__.TestDerivativeChainRule) ... ok
test_a2 (__main__.TestDerivativeChainRule) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.775s

OK
%