2020年5月16日土曜日

学習環境

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



    1. x X 3 2 = x x 2 + y 2 + z 2 3 4 g r a d f X = 1 x 2 + y 2 + z 2 3 2 ( x 2 + y 2 + z 2 3 4 - x · 3 4 x 2 + y 2 + z 2 - 1 4 · 2 x , - x · 3 4 x 2 + y 2 + z 2 - 1 4 · 2 y , - x · 3 4 x 2 + y 2 + z 2 - 1 4 · 2 z )

      よって、問題 の X の関数が与えられた点(1,-1,2)で最も急速に増加する向きは、

      · 1 6 3 2 6 3 4 - 3 2 · 6 - 1 4 , 3 2 · 6 - 1 4 , - 3 2 · 6 - 1 4 · 2 = 1 6 3 4 - 3 2 · 6 7 4 , 3 2 · 6 7 4 , - 3 6 7 4 = 3 4 · 6 3 4 , 3 2 · 6 7 4 , - 3 6 7 4

    2. X 5 = x 2 + y 2 + z 2 + w 2 5 2 g r a d X 5 = 5 x 2 + y 2 + z 2 + w 2 3 2 x , y , z , w 5 1 + 4 + 1 + 1 3 2 1 , 2 , - 1 , 1 = 5 · 7 3 2 1 , 2 , - 1 , 1

コード

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

print('4.')

x, y, z, w = symbols('x, y, z, w', real=True)


class TestGrad(TestCase):
    def test_a(self):
        f = x / Matrix([x, y, z]).norm() ** Rational(3, 2)

        self.assertEqual(
            Matrix([f.diff(o, 1) for o in [x, y, z]]).subs(
                {x: 1, y: -1, z: 2}),
            3 * Matrix([1 / (4 * 6 ** Rational(3, 4)),
                        1 / (2 * 6 ** Rational(7, 4)),
                        -1 / 6 ** Rational(7, 4)])
        )

    def test_b(self):
        f = Matrix([x, y, z, w]).norm() ** 5
        self.assertEqual(
            Matrix([f.diff(o, 1) for o in [x, y, z, w]]).subs(
                {x: 1, y: 2, z: -1, w: 1}),
            5 * 7 ** Rational(3, 2) * Matrix([1, 2, -1, 1])
        )


for i, f in enumerate([x / Matrix([x, y, 2]).norm() ** Rational(3, 2),
                       Matrix([x, y, -1, 1]).norm() ** 5]):
    c = chr(ord("a") + i)
    p = plot3d(f,
               (x, -5, 5),
               (y, -5, 5),
               show=False)
    p.save(f'sample4_{c}.png')
p.show()


if __name__ == "__main__":
    main()

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

% ./sample4.py -v
4.
test_a (__main__.TestGrad) ... ok
test_b (__main__.TestGrad) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.043s

OK
%

0 コメント:

コメントを投稿