-
Notifications
You must be signed in to change notification settings - Fork 4
/
dx.py
51 lines (30 loc) · 713 Bytes
/
dx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
#
# Author Dario Clavijo 2015
# GPLv3
# derivative of f(x)
def __df(f, x, h):
return (f(x + h / 2) - f(x - h / 2)) / h
def derivative(f):
h = 1e-8
def dx(x):
return __df(f, x, h)
return dx
def g(x):
return x**2
dg = derivative(g)
# definite integral of f(x)
def integral(f):
h = 1e-4
def intf(b, a=0):
sum = 0
x = a
while x <= b:
sum += h * __df(f, x, h) / 2.0
x += h
return sum
return intf
print(("g(x)=", [g(x) for x in range(11)]))
print(("df g(x)=", [dg(x) for x in range(11)]))
inv1 = integral(derivative(g))
print(("integral(df g(x))=", [inv1(x) for x in range(11)]))