2020年4月8日水曜日

学習環境

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


  1. x f x , y = x 2 x - 3 y = 2 y 2 x - 3 y = - 3

コード

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


print('1.')

x, y = symbols('x, y')
f = 2 * x - 3 * y
fx = 2
fy = -3


class TestPartialDerivative(TestCase):
    def test_dx(self):
        self.assertEqual(Derivative(f, x, 1).doit(), fx)

    def test_dy(self):
        self.assertEqual(Derivative(f, y, 1).doit(), fy)


p = plot3d(f, show=False)
t = symbols('t')

for _ in range(10):
    px = random.randrange(-5, 6)
    py = random.randrange(-5, 6)
    x0 = px + t * fx
    y0 = px + t * fy
    p.append(plot3d_parametric_line(x0, y0, f.subs({x: x0, y: y0}),
                                    legend=True,
                                    show=False)[0])
colors = ['red', 'green', 'blue', 'brown', 'orange',
          'purple', 'pink', 'gray', 'skyblue', 'yellow']

for o, color in zip(p, colors):
    o.line_color = color

p.show()
p.save('sample1.png')

if __name__ == "__main__":
    main()

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

% ./sample1.py -v
1.
test_dx (__main__.TestPartialDerivative) ... ok
test_dy (__main__.TestPartialDerivative) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.002s

OK
%

0 コメント:

コメントを投稿