1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

めざせpythonライブラリマスター (31)adipy

Posted at

#【ライブラリ説明】
 微分ができる

#【準備】
 pip install numpy
  上記コマンドを事前に実行
 pip install matplotlib
  グラフを描くときはmatplotlibも必要

#【プログラム】

adipy1.py
# -*- coding: utf-8 -*-

from adipy import ad, sin, adn

# adオブジェクト作成
x = ad(1.5)

# xの2乗をyに代入
y = x**2
print y
# ad(2.25, array([ 3.]))
# array([ 3.])の「3」はyを1回微分してxを代入した値

# dy(1)/dx
print y.d(1)
# 3.0

z = x*sin(x**2)
print z
# ad(1.1671097953318819, array([-2.04870811]))

# dy(1)/dx
print z.d(1)
# -2.04870810536

# adnオブジェクト作成
# 第二引数の4は4回微分まで求める
x = adn(1.5, 4)

y = x**2
print y
# ad(2.25, array([ 3.,  2.,  0., -0.]))

# dy(2)/dx
print y.d(2)
# 2.0

z = x*sin(x**2)
print z
# ad(1.1671097953318819, array([  -2.04870811,  -16.15755076,  -20.34396265,  194.11618384]))

# dy(4)/dx
print z.d(4)
# 194.116183837
adipy2.py
# -*- coding: utf-8 -*-

from adipy import adn, sin, taylorfunc
import matplotlib.pyplot as plt
import numpy as np

# adnオブジェクト作成
xAD = [adn(1.5, i) for i in xrange(1, 7)]

def z(x):
    return x*sin(x**2)

# グラフのx軸の範囲を設定
x = np.linspace(0.75, 2.25)
# 元の関数をActual Functionとしてラベル付けして描画
plt.plot(x, z(x), label='Actual Function')


for i in xrange(len(xAD)):
    # テイラー多項式を使用
    fz = taylorfunc(z(xAD[i]), at=xAD[i].nom)
    plt.plot(x, fz(x), label='Order %d Taylor'%(i+1))

# 関数のラベルの位置を設定
plt.legend(loc=0)
plt.show()
graph.png

#【参考サイト】
 pypi
 github

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?