기본 콘텐츠로 건너뛰기

[matplotlib] 등고선(Contour)

삼각함수의 미분

내용

삼각함수(trigonometric functions)의 미분

sin, cos, tan의 미분

일반적으로 각을 표시하기 위해 그리스 문자 θ으로 사용합니다. 다음 함수를 고려해 봅니다.

y = sin(θ)

이 함수에서 조사할 것은 그 함수의 변화량 (미분 계수) $\frac{d(\sin(\theta))}{d\theta}$ 입니다. 즉, 각도 θ와 sin(θ)의 변화 사이의 관계를 찾는 것입니다. 특히 증가가 무한히 작을 경우가 주된 관심 사항입니다. 이러한 관계를 그림 1에 나타내었습니다.

import numpy as np
import pandas as pd
from sympy import *
import matplotlib.pyplot as plt
plt.figure(figsize=(5, 5))
a=plt.axes(xlim=(-1.5, 2.5), ylim=(-1.5, 2.5))
r=plt.Circle((0, 0), 1, facecolor="none", edgecolor="navy", linewidth=2, label="radius=r")
a.add_patch(r)
plt.arrow(0, 0, 0.8, np.sqrt(1-(-0.8)**2), color="green", label=r"$\mathbf{degree=\theta}}$")
plt.arrow(0, 0, 0.7, np.sqrt(1-(-0.7)**2), color="red", label=r"$\mathbf{degree=\theta+\Delta\theta}}$")
plt.hlines(0, -1.5, 1.5, color="black")
plt.vlines(0, -1.5, 1.5, color="black")
plt.vlines(0.7, 0,np.sqrt(1-(-0.7)**2), linestyle="--", color="red", label=r"$\mathbf{y+\Delta y}}$")
plt.vlines(0.8,0, np.sqrt(1-(-0.8)**2), linestyle="--", color="green", label=r"$\mathbf{y}}$")
plt.legend(loc="best", prop={"weight":"bold"})
plt.text(0.5, -0.2, "X", size=12, weight="bold")
plt.text(-0.2, 0.5,"Y", size=12, weight="bold")
plt.xticks([]) 
plt.yticks([])
plt.show()
그림 1. 원에서 각도의 변화.

그림 1은 반지름 r의 원에서 y는 높이, θ 각을 나타낼 수 있으며 r = 1인 경우 높이와 각 사이 y= sin(θ) 관계가 성립합니다. θ 변화에 따른 sin(θ) 즉, 높이의 변화를 dy(Δ y)라 할 수 있습니다. 새로운 높이는 y+dy가 되고 각은 θ+dθ 될 것입니다.

$$\begin{align} y+dy &= \sin(\theta+d\theta)\\ \rightarrow dy &= \sin(\theta+d\theta) - \sin(\theta) \end{align}$$

위 식의 우항은 삼각함수의 합법칙을 적용하여 식 1과 같이 전개할 수 있습니다.

$$\begin{align}\tag{1} &\sin(m)-\sin(n) = 2\cos\left(\frac{m+n}{2}\right)⋅\sin \left(\frac{m-n}{2}\right)\\ &\rightarrow  m = \theta+d\theta, n = \theta\\ &\begin{aligned}dy &= 2\cos\left(\frac{\theta+d\theta+\theta}{2}\right)⋅\sin\left(\frac{\theta+d\theta-\theta}{2}\right)\\ &= 2\cos \left(\theta+\frac{1}{2}d\theta \right)⋅\sin\left(\frac{1}{2}d\theta \right)\end{aligned} \end{align}$$

$\theta$ 무한히 작다면 다음 코드의 결과와 같이 그 값의 크기는 무시 가능하므로 위 식에서 $\theta+\frac{1}{2}d\theta \approx \theta$로 간주할 수 있으며 또한 각($\theta$)과 $\sin(\theta)$ 값이 거의 일치하기 때문에 $\sin \left(\frac{1}{2}\theta \right) \approx \frac{1}{2}\theta$이 성립합니다.

th=[0.001,0.0001, 0.00001, 0.000001]
rad=np.deg2rad(th)
np.around(rad, 8)
array([1.745e-05, 1.750e-06, 1.700e-07, 2.000e-08])
val=[N(sin(i), 8) for i in rad]
pd.DataFrame([rad, val], index=["radian", "sin(x)"])
0 1 2 3
radian 0.000017 0.000002 0.0 0.0
sin(x) 1.74e-5 1.74e-6 1.74e-7 1.74e-8

