## 2020年4月20日月曜日

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

1. $\begin{array}{l}\frac{dr}{{\mathrm{dx}}_{i}}\\ =\frac{d}{{\mathrm{dx}}_{i}}{\left(\sum _{k=1}^{n}{x}_{k}^{2}\right)}^{\frac{1}{2}}\\ =\frac{1}{2}{\left(\sum _{k=1}^{n}{x}_{k}^{2}\right)}^{-\frac{1}{2}}2{x}_{i}\\ ={x}_{i}{\left(\sum _{k=1}^{n}{x}_{k}^{2}\right)}^{-\frac{1}{2}}\\ ={x}_{i}{r}^{-1}\end{array}$

コード

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

print('4.')

class TestPartialDerivative(TestCase):
def test_dxi(self):
k = symbols('k', integer=True)
for n in range(2, 11):
r = sum([symbols(f'x{k}') ** 2
for k in range(1, n + 1)]) ** Rational(1, 2)
for i in range(1, n + 1):
xi = symbols(f'x{i}')
self.assertEqual(r.diff(xi, 1), xi / r)

x, y = symbols('x, y')
p = plot3d((x ** 2 + y ** 2) ** Rational(1, 2),
show=True)
p.save('sample4.png')

if __name__ == "__main__":
main()


% ./sample4.py -v
4.
test_dxi (__main__.TestPartialDerivative) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.126s

OK
%