-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumerical_diff.py
More file actions
47 lines (37 loc) · 811 Bytes
/
Numerical_diff.py
File metadata and controls
47 lines (37 loc) · 811 Bytes
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
#Numerical differentiation
import numpy as np
import matplotlib.pyplot as plt
def fun(x):
return 4*(x**3) + 2*(x**2)
def der_true(x):
return 12*(x**2) + 4*x
def bwd(f,x,h):
m=x-h
df=(f(x)-f(m))/h
return df
def fwd(f,x,h):
l=x+h
df=(f(l)-f(x))/h
return df
def cd(f,x,h):
m=x-h
l=x+h
df=(f(l)-f(m))/(2*h)
return df
samples=30
h=0.1
xs=np.linspace(1,20,num=samples)
yb=np.linspace(1,20,num=samples)
yf=np.linspace(1,20,num=samples)
yc=np.linspace(1,20,num=samples)
true=np.linspace(1,20,num=samples)
for i in range(0,len(xs)):
yb[i]=bwd(fun,xs[i],h)
yf[i]=fwd(fun,xs[i],h)
yc[i]=cd(fun,xs[i],h)
true[i]=der_true(xs[i])
plt.plot(xs,yb,'r.')
plt.plot(xs,yf,'b.')
plt.plot(xs,yc,'g.')
plt.plot(xs,true,'k-')
plt.show()