2020年5月16日土曜日

学習環境

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



    1. xX32=xx2+y2+z234gradfX=1x2+y2+z232(x2+y2+z234-x·34x2+y2+z2-14·2x,-x·34x2+y2+z2-14·2y,-x·34x2+y2+z2-14·2z)

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

      ·1632634-32·6-14,32·6-14,-32·6-14·2=1634-32·674,32·674,-3674=34·634,32·674,-3674

    2. X5=x2+y2+z2+w252gradX5=5x2+y2+z2+w232x,y,z,w51+4+1+1321,2,-1,1=5·7321,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 コメント:

コメントを投稿