![見出し画像](https://assets.st-note.com/production/uploads/images/88133490/rectangle_large_type_2_bb25ad1342ce22a695cd0eeee826a131.png?width=1200)
pythonのmatplotlibでsin θとcos θの半角定理を確認する
Pythonで$${\sin^2\theta}$$と$${\cos^2\theta}$$のグラフを描きます。
import numpy as np
import matplotlib.pyplot as plt
N = 100
theta = np.linspace(-2*np.pi, 2*np.pi, N)
sin_2 = np.sin(theta)**2
cos_2 = np.cos(theta)**2
add_ = sin_2 + cos_2
fig ,ax = plt.subplots()
plt.rcParams["figure.figsize"] = (6, 4)
ax.set_title( r'$\sin^2\theta$と$\cos^2 \theta$の関係', loc = 'center', pad=30, fontname="MS Gothic", fontsize = 24)
ax.plot(theta, sin_2, color='r', linewidth=1, label= r'$\sin^2 \theta$')
ax.plot(theta, cos_2, color='b', linewidth=1, label= r'$\cos^2 \theta$')
ax.plot(theta, add_, color='g', linewidth=1,label= r'$\sin^2 \theta + \cos^2 \theta$')
ax.set_xticks([-2*np.pi,-1.5*np.pi, -np.pi,-0.5*np.pi,0,0.5*np.pi, np.pi,1.5*np.pi, 2*np.pi])
ax.set_xticklabels( [ '-2π',r'$-\frac{3}{2} \pi$' ,'-π', r'$-\frac{π}{2} \pi$','',
r'$\frac{π}{2} \pi$','π', r'$\frac{3}{2} \pi$', '2π'])
ax.set_xlabel(r'$\theta$', loc = 'right', labelpad=-30)
ax.set_yticks([-1, -1/2**0.5, -0.5, 0,0.5, 1/2**0.5,1])
ax.set_yticklabels( [ '-1', r'$-\frac{1}{\sqrt{2}}$',r'$-\frac{1}{2}$', '0',r'$\frac{1}{2}$',r'$\frac{1}{\sqrt{2}}$','1'])
ax.spines['left'].set_position('zero')
ax.spines['bottom'].set_position(('data', 0))
ax.spines["right"].set_color("none")
ax.spines["top"].set_color("none")
ax.legend( bbox_to_anchor=(1, 0.2))
# ax.set_aspect(2, adjustable='box')
ax.grid(which = "major", axis = "x", color = "green", alpha = 0.8,
linestyle = "--", linewidth = 0.5)
ax.grid(which = "major", axis = "y", color = "green", alpha = 0.8,
linestyle = "--", linewidth = 0.5)
plt.show()
![画像1](https://assets.st-note.com/production/uploads/images/88133322/picture_pc_f42d146447d8ac962f5ae6b2de08fc3e.png)
いずれも、2乗するので当然$${\geqq0}$$になりますが、きれいに上下で対称になります。$${\sin^2\theta+\cos^2\theta=1}$$というのは三角関数の最も基本的な定理ですが、改めて目で見るととても不思議です。
Pythonで$${\sin^2\theta}$$と$${\cos^2\theta}$$のグラフを描きます。
import numpy as np
import matplotlib.pyplot as plt
N = 100
theta = np.linspace(-2*np.pi, 2*np.pi, N)
sin_h2 = np.sin(theta/2)**2
sin_f2 = (1 - np.cos(theta))/2
cos_h2 = np.cos(theta/2)**2
cos_f2 = (1 + np.cos(theta))/2
fig ,ax = plt.subplots()
plt.rcParams["figure.figsize"] = (6, 4)
ax.set_title( r'$\sin^2\theta$と$\cos^2 \theta$の半角定理', loc = 'center', pad=30, fontname="MS Gothic", fontsize = 24)
ax.plot(theta, sin_h2, color='r', linewidth=1, label= r'$\sin^2 (\theta/2)$')
ax.plot(theta, sin_f2, color='r', linewidth=8, alpha = 0.2, label= r'$(1-\cos\theta)/2$')
ax.plot(theta, cos_h2, color='b', linewidth=1, label= r'$\cos^2 (\theta/2)$')
ax.plot(theta, cos_f2, color='b', linewidth=8, alpha = 0.2, label= r'$(1+\cos\theta)/2$')
ax.set_xticks([-2*np.pi,-1.5*np.pi, -np.pi,-0.5*np.pi,0,0.5*np.pi, np.pi,1.5*np.pi, 2*np.pi])
ax.set_xticklabels( [ '-2π',r'$-\frac{3}{2} \pi$' ,'-π', r'$-\frac{π}{2} \pi$','',
r'$\frac{π}{2} \pi$','π', r'$\frac{3}{2} \pi$', '2π'])
ax.set_xlabel(r'$\theta$', loc = 'right', labelpad=-30)
ax.set_yticks([-1, -1/2**0.5, -0.5, 0,0.5, 1/2**0.5,1])
ax.set_yticklabels( [ '-1', r'$-\frac{1}{\sqrt{2}}$',r'$-\frac{1}{2}$', '0',r'$\frac{1}{2}$',r'$\frac{1}{\sqrt{2}}$','1'])
ax.spines['left'].set_position('zero')
ax.spines['bottom'].set_position(('data', 0))
ax.spines["right"].set_color("none")
ax.spines["top"].set_color("none")
ax.legend( bbox_to_anchor=(1, 0.2))
# ax.set_aspect(2, adjustable='box')
ax.grid(which = "major", axis = "x", color = "green", alpha = 0.8,
linestyle = "--", linewidth = 0.5)
ax.grid(which = "major", axis = "y", color = "green", alpha = 0.8,
linestyle = "--", linewidth = 0.5)
plt.show()
![画像2](https://assets.st-note.com/production/uploads/images/88133406/picture_pc_9f2a5917bfac6288590f50a139bd07da.png)
$${\sin^2\theta}$$と$${\cos^2\theta}$$を使い、半角定理の計算をすることができます。
$${\sin^2 (\theta/2)=(1-\cos\theta)/2}$$
$${\cos^2(\theta/2)=(1+\cos\theta)/2}$$
という2乗の三角関数を1乗の式に変換することができます。グラフを作成すると定理通りぴったりと重なります。
この記事が気に入ったらサポートをしてみませんか?