그러므로 위 식은 다음과 같이 정리 됩니다.

$$\begin{aligned} dy &= \frac{2\cos(\theta)}{2}d\theta\\ \frac{dy}{d\theta}&= \cos(\theta) \end{aligned}$$

위 함수 $y=\sin(\theta)$와 미분 함수인 $\cos(\theta)$는 각각 그림 2와 같습니다.

그림 2. sin(θ)와 미분 곡선.

식 1과 동일한 과정으로 y=cos(θ)와 y=tan(θ)의 미분을 유도하면 각각 식2와 3과 같습니다.

$$\begin{align}\tag{2} \cos(\theta)&= \sin \left(\frac{\pi}{2}-\theta \right)\\ y&=\cos(\theta)\\ dy&=d\left(\sin \left(\frac{\pi}{2}-\theta \right) \right)\\ &=\cos \left(\frac{\pi}{2}-\theta \right) \cdot d(-\theta)\\ &=-\cos\left(\frac{\pi}{2}-\theta \right) \cdot d(\theta)\\ \therefore \frac{dy}{d\theta}&=-\cos\left(\frac{\pi}{2}-\theta \right)\\ &=-\sin(\theta) \end{align}$$ $$\begin{align}\tag{3} y&=\tan(\theta)\\ &=\frac{\sin(\theta)}{\cos(\theta)}\\ \frac{dy}{d\theta}&=\frac{d(\sin(\theta))\cos(\theta)-\sin(\theta)d(\cos(\theta))}{\cos^2(\theta)}\\ &=\frac{\cos(\theta)\cos(\theta)+\sin(\theta)\sin(\theta)}{\cos^2(\theta)}\\ &=\frac{\cos^2(\theta)+\sin^2(\theta)}{\cos^2(\theta)}\\ &=\frac{1}{\cos^2(\theta)}\\ &=\sec^2(\theta) \end{align}$$

simpy 함수 diff()를 삼각함수의 미분에 적용할 수 있습니다.

theta=symbols("theta")
diff(sin(theta))
$\quad \small \color{navy}{\cos{\left(\theta \right)}}$
diff(cos(theta))
$\quad \small \color{navy}{ - \sin{\left(\theta \right)}}$
diff(tan(theta))
$\quad \small \color{navy}{ \tan^{2}{\left(\theta \right)} + 1}$

위 결과 tan2(θ)+1를 정리하면 다음과 같습니다.

$$\begin{align} \tan^2(\theta)+1&=\frac{\sin^2(\theta)}{\cos^2(\theta)}+1\\ &=\frac{1-\cos^2(\theta)}{\cos^2(\theta)}+1\\ &=\frac{1}{\cos^2(\theta)}\\ &=\sec^2(\theta) \end{align}$$
simplify(diff(tan(theta)))
$\quad \small \color{navy}{ \frac{1}{\cos^{2}{\left(\theta \right)}}}$

원의 총 각도는 2π 또는 360도 입니다. 어떤 총 기간을 T로 할 때 일정한 시간 t에 따라 각도의 변화는 다음과 같이 나타낼 수 있습니다.

$$\theta = 2\pi \frac{t}{T}$$

초당 주기(T)의 수를 주파수(frequency, n)라고 하면 $\quad \small \color{navy}{ n =\frac{1}{T}}$의 관계를 갖습니다. 이 관계를 위 식에 대입하면 다음과 같이 표현됩니다.

$$\theta =2 \pi nt$$

예)
  함수 $y = \sin(2 \pi nt)$를 미분 합니다.

이 식의 변수는 $\theta$가 아닌 시간 t에 대해 미분하기 위해 다음 관계를 적용합니다.

$$\begin{align} \frac{dy}{dt}&=\frac{dy}{d \theta}\frac{d \theta}{dt}\\ \rightarrow y&=\sin(\theta), \theta=2 \pi nt\\ \frac{dy}{dt}&=\cos(\theta)2n \pi\\ &=2n\pi \cos(2 \pi nt) \end{align}$$
n, t=symbols("n, t")
y=cos(2*pi*n*t)
y.diff(t)
$\quad \small \color{navy}{ - 2 \pi n \sin{\left(2 \pi n t \right)}}$

2차미분과 역함수 미분

삼각함수의 2차미분

sin과 cos의 미분은 각각 상호간의 함수가 전환됩니다. 이 관계를 사용하여 각 함수의 2차 미분을 계산할 수 있습니다.

$$\begin{align}\tag{4} \frac{d(\sin(\theta))}{d \theta}&=\cos(\theta)\\ \frac{d^2(\sin(\theta))}{d \theta^2}&=\frac{d(\cos(\theta))}{d \theta}\\ &=-\sin(\theta) \end{align}$$ $$\begin{align}\tag{5} \frac{d(\cos(\theta))}{d \theta}&=-\sin(\theta)\\ \frac{d^2(\cos(\theta))}{d \theta^2}&=-\frac{d(\sin(\theta))}{d \theta}\\ &=-\cos(\theta) \end{align}$$ $$\begin{align}\tag{6} \frac{d^2(\tan(\theta))}{d \theta}&=\frac{d\left(\frac{1}{\cos^2(\theta)} \right)}{d \theta}\\ &= \frac{2 \cos(\theta) \sin(\theta)}{\cos^4(\theta)}\\ &=\frac{2 \tan(\theta)}{\cos^2(\theta)}\\ &=2\tan(\theta)\sec^2(\theta)\\ \end{align}$$
theta=symbols("theta")
diff(sin(theta), theta, 2)
$\quad \small \color{navy}{- \sin{\left(\theta \right)}}$
diff(cos(theta), theta, 2)
$\quad \small \color{navy}{ - \cos{\left(\theta \right)}}$
simplify(diff(tan(theta), theta, 2))
$\quad \small \color{navy}{ \frac{2 \tan{\left(\theta \right)}}{\cos^{2}{\left(\theta \right)}}}$

sin(x), cos(x)함수의 2차 미분계수는 원함수와 부호만 반대인 결과를 갖습니다.

표 1. sin, cos, tan 함수의 미분규칙
함수(y)1차 미분$\left(\frac{dy}{d\theta}\right)$2차 미분$\left(\frac{d^2y}{d\theta^2}\right)$
sin(θ) cos(θ) -sin(θ)
cos(θ) -sin(θ) -cos(θ)
tan(θ) sec2(θ) 2tan(θ)sec2(θ)

역함수의 미분

식 7은 sin, cos, tan의 역함수를 나타낸 것입니다.

$$\begin{align}\tag{7} & \;y=\arcsin(x) \;\Leftrightarrow \; y = \sin^{-1}(x)\;\Leftrightarrow \; x=\sin(y)\\ & \;y=\arccos(x)\;\Leftrightarrow \; y = \cos^{-1}(x)\;\Leftrightarrow \; x=\cos(y)\\ & \;y=\arctan(x) \;\Leftrightarrow \; y = \tan^{-1}(x)\;\Leftrightarrow \; x=\tan(y)\\ \end{align}$$

예를 들어 $y = \sin^{-1}(x)$는 x = sin(y)를 의미합니다. 이 삼각함수의 역함수의 미분은 다음과 같습니다.

$$\begin{align} y=\sin^{-1}(x)& \Leftrightarrow x=\sin(y)\\ \frac{dx}{dy}&=\cos(y)\\ \frac{dy}{dx}&=\frac{1}{\cos(y)} \end{align}$$

삼각함수의 합공식 sin2(y) + cos2(y) = 1을 적용하여 위 결과를 정리할 수 있습니다.

$$\begin{align} \cos(y)&=\sqrt{1-\sin^2(y)}\\ &=\sqrt{1-x^2}\\ \frac{dy}{dx}&=\frac{1}{\cos(y)}\\ &=\frac{1}{\sqrt{1-x^2}} \end{align}$$
x, y=symbols("x, y")
eq=y-asin(x) #원 식 
eq_x=solve(eq, x) #원 식을 x에 대해 정리(역함수) 
eq_x
[sin(y)]
eq_y=solve(eq, y) #원 식을 y에 대해 정리
eq_y
[asin(x)]
dxdy=eq_x[0].diff(y) # 역함수 미분 
dxdy
$\quad \small \color{navy}{ \cos{\left(y \right)}}$
(1/dxdy).subs(y, eq_y[0])
$\quad \small \color{navy}{ \frac{1}{\sqrt{1 - x^{2}}}}$
eq_y[0].diff(x)#원 함수 미분
$\quad \small \color{navy}{ \frac{1}{\sqrt{1 - x^{2}}}}$

예)
  cos-1(x)의 미분합니다.

이 함수 역시 위의 sin-1(x)와 동일한 방식으로 계산할 수 있습니다.

$$\begin{align} y = \cos^{-1}(x)& \rightarrow x = \cos(y)\\ \frac{dx}{dy} &= - \sin(y)\\ &= - \sqrt{1-\cos^2(y)}\\ \frac{dy}{dx} &= -\frac{1}{\sin(y)}\\ &= -\frac{1}{\sqrt{1-\cos^2(y)}}\\ &= -\frac{1}{\sqrt{1-x^2}} \end{align}$$
x, y=symbols("x, y") 
eq=y-acos(x) 
eq_x=solve(eq, x) 
eq_x
[cos(y)]
eq_y=solve(eq, y) #원 식을 y에 대해 정리 
eq_y
[acos(x)]
dxdy=eq_x[0].diff(y) # 역함수 미분  
dxdy
$\quad \small \color{navy}{ - \sin{\left(y \right)}}$
(1/dxdy).subs(y, eq_y[0])
$\quad \small \color{navy}{ - \frac{1}{\sqrt{1 - x^{2}}}}$
eq_y[0].diff(x)#원 함수 미분
$\quad \small \color{navy}{ - \frac{1}{\sqrt{1 - x^{2}}}}$

예)
  tan-1(x)의 미분합니다.

$$\begin{align} y = \tan^{-1}(x)& \rightarrow x = \tan(y) = \frac{\sin(y)}{\cos(y)}\\ \frac{dx}{dy}& =\frac{1}{\cos^2(y)}\\ & =\frac{\sin^2(y)+\cos^2(y)}{\cos^2(y)}\\ & = 1 + \tan^2(y)\\ & = 1 + x^2\\ \frac{dy}{dx}& =\frac{1}{1+x^2} \end{align}$$
x= symbols("x")
diff(atan(x))
$\quad \small \color{navy}{ \frac{1}{x^{2} + 1}}$
표 2. 역삼각함수의 미분규칙
함수(y)1차 미분$\left(\frac{dy}{dx}\right)$
sin-1(x) $\frac{1}{\sqrt{1-x^2}}$
cos-1(x)$-\frac{1}{\sqrt{1-x^2}}$
tan-1(x)$\frac{1}{1+x^2}$

예)
  y = cos3(θ)을 미분해 봅니다.

이 식에서 cos3(θ) = (cos(θ))3이 성립하므로 cos(θ) = u로 치환하여 미분할 수 있습니다.

theta, u=symbols("theta u")
u1=cos(theta)
y=u**3
dydu=y.diff(u)
dydu
$\quad \small \color{navy}{ 3 u^{2}}$
dudth=u1.diff(theta)
dudth
$\quad \small \color{navy}{ - \sin{\left(\theta \right)}}$
dydth=dydu*dudth
dydth
$\quad \small \color{navy}{ - 3 u^{2} \sin{\left(\theta \right)}}$
dydth.subs(u, u1)
$\quad \small \color{navy}{ - 3 \sin{\left(\theta \right)} \cos^{2}{\left(\theta \right)}}$
diff(cos(theta)**3, theta) #원 함수를 직접미분
$\quad \small \color{navy}{ - 3 \sin{\left(\theta \right)} \cos^{2}{\left(\theta \right)}}$

예)
 y = sin(x+a)를 미분합니다.

이 함수 역시 치환 방법을 적용합니다. 이 문제에 대한 미분 과정은 다음과 같습니다.

$$\begin{aligned} &u=x+a \rightarrow y=\sin(u)\\ &\frac{du}{dx}=1\\ &\frac{dy}{du}=\cos(u)\\ &\frac{dy}{du}\frac{du}{dx}=\cos(u)\frac{du}{dx}\\ &\therefore \frac{dy}{dx}=\cos(x+a) \end{aligned}$$
a, x=symbols("a x")
y=sin(x+a)
y.diff(x)
$\quad \small \color{navy}{ \cos{\left(a + x \right)}}$

결과적으로 치환한 부분의 미분과 전체 미분을 곱한 것과 같습니다.

예)
  y = log(sin(θ))를 미분합니다. sin(θ) = u로 치환하면 y = log(u)이 됩니다.

theta, u=symbols("theta u")
y=log(sin(theta))#원 함수 
y.diff(theta)
$\quad \small \color{navy}{ \frac{\cos{\left(\theta \right)}}{\sin{\left(\theta \right)}}}$
u1=sin(theta) #원함수의 부분을 u(=u1)로 치환 
y1=log(u)
dydu=y1.diff(u)
dydu
$\quad \small \color{navy}{ \frac{1}{u}}$
dudth=u1.diff(theta)
dudth
$\quad \small \color{navy}{ \cos{\left(\theta \right)}}$
dydth=dydu*dudth
dydth
$\quad \small \color{navy}{ \frac{\cos{\left(\theta \right)}}{u}}$
dydth.subs(u, u1)
$\quad \small \color{navy}{ \frac{\cos{\left(\theta \right)}}{\sin{\left(\theta \right)}}}$

예)
 y = cot(θ)를 미분합니다.

$$\begin{align} y &= \cot(\theta) \\    &= \frac{\cos(\theta)}{\sin(\theta)}\\ \frac{dy}{dx} &=\frac{-\sin^2(\theta)-\cos^2(\theta)}{\sin^2(\theta)}\\ &= \frac{-1}{\sin^2(\theta)}\\ &= -\operatorname{csc}^2(\theta) \end{align}$$
theta=symbols("theta")
y=cot(theta)
dy=diff(y, theta)
simplify(dy)
$\quad \small \color{navy}{ - \frac{1}{\sin^{2}{\left(\theta \right)}}}$

예)
 y=tan(3θ)를 미분합니다.

theta=symbols("theta")
y=tan(3*theta)
dy=diff(y, theta)
simplify(dy)
$\quad \small \color{navy}{ \frac{3}{\cos^{2}{\left(3 \theta \right)}}}$

예)
 $y = \sqrt{1 + 3\tan^2(\theta)}$를 미분합니다.

이 함수에서 $3\tan^2(\theta) = u$로 치환할 수 있습니다.

theta, u=symbols("theta u")
y=sqrt(1+3*tan(theta)**2)
y.diff(theta)
$\quad \small \color{navy}{ \frac{3 \left(2 \tan^{2}{\left(\theta \right)} + 2\right) \tan{\left(\theta \right)}}{2 \sqrt{3 \tan^{2}{\left(\theta \right)} + 1}}}$
u1=3*tan(theta)**2
y1=sqrt(1+u)
dydu=y1.diff(u)
dydu
$\quad \small \color{navy}{ \frac{1}{2 \sqrt{u + 1}}}$
dudth=u1.diff(theta)
dudth
$\quad \small \color{navy}{ 3 \left(2 \tan^{2}{\left(\theta \right)} + 2\right) \tan{\left(\theta \right)}}$
dydth=dydu*dudth
dydth
$\quad \small \color{navy}{ \frac{3 \left(2 \tan^{2}{\left(\theta \right)} + 2\right) \tan{\left(\theta \right)}}{2 \sqrt{u + 1}}}$
dydth.subs(u, u1)
$\quad \small \color{navy}{ \frac{3 \left(2 \tan^{2}{\left(\theta \right)} + 2\right) \tan{\left(\theta \right)}}{2 \sqrt{3 \tan^{2}{\left(\theta \right)} + 1}}}$

예)
 y = sin(x)cos(x)를 미분합니다.

이 함수의 미분은 곱법칙을 적용합니다.

$$\begin{align} y = A(x)B(x) & \rightarrow \frac{dy}{dx}=A'(x)B(x)+A(x)B'(x)\\ \frac{dy}{dx} & = d(\sin(x)) \cos(x)+\sin(x)d(\cos(x))\\ &= \cos^2(x)-\sin^2(x) \end{align}$$
x=symbols("x")
y=sin(x)*cos(x)
dy=diff(y, x)
dy
$\quad \small \color{navy}{ - \sin^{2}{\left(x \right)} + \cos^{2}{\left(x \right)}}$

댓글