2019年3月11日月曜日

学習環境

解析入門 原書第3版 (S.ラング(著)、松坂 和夫(翻訳)、片山 孝次(翻訳)、岩波書店)の第4部(級数)、第14章(テイラーの公式)、3(三角関数)の練習問題1の解答を求めてみる。


  1. cos 0 + - sin 0 x + - cos 0 2 ! x 2 + sin 0 3 ! x 3 + cos 0 4 ! x 4 = 1 - 1 2 ! x 2 + 1 4 ! x 4

コード

Python 3

#!/usr/bin/env python3
from sympy import pprint, symbols, cos, factorial, Derivative, plot

x = symbols('x')
f = cos(x)
g = sum([Derivative(f, x, n).subs({x: 0}) * x ** n / factorial(n)
         for n in range(5)])

for o in [f, g, g.doit()]:
    pprint(o)
    print()
p = plot(f, g.doit(), ylim=(-5, 5), legend=True, show=False)
colors = ['red', 'green', 'blue', 'brown', 'orange', 'purple']
for s, color in zip(p, colors):
    s.line_color = color

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

入出力結果(cmd(コマンドプロンプト)、Terminal、Jupyter(IPython))

C:\Users\...>py -3 sample1.py
cos(x)

   ⎛  4        ⎞│         ⎛  3        ⎞│         ⎛  2        ⎞│               
 4 ⎜ d         ⎟│       3 ⎜ d         ⎟│       2 ⎜ d         ⎟│               
x ⋅⎜───(cos(x))⎟│      x ⋅⎜───(cos(x))⎟│      x ⋅⎜───(cos(x))⎟│               
   ⎜  4        ⎟│         ⎜  3        ⎟│         ⎜  2        ⎟│               
   ⎝dx         ⎠│x=0      ⎝dx         ⎠│x=0      ⎝dx         ⎠│x=0     ⎛d     
──────────────────── + ──────────────────── + ──────────────────── + x⋅⎜──(cos
         24                     6                      2               ⎝dx    

             
             
             
             
    ⎞│       
(x))⎟│    + 1
    ⎠│x=0    

 4    2    
x    x     
── - ── + 1
24   2     


C:\Users\...>

0 コメント:

コメントを投稿