2020年5月18日月曜日

学習環境

解析入門 原書第3版 (S.ラング(著)、松坂 和夫(翻訳)、片山 孝次(翻訳)、岩波書店)の第Ⅵ部(多変数の関数)、第20章(合成微分律と勾配ベクトル)、3(方向微分係数)の練習問題6の解答を求めてみる。


  1. g r a d f x , y , z = g r a d x + y 2 + y + z 2 + z + x 2 = 2 x + y + 2 z + x , 2 y + z + 2 y + x , 2 z + x + 2 z + y = 2 2 x + y + z , x + 2 y + z , x + y + 2 z g r a d f 2 , - 1 , 2 = 2 4 - 1 + 2 , 2 - 2 + 2 , 2 - 1 + 4 = 2 5 , 2 , 5

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

    5 , 2 , 5

    方向微係数について。

    2 5 , 2 , 5 · 5 , 2 , 5 5 , 2 , 5 = 2 5 , 2 , 5 2 5 , 2 , 5 = 2 5 2 + 2 2 + 5 2 = 2 54

コード

#!/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}
gradfa = gradf.subs(a)


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

    def test_directional_derivative(self):
        self.assertEqual(gradfa.norm(), 2 * sqrt(54))


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()

入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))

% ./sample6.py -v
6.
test_direction (__main__.TestGrad) ... ok
test_directional_derivative (__main__.TestGrad) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.003s

OK
%

0 コメント:

コメントを投稿