def romberg(f,a,b,tol=1.0e-6): def richardson(r,k): for j in range(k-1,0,-1): const = 4.0**(k-j) r[j] = (const*r[j+1] - r[j])/(const - 1.0) return r r = np.zeros(21) r[1] = trapezoid(f,a,b,0.0,1) r_old = r[1] for k in range(2,21): r[k] = trapezoid(f,a,b,r[k-1],k) r = richardson(r,k) if abs(r[1]-r_old) < tol*max(abs(r[1]),1.0): return r[1],2**(k-1) r_old = r[1] print("Romberg quadrature did not converge")
def romberg(f,a,b,tol=1.0e-6): def richardson(r,k): for j in range(k-1,0,-1): const = 4.0**(k-j) r[j] = (const*r[j+1] - r[j])/(const - 1.0) return r r = zeros((21),type=Float64) r[1] = trapezoid(f,a,b,0.0,1) r_old = r[1] for k in range(2,21): r[k] = trapezoid(f,a,b,r[k-1],k) r = richardson(r,k) if abs(r[1]-r_old) < tol*max(abs(r[1]),1.0): return r[1],2**(k-1) r_old = r[1] print "Romberg quadrature did not converge"
def problem22(): Iold = 0.0 a = 0.0 b = 1.0 integral, npan = romberg(f2, 0, 4) print("n", "\t", "h", "\t", "T_h(f)", "\t", "e_h", "\t", "e_2h/e_h") for k in range(1,9): Inew = trapezoid(f1, a, b, Iold, k) if (k > 1) and (abs(Inew - Iold)) < 1.0e-10: break eh2 = ((integral - Iold) / (integral - Inew)) Iold = Inew n1 = (2**(k-1)) print(2**(k-1), "\t", '{0:.4f}'.format(((b-a)/n1)), "\t", '{0:.4f}'.format(Iold), "\t",'{0:.4f}'.format(math.fabs(integral - Iold)), "\t", '{0:.4f}'.format((eh2)), "\n")
## example6_4 from math import sqrt, cos, pi from trapezoid import * def f(x): return sqrt(x) * cos(x) Iold = 0.0 for k in range(1, 21): Inew = trapezoid(f, 0.0, pi, Iold, k) if (k > 1) and (abs(Inew - Iold)) < 1.0e-6: break Iold = Inew print "Integral =", Inew print "nPanels =", 2**(k - 1) raw_input("\nPress return to exit")
while True: go=win.getMouse() if go.x>120 and go.x<230: #Trapezoid if go.y>200 and go.y<275: from trapezoid import * def f(x): return eval(fninput.getText()) print(type(f)) Iold=0.0 a=eval(lower.getText()) print(type(a)) b=eval(upper.getText()) print(type(b)) for k in range(1,21): Inew=trapezoid(f,a,b,Iold,k) if (k>1) and (abs(Inew-Iold))<1.0e-6: break Iold=Inew print("Trapezoidal Integral =",Inew) answer=Inew showAnswer() #Romberg if go.y>310 and go.y<385: from romberg import * def f(x): return eval(fninput.getText()) print(type(f)) I,n=romberg(f,eval(lower.getText()),eval(upper.getText())) print("Romberg Integral =",I) answer=I
from math import * import matplotlib.pyplot as plt #true=exp(1.0)-1.0 #print true #while n1 % 2 != 0 : # n1 = int(raw_input( "Enter number of intervals desired for trapezoidal rule (must be even)" )) n = [] d1 = [] d2 = [] for n1 in range(100, 1100, 100): a = 0.0 b = 1.0 ans1 = trapezoid(exp, a, b, n1) diff1 = abs(true - ans1) print 'Trapezoidal rule = ' + str(ans1) print 'Difference between the result and the true value:', diff1 d1.append(diff1) ans2 = simpson(exp, a, b, n1) print 'Simpson = ' + str(ans2) diff2 = abs(true - ans2) print diff2 d2.append(diff2) n.append(n1) dl1 = [] dl2 = [] for x in d1: dl1.append(log10(x))
## example6_4 from math import sqrt,cos,pi from trapezoid import * def f(x): return sqrt(x)*cos(x) Iold = 0.0 for k in range(1,21): Inew = trapezoid(f,0.0,pi,Iold,k) if (k > 1) and (abs(Inew - Iold)) < 1.0e-6: break Iold = Inew print "Integral =",Inew print "nPanels =",2**(k-1) raw_input("\nPress return to exit")
import numpy as np func = lambda x: np.sqrt(x) * np.cos(x) ## Recursive Trapezoidal Rule from trapezoid import * Iold = 0.0 a = 0.0 b = np.pi for k in range(1, 10): Inew = trapezoid(func, a, b, Iold, k) Iold = Inew print('Inew : ', Inew, ' Panel : ', 2**(k - 1)) ## Romberg Integration from romberg import * print(romberg(func, a, b, tol=1.0e-06))
from trapezoid import * from simpson import * from math import * n1 = 1 n2 = 0 while n1 % 2 != 0 : n1 = int(raw_input( "Enter number of intervals desired for trapezoidal rule (must be even)" )) a = 0 b = 2 * atan(1.0) ans1 = trapezoid(sin, a, b, n1) print 'Trapezoidal rule = ' + str(ans1) ans2 = adaptive_trapezoid(sin, a, b, 0.0001) print 'Adaptive trapezoidal rule = ' + str(ans2)
from trapezoid import * from simpson import * from math import * n1 = 1 n2 = 0 while n1 % 2 != 0: n1 = int( raw_input( "Enter number of intervals desired for trapezoidal rule (must be even)" )) a = 0 b = 2 * atan(1.0) ans1 = trapezoid(sin, a, b, n1) print 'Trapezoidal rule = ' + str(ans1) ans2 = adaptive_trapezoid(sin, a, b, 0.0001) print 'Adaptive trapezoidal rule = ' + str(ans2)