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

$e^x$のマクローリン展開 方法1: $e^x={\displaystyle\sum_{n=0}^{\infty}}\dfrac{x^{n}}{n!}$を利用

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


def maclaurin_expansion_exp(x, n):
    y2 = 0
    for i in range(0, n):
        y2 += (x**i/math.factorial(i))
    return y2


x = np.linspace(-9, 9, 100)
y = np.exp(x)
plt.plot(x, y, label=r'$e^x$', linewidth=3)
y2 = maclaurin_expansion_exp(x, 1)
plt.plot(x, y2, label='n=0')
y2 = maclaurin_expansion_exp(x, 2)
plt.plot(x, y2, label='n=1')
y2 = maclaurin_expansion_exp(x, 3)
plt.plot(x, y2, label='n=2')
y2 = maclaurin_expansion_exp(x, 4)
plt.plot(x, y2, label='n=3')
y2 = maclaurin_expansion_exp(x, 5)
plt.plot(x, y2, label='n=4')
plt.xlim(-8, 8)
plt.ylim(-5, 5)
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

exp_1

@L3onSW
Copy link
Author

L3onSW commented Feb 26, 2024

$e^x$のマクローリン展開 方法2: $e^x=1+x+\dfrac{x^2}{2!}+\dfrac{x^3}{3!}+\dfrac{x^4}{4!}+\cdots$を利用

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

x = np.linspace(-9, 9, 100)
y = np.exp(x)
plt.plot(x, y, label=r'$e^x$', linewidth=3)
y2 = np.linspace(1, 1, 100)
plt.plot(x, y2, label='n=0')
y2 = y2 + x
plt.plot(x, y2, label='n=1')
y2 = y2 + (x**2/math.factorial(2))
plt.plot(x, y2, label='n=2')
y2 = y2 + (x**3/math.factorial(3))
plt.plot(x, y2, label='n=3')
y2 = y2 + (x**4/math.factorial(4))
plt.plot(x, y2, label='n=4')
plt.xlim(-8, 8)
plt.ylim(-5, 5)
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

exp_2

@L3onSW
Copy link
Author

L3onSW commented Feb 26, 2024

$\sin x$のマクローリン展開 方法1: $f(x)={\displaystyle\sum_{n=0}^{\infty}}\dfrac{f^{(n)}(0)}{n!}x^n$を利用

$\sin x$及び $\cos x$は微分すると位相が $\frac{\pi}{2}$進む。
つまり、 $(\sin x)^{\prime}=\cos x=\sin (x+\frac{\pi}{2})$であるから、
$\sin x$の微分は、 $\sin (x+\frac{\pi}{2})$となり、
$\sin x$$n$回微分は、 $\sin (x+\frac{n}{2}\pi)$と求められる。

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()

@L3onSW
Copy link
Author

L3onSW commented Feb 26, 2024

sin_1

@L3onSW
Copy link
Author

L3onSW commented Feb 26, 2024

$\sin x$のマクローリン展開 方法2: $\sin x={\displaystyle\sum_{n=0}^{\infty}}(-1)^n\dfrac{x^{2n+1}}{(2n+1)!}$を利用

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()

@L3onSW
Copy link
Author

L3onSW commented Feb 26, 2024

sin_2

@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