2020年3月31日火曜日

学習環境

解析入門 原書第3版 (S.ラング(著)、松坂 和夫(翻訳)、片山 孝次(翻訳)、岩波書店)の第Ⅵ部(多変数の関数)、第19章(多変数の関数)、2(偏微分)の練習問題1、2、3、4、5、6、7、8、9、10の解答を求めてみる。


  1. f x = x x y + z = y y x y + z = x z x y + z = 1

  2. x x 2 y 5 + 1 = 2 x y 5 f y = 5 x 2 y 4 f z = 0

  3. x sin x y + cos z = cos x y y f y = cos x y x f z = - sin z

  4. x cos x y = - sin x y y f y = - sin x y x f z = 0

  5. x sin x y z = cos x y z y z f y = cos x y z x z f z = cos x y z x y

  6. x e x y z = e x y z y z f y = e x y z x z f z = e x y z x y

  7. x x 2 sin y z = 2 x sin y z f y = x 2 cos y z z f z = x 2 cos y z y

  8. x x y z = y z f y = x z f z = x y

  9. x x z + y z + x y = z + y f y = z + x f z = x + y

  10. x x cos y - 3 z + arcsin x y = cos y - 3 z + y 1 - x 2 y 2 f y = x - sin y - 3 z + x 1 - x 2 y 2 f z = x - sin y - 3 z - 3

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, plot, sin, cos, exp, asin, Derivative, sqrt
from sympy.plotting import plot3d

print('1, 2, 3, 4, 5, 6, 7, 8, 9, 10.')

x, y, z = symbols('x, y, z', real=True)
fs = [x * y + z,
      x ** 2 * y ** 5 + 1,
      sin(x * y) + cos(z),
      cos(x * y),
      sin(x * y * z),
      exp(x * y * z),
      x ** 2 * sin(y * z),
      x * y * z,
      x * z + y * z + x * y,
      x * cos(y - 3 * z) + asin(x * y)]

fxyzs = [(y, x, 1),
         (2 * x * y ** 5, 5 * x ** 2 * y ** 4, 0),
         (cos(x * y) * y, cos(x * y) * x, -sin(z)),
         (-sin(x * y) * y, -sin(x * y) * x, 0),
         (cos(x * y * z) * y * z,
          cos(x * y * z) * x * z,
          cos(x * y * z) * x * y),
         (exp(x * y * z) * y * z,
          exp(x * y * z) * x * z,
          exp(x * y * z) * x * y),
         (2 * x * sin(y * z),
          x ** 2 * cos(y * z) * z,
          x ** 2 * cos(y * z) * y),
         (y * z, x * z, x * y),
         (z + y, z + x, x + y),
         (cos(y - 3 * z) + y / sqrt(1 - x ** 2 * y ** 2),
          x * (- sin(y - 3 * z)) + x / sqrt(1 - x ** 2 * y ** 2),
          x * (-sin(y - 3 * z)) * (-3))]


class TestPartialDerivative(TestCase):
    def test(self):
        for f, fxyz in zip(fs, fxyzs):
            for fo, o in zip(fxyz, [x, y, z]):
                self.assertEqual(
                    Derivative(f, o, 1).doit(),
                    fo)


for i, f in enumerate(fs, 1):
    p = plot3d(f.subs({z: 1}),
               show=False)
    p.save(f'sample{i}.png')

p.show()


if __name__ == "__main__":
    main()

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

% ./sample1.py -v
1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
test (__main__.TestPartialDerivative) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.034s

OK
%

0 コメント:

コメントを投稿