## 2020年4月19日日曜日

### 数学 - Python - 解析学 - 多変数の関数 - 合成微分律と勾配ベクトル - 偏微分、偏導関数

• $\begin{array}{l}\frac{\partial f}{\partial x}\\ =\frac{\partial }{\partial x}{\left({x}^{2}+{y}^{2}+{z}^{2}\right)}^{\frac{1}{2}}\\ =\frac{1}{2}{\left({x}^{2}+{y}^{2}+{z}^{2}\right)}^{-\frac{1}{2}}·2x\\ =x{\left({x}^{2}+{y}^{2}+{z}^{2}\right)}^{-\frac{1}{2}}\end{array}$

• $\frac{\partial f}{\partial y}=y{\left({x}^{2}+{y}^{2}+{z}^{2}\right)}^{-\frac{1}{2}}$

コード

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

print('3.')

x, y, z = symbols('x, y, z')
f = (x ** 2 + y ** 2 + z ** 2) ** Rational(1, 2)

class TestPartialDerivative(TestCase):
def test_dx(self):
self.assertEqual(f.diff(x, 1),
x * (x ** 2 + y ** 2 + z ** 2) ** -Rational(1, 2))

def test_dy(self):
self.assertEqual(f.diff(y, 1),
y * (x ** 2 + y ** 2 + z ** 2) ** -Rational(1, 2))

p = plot3d(f.subs({z: 1}), show=True)
p.xlabel = x
p.ylabel = y
p.save('sample3.png')

if __name__ == "__main__":
main()


% ./sample3.py -v
3.
test_dx (__main__.TestPartialDerivative) ... ok
test_dy (__main__.TestPartialDerivative) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.014s

OK
%