-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3 Point Newton-Cotes Open Rule
50 lines (40 loc) · 1.59 KB
/
3 Point Newton-Cotes Open Rule
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
Hey guys I'm new to python
Kindly help me figure out why the Convergence rate is all zero. I've been battling with for some hours now.
I'm trying to answer this question: Do a simple mesh refinement study to find the rate of convergence for the three
point Newton-Cotes Open Rule. My text function is sin(x)/x.
def f(x):
return np.sin(x)/x
def Two_Pt_Gauss(f=f,a=-1,b=1,n=400):
w1 = 1.0
w2 = 1.0
x1 = .57735227
x2 = -.57735227
Area = w1*f(x1) + w2*f(x2)
return Area
#Convergence rate for Two Point Gauss
def Error(trueValue,Aziegbemi):
return abs(trueValue-Aziegbemi)
def ConvergenceRate(ErrorVec):
rateVec = []
for i in range(len(ErrorVec)-1):
rateVec.append(np.log(ErrorVec[i]/ErrorVec[i+1])/np.log(2.0))
return rateVec
eps = 1e-16 #fudge factor to deal with the singularity in f(x) when x = 0
trueValue = 0.946083070367
#truevalue = 11.58666667
AreaVec = []
ErrorVec = []
for i in range(7):
n = 2**(i+1)
AreaVec.append(Two_Pt_Gauss(f,0+eps,1.0,n))
ErrorVec.append(Error(trueValue,AreaVec[i]))
RateVec = ConvergenceRate(ErrorVec)
print("Area = ", '%.9f' % AreaVec[1])
print("Area Vector: ",AreaVec)
print("Erro Vector: ", ErrorVec)
print("Convergence Rate: ", RateVec)
OUTPUT:
Area = 1.890725367
Area Vector: [1.890725366594047, 1.890725366594047, 1.890725366594047, 1.890725366594047, 1.890725366594047, 1.890725366594047, 1.890725366594047]
Erro Vector: [0.9446422962270471, 0.9446422962270471, 0.9446422962270471, 0.9446422962270471, 0.9446422962270471, 0.9446422962270471, 0.9446422962270471]
Convergence Rate: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]