2020年4月23日木曜日

学習環境

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



    1. ∂ z ∂ r = ∂ ∂ r f x , y = ∂ ∂ r f r cos θ , r sin θ = ∂ f ∂ x , ∂ f ∂ y · cos θ , sin θ = ∂ f ∂ x cos θ + ∂ f ∂ y sin θ 1 r ∂ z ∂ θ = 1 r ∂ f ∂ x , ∂ f ∂ y · - r sin θ , r cos θ = - ∂ f ∂ x sin θ + ∂ f ∂ y cos θ

    2. ∂ g ∂ r 2 + 1 r 2 ∂ r ∂ θ 2 = ∂ z ∂ r 2 + 1 r ∂ z ∂ θ 2 = ∂ f ∂ x 2 cos 2 θ + 2 ∂ f ∂ y sin θ cos θ + ∂ f ∂ y 2 sin 2 θ + ∂ f ∂ x 2 sin 2 θ - 2 ∂ f ∂ y sin θ cos θ + ∂ f ∂ y 2 cos 2 θ = ∂ f ∂ x 2 + ∂ f ∂ y 2

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, Function, sin, cos, pprint
from sympy.plotting import plot3d
print('7.')

x, y, r, theta = symbols('x, y, r, θ')
f = Function('f')(x, y)
xy = {x: r * cos(theta), y: r * sin(theta)}
z = f.subs(xy)


class TestDerivativeChainRule(TestCase):
    def test_a1(self):
        self.assertEqual(
            z.diff(r),
            (f.diff(x, 1) * cos(theta) + f.diff(y, 1) * sin(theta)).subs(xy)
        )

    def test_a2(self):
        self.assertEqual(
            (1 / r * z.diff(theta, 1)).simplify(),
            (-f.diff(x, 1).subs(xy) * sin(theta) +
             f.diff(y, 1) * cos(theta)).subs(xy).simplify()
        )


g = 2 * x ** 2 - y ** 3

for i, h in enumerate([g, g.subs(xy)]):
    p = plot3d(h, show=False)
    p.save(f'sample7_{i}.png')
p.show()

if __name__ == "__main__":
    main()

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

% ./sample7.py -v
7.
test_a1 (__main__.TestDerivativeChainRule) ... ok
test_a2 (__main__.TestDerivativeChainRule) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.775s

OK
%

0 コメント:

コメントを投稿