본문 바로가기

Python/AI 수학 with Python

[Python] 접선 그리기

 

위의 예제를 python을 통해 구현해보자.

[In]

# 접선 그리기

# f(x) = 3x^2 + 4x - 5, x = 1
# f'(x) = 6x + 4
# f'(1) = 10

%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt

# f(x)
def my_func(x):
    return 3*x**2 + 4*x - 5

# f'(x)
def my_func_dif(x):
    return 6*x + 4

x = np.linspace(-3, 3, 1000)
y = my_func(x)

a = 1

y_t = my_func_dif(a)*x- a*my_func_dif(a) + my_func(a) # x = 1일 떄 접선

plt.plot(x, y, label = 'y')
plt.plot(x, y_t, label = 'y_t')
plt.legend()

plt.xlabel('x', size = 14)
plt.ylabel('y', size = 14)
plt.grid()

plt.show()

[Out]