2020年4月3日金曜日

学習環境

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


  1. g r a d f x , y , z = g r a d log z + sin y 2 - x = - cos y 2 - x z + sin y 2 - x , 2 y cos y 2 - x z + sin y 2 - x , 1 z + sin y 2 - x g r a d f P = g r a d f 1 , - 1 , 1 = - 1 , - 2 , 1

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, Matrix, sin, log, Derivative
from sympy.plotting import plot3d, plot3d_parametric_line

print('13.')

x, y, z = symbols('x, y, z', real=True)
f = log(z + sin(y ** 2 - x))
p = {x: 1, y: -1, z: 1}
xyz = Matrix([Derivative(f, o, 1).doit() for o in [x, y, z]])


class TestGrad(TestCase):
    def test(self):
        self.assertEqual(xyz.subs(p), Matrix([-1, -2, 1]))


t = symbols('t')

for i, z0 in enumerate(list(range(-2, 3)), 1):
    d = {z: z0}
    p0 = plot3d(f.subs(d), show=False)
    p0.append(plot3d_parametric_line(1 - t, -1 - 2 * t, 1 + t, show=False)[0])
    p0.save(f'sample13_{i}.png')
p0.show()

if __name__ == "__main__":
    main()

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

% ./sample13.py -v
13.
test (__main__.TestGrad) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.005s

OK
%

0 コメント:

コメントを投稿