## 2020年4月26日日曜日

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

1. $\begin{array}{l}\frac{\partial g}{\partial u}\\ =\frac{\partial f}{\partial u}\\ =\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{\partial g}{\partial v}\\ =\left(\frac{\partial f}{\partial x},\frac{\partial f}{\partial y}\right)·\left(-\mathrm{sin}\theta ,\mathrm{cos}\theta \right)\\ =-\frac{\partial f}{\partial x}\mathrm{sin}\theta +\frac{\partial f}{\partial y}\mathrm{cos}\theta \\ {\left(\frac{\partial g}{\partial u}\right)}^{2}+{\left(\frac{\partial g}{\partial v}\right)}^{2}\\ ={\left(\frac{\partial f}{\partial x}\right)}^{2}{\mathrm{cos}}^{2}\theta +2\frac{\partial f}{\partial x}\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 x}\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
from sympy.plotting import plot3d
import random

print('10.')

x, y, u, v, theta = symbols('x, y, u, v, θ', real=True)
f = 2 * x + y ** 2
xy = {x: u * cos(theta) - v * sin(theta),
y: u * sin(theta) + v * cos(theta)}
g = f.subs(xy)

class TestDerivativeChainRule(TestCase):
def test(self):
left = g.diff(u, 1) ** 2 + g.diff(v, 1) ** 2
right = (f.diff(x, 1) ** 2 + f.diff(y, 1) ** 2).subs(xy)
for _ in range(10):
d = {o: random.randrange(-100, 101) for o in [x, y, u, v, theta]}
self.assertEqual(
float(left.subs(d)),
float(right.subs(d)))

p = plot3d(*[g.subs({theta: o}) for o in range(-5, 5)], show=True)
p.save('sample10.png')

if __name__ == "__main__":
main()

% ./sample10.py -v
10.
test (__main__.TestDerivativeChainRule) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.131s

OK
%