2020年5月18日月曜日

数学 - Pytyhon - 解析学 - 多変数の関数 - 合成微分律と勾配ベクトル - 方向微分係数 - 最大増加の向き、方向微分係数、長さ

1. $\begin{array}{l}gradf\left(x,y,z\right)\\ =grad\left({\left(x+y\right)}^{2}+{\left(y+z\right)}^{2}+{\left(z+x\right)}^{2}\right)\\ =\left(2\left(x+y\right)+2\left(z+x\right),2\left(y+z\right)+2\left(y+x\right),2\left(z+x\right)+2\left(z+y\right)\right)\\ =2\left(2x+y+z,x+2y+z,x+y+2z\right)\\ gradf\left(2,-1,2\right)\\ =2\left(4-1+2,2-2+2,2-1+4\right)\\ =2\left(5,2,5\right)\end{array}$

よって 問題の関数の最大増加の向きは

$\left(5,2,5\right)$

方向微係数について。

$\begin{array}{l}2\left(5,2,5\right)·\frac{\left(5,2,5\right)}{\left|\left(5,2,5\right)\right|}\\ =\frac{2{\left|\left(5,2,5\right)\right|}^{2}}{\left|\left(5,2,5\right)\right|}\\ =2\sqrt{{5}^{2}+{2}^{2}+{5}^{2}}\\ =2\sqrt{54}\end{array}$

コード

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

print('6.')

x, y, z = symbols('x, y, z', real=True)
f = (x + y) ** 2 + (y + z) ** 2 + (z + x) ** 2
gradf = Matrix([f.diff(o, 1) for o in [x, y, z]])
a = {x: 2, y: -1, z: 2}

def test_direction(self):
self.assertEqual(gradfa, 2 * Matrix([5, 2, 5]))

def test_directional_derivative(self):

for o in zip([x, y, z], [2, -1, 2]):
p = plot3d(f.subs(dict([o])),
show=False)
p.save(f'sample6_{o[0]}.png')
p.show()
if __name__ == "__main__":
main()


% ./sample6.py -v
6.