まず、マクローリン展開は以下のような式で表される。
以下では、いくつかの具体例についてPythonのMatlotlibを使って可視化したい。
つまり、
import numpy as np
import matplotlib.pyplot as plt
import math
def maclaurin_expansion_sin(x, n):
y2 = 0
for i in range(0, n+1):
y2 += (np.sin(0+((i*np.pi)/2))/math.factorial(i))*(x**i)
return y2
x = np.linspace(-9, 9, 100)
y = np.sin(x)
plt.plot(x, y, label=r'$\sin x$', linewidth=3)
y2 = maclaurin_expansion_sin(x, 1)
plt.plot(x, y2, label='n=1')
y2 = maclaurin_expansion_sin(x, 3)
plt.plot(x, y2, label='n=3')
y2 = maclaurin_expansion_sin(x, 5)
plt.plot(x, y2, label='n=5')
y2 = maclaurin_expansion_sin(x, 7)
plt.plot(x, y2, label='n=7')
y2 = maclaurin_expansion_sin(x, 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()
import numpy as np
import matplotlib.pyplot as plt
import math
def maclaurin_expansion_sin2(x, n):
y2 = 0
for i in range(0, n):
y2 += ((-1)**i)*(x**(2*i+1)/math.factorial(2*i+1))
return y2
x = np.linspace(-9, 9, 100)
y = np.sin(x)
plt.plot(x, y, label=r'$\sin x$', linewidth=3)
y2 = maclaurin_expansion_sin2(x, 1)
plt.plot(x, y2, label='n=1')
y2 = maclaurin_expansion_sin2(x, 2)
plt.plot(x, y2, label='n=3')
y2 = maclaurin_expansion_sin2(x, 3)
plt.plot(x, y2, label='n=5')
y2 = maclaurin_expansion_sin2(x, 4)
plt.plot(x, y2, label='n=7')
y2 = maclaurin_expansion_sin2(x, 5)
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()
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()