Skip to content

Instantly share code, notes, and snippets.

@L3onSW
Last active February 26, 2024 04:12
Show Gist options
  • Save L3onSW/a18f651030befd3236a959fc6b54e551 to your computer and use it in GitHub Desktop.
Save L3onSW/a18f651030befd3236a959fc6b54e551 to your computer and use it in GitHub Desktop.

マクローリン展開をPythonで可視化する

まず、マクローリン展開は以下のような式で表される。

$$ f(x)=\sum_{n=0}^\infty\cfrac{f^{(n)}(0)}{n!}x^n=f(0)+f^{\prime}(0)x+\cfrac{f^{\prime\prime}(0)}{2!}x^2+\cdots+\cfrac{f^{(n)}(0)}{n!}x^n+\cdots $$

以下では、いくつかの具体例についてPythonのMatlotlibを使って可視化したい。

@L3onSW
Copy link
Author

L3onSW commented Feb 26, 2024

$\sin x$のマクローリン展開 方法3: $\sin x=x-\dfrac{x^3}{3!}+\dfrac{x^5}{5!}-\dfrac{x^7}{7!}+\dfrac{x^9}{9!}-\cdots$を利用

import numpy as np
import matplotlib.pyplot as plt
import math

x = np.linspace(-9, 9, 100)
y = np.sin(x)
plt.plot(x, y, label=r'$\sin x$', linewidth=3)
y2 = x
plt.plot(x, y2, label='n=1')
y2 = y2-x**3/math.factorial(3)
plt.plot(x, y2, label='n=3')
y2 = y2 + x**5/math.factorial(5)
plt.plot(x, y2, label='n=5')
y2 = y2 - x**7/math.factorial(7)
plt.plot(x, y2, label='n=7')
y2 = y2 + x**9/math.factorial(9)
plt.plot(x, y2, label='n=9')
plt.xlim(-8, 8)
plt.ylim(-5, 5)
plt.xticks([-2*np.pi, -np.pi, 0, np.pi, 2*np.pi], ['-2π', '-π', '0', 'π', '2π'])
plt.axhline(0, color='black')
plt.axvline(0, color='black')
plt.xlabel('$x$', fontsize=10)
plt.ylabel('$y$', fontsize=10)
plt.grid(True)
plt.legend(loc='lower right')
plt.show()

@L3onSW
Copy link
Author

L3onSW commented Feb 26, 2024

sin_3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment