プログラミング言語「Python」で制作 Part24 | Photoshop CC Tutorials

今回は今、人気急上昇中であるプログラミング言語の「Python」を使って、

数値微分をやってみました。

 

 

 

■ プログラム

import numpy as np
import matplotlib.pyplot as plt

def numerical_diff (f,x):
    h = 1e-4
    return (f(x+h)-f(x-h))/(2*h)

def function_1 (x):
    return x**2

def tangent_line (f,x):
    d = numerical_diff (f,x)
    y = f(x) - d*x
    print (d)
    return lambda t: d*t + y 

x = np.arange (0.0, 20.0, 0.1)
y = function_1(x)

plt.xlabel ("x")
plt.ylabel ("f(x)")

numerical_diff (function_1, 1)

tf = tangent_line (function_1, 10)
y2 = tf(x)

plt.plot (x, y)
plt.plot (x, y2)
plt.show ()

 

 

■ 参考サイト

数値微分(Numerical Differentiation)