Can be differentiated
pip install numpy
Execute the above command in advance
pip install matplotlib
You also need matplotlib when drawing graphs
adipy1.py
# -*- coding: utf-8 -*-
from adipy import ad, sin, adn
#ad object creation
x = ad(1.5)
#Substitute x squared for y
y = x**2
print y
# ad(2.25, array([ 3.]))
# array([ 3.])"3" is the value obtained by differentiating y once and substituting 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 object creation
#The second argument 4 is calculated up to the fourth derivative
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 object creation
xAD = [adn(1.5, i) for i in xrange(1, 7)]
def z(x):
return x*sin(x**2)
#Set the x-axis range of the graph
x = np.linspace(0.75, 2.25)
#Label and draw the original function as an Actual Function
plt.plot(x, z(x), label='Actual Function')
for i in xrange(len(xAD)):
#Use Taylor polynomial
fz = taylorfunc(z(xAD[i]), at=xAD[i].nom)
plt.plot(x, fz(x), label='Order %d Taylor'%(i+1))
#Set the position of the function label
plt.legend(loc=0)
plt.show()
Recommended Posts