学習環境
- Surface
- Windows 10 Pro (OS)
- Nebo(Windows アプリ)
- iPad
- MyScript Nebo - MyScript(iPad アプリ(iOS))
- 参考書籍
解析入門 原書第3版 (S.ラング(著)、松坂 和夫(翻訳)、片山 孝次(翻訳)、岩波書店)の第Ⅵ部(多変数の関数)、第19章(多変数の関数)、2(偏微分)の練習問題12の解答を求めてみる。
コード
#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, Matrix, sin, cos, pi, Derivative, Matrix
from sympy.plotting import plot3d, plot3d_parametric_line
print('12.')
x, y, z = symbols('x, y, z', real=True)
fs = [cos(x * y),
sin(x * y * z),
x ** 2 * sin(y * z)]
xyzs = [[Derivative(f, o, 1).doit() for o in [x, y, z]]
for f in fs]
p = {x: 1, y: pi, z: pi}
grad_fps = [[0, 0, 0],
[pi ** 2 * cos(pi ** 2),
pi * cos(pi ** 2),
pi * cos(pi ** 2)],
[2 * sin(pi ** 2), pi * cos(pi ** 2), pi * cos(pi ** 2)]]
class TestGrad(TestCase):
def test(self):
for xyz, grad_fp in zip(xyzs, grad_fps):
self.assertEqual(Matrix(xyz).subs(p), Matrix(grad_fp))
gs = [sin(x * y), x ** sin(y)]
p0 = plot3d(cos(x * y), show=False)
p0.save('sample12.png')
t = symbols('t')
for i, g in enumerate(gs):
p0 = plot3d(g, show=False)
x0, y0, z0 = grad_fps[i + 1]
p0.append(plot3d_parametric_line(
1 + t * x0, pi + t * y0, pi + t * z0, show=False)[0])
p0.save(f'sample12_{i}.png')
p0.show()
if __name__ == "__main__":
main()
入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))
% ./sample12.py -v
12.
test (__main__.TestGrad) ... ok
----------------------------------------------------------------------
Ran 1 test in 0.008s
OK
%
0 コメント:
コメントを投稿