def check1():
    I1, n1 = romberg(f1, 0, 1)
    I2, n2 = romberg(f2, 0, 4)
    print("Comparing Solutions\n")
    print("The integral for the 1st equation using romberg's is:", I1, "and the number of evaluations are:", n1 )
    print("The integral for the 2nd equation using romberg's is:", I2, "and the number of evaluations are:", n2, "\n" )
    print("The percentage error in the 1st solution for 10^2 is", math.fabs((((I1 - result11) / I1 )*100)))
    print("The percentage error in the 1st solution for 10^4 is", math.fabs((((I1 - result12) / I1 )*100)))
    print("The percentage error in the 1st solution for 10^6 is", math.fabs((((I1 - result13) / I1 )*100)))
    print("The percentage error in the 2nd solution for 10^2 is", math.fabs((((I2 - result21) / I2 )*100)))
    print("The percentage error in the 2nd solution for 10^4 is", math.fabs((((I2 - result22) / I2 )*100)))
    print("The percentage error in the 2nd solution for 10^6 is", math.fabs((((I2 - result23) / I2 )*100)))
    print("\n")
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") 

# The u values that the function will be evaluated with
u = arange(0, 1.01, 0.05)
print("    u\t   g(u)")
gu = []  #list that will contain all of g(u)s (y-coordinates for your plot)

# Iterating through the u list
for i in u:
    # if the current i value is 0 then the integral will evaluate to 0
    if i == 0:
        g = 0.0
        # Otherwise perform romberg integration
    else:
        I, nPanels = romberg(
            f, 0,
            1 / i)  # perform romberg integration on f here (get result in
        # I, nPanels is the number of panels used but is not
        # used for output).
        # g(i) is i^3 multiplied by the romberg integration of f(x)
        g = I * i**3  # evaluate g(i) here
    print('{:6.2f}{:13.6f}'.format(i, g))
    gu.append(g)
#
# Place the code that creates the required plot using pylab here.
# Be sure to label axes and provide the same title as shown
# in the "prob6_1-14.png" image file on BB

# Setting the y axis label
plt.ylabel('g(u)')
# Setting the x axis label
Beispiel #4
0
from romberg import *
import math


def f(x):
    return math.sin(x)**-.5


def F(t):
    return 2 * (1 - t**4)**-.5


I, n = romberg(F, math.sin(0)**.5, math.sin(math.pi / 4)**.5)
print("Integral =", I)
print("nPanels =", n)
'''
::: CODE RUN :::
cheng@Cheng:~/code/na/romberg$ python3 main.py 
Integral = 1.7911613389539645
nPanels = 64
'''
Beispiel #5
0
    return 2 * sp.jv(3, 2.7 * x) * sp.jv(3, 2.7 * x) * x


def integ2(x):
    return 2 * pow(abs(sp.jv(3, 2.7) / sp.kv(3, 1.2)), 2) * sp.kv(3, 1.2 * x) * sp.kv(3, 1.2 * x) * x


def total_integ(x):
    if x <= 1:
        return integ1(x)

    else:
        return integ2(x)


ss, j, y, dy, index = romberg(total_integ, 0, 15, full=True)
ANS = 0.046038860370705266
y = array(y)
temp = [ANS] * len(index)
temp = array(temp)
dy = array(dy)
index = array(index)
loglog(pow(2, index), abs(dy), "k", color="r")

ss, j, y, dy, index = qromb3(total_integ, 0, 15, full=True)
y = array(y)

temp = [ANS] * len(index)
temp = array(temp)
dy = array(dy)
index = array(index)
Beispiel #6
0
## example6_7
from math import cos, sqrt, pi
from romberg import *


def f(x):
    return 2.0 * (x**2) * cos(x**2)


I, n = romberg(f, 0, sqrt(pi))
print "Integral =", I
print "numEvals =", n
raw_input("\nPress return to exit")
#Imports

from romberg import *  # Have the romberg file and the trapezoid integration file provided on study direct in the same folder as your code.

#Constants
LowerLimit = 0
UpperLimit = 10


#Defining Function
def function(x):
    return (x**2)


#Returns the integral and the number of panels used. Unless stated otherwise, leave the last term as "tol=1.0e-6". tol stands for tolerance, and controls the size of the error.
#Answer[0] = Numerical value of definite integration.
#Answer[1] = Number of panels (Number of trapeziums used).
Answer = romberg(function, LowerLimit, UpperLimit, tol=1.0e-6)

print Answer
            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
            showAnswer()

        #Gaussian
        if go.y>420 and go.y<495:
            pass
##        else:
Beispiel #9
0
from numpy import *
import scipy.weave as weave
from trapz import *
from week0 import *
from romberg import *
from trapz import *
import scipy.special as sp
from matplotlib.pyplot import *

def integ1(x) :
    return 2*sp.jv(3,2.7*x)*sp.jv(3,2.7*x)*x

def integ2(x):
    return 2*pow(abs(sp.jv(3,2.7)/sp.kv(3,1.2)),2)*sp.kv(3,1.2*x)*sp.kv(3,1.2*x)*x

def total_integ(x):
    if x<=1 :
       return integ1(x)
     
    else:
        return integ2(x)
def integ1_mod(x):
    return sp.jv(3,2.7*x**0.5)*sp.jv(3,2.7*x**0.5)
print romberg(integ1,0,1,EPS =1e-15) 
print romberg(integ1_mod,0,1,EPS = 1e-15)
Beispiel #10
0
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))
Beispiel #11
0
import scipy.special as sp
from matplotlib.pyplot import *
def integ1(x) :
    return 2*sp.jv(3,2.7*x)*sp.jv(3,2.7*x)*x

def integ2(x):
    return 2*pow(abs(sp.jv(3,2.7)/sp.kv(3,1.2)),2)*sp.kv(3,1.2*x)*sp.kv(3,1.2*x)*x

def total_integ(x):
    if x<=1 :
       return integ1(x)
     
    else:
        return integ2(x)

romb =  romberg(total_integ,0,15)
print "integration value = %f , no. of function calls = %d" % (romb[0],pow(2,romb[1]))
a =  romberg(integ1,0,1)
b = romberg(integ2,1,15)
ans = a[0]+b[0]
calls = pow(2,a[1]-1)+pow(2,b[1]-1)
print "integration value with splitting = %f no . of function calls = %d "%(ans,calls)

ss,j,y,dy,index = romberg(total_integ,0,15,full = True)
ANS = 0.046038860370705266
print index, dy
y = array(y)
temp = [ANS]*len(index)
temp = array(temp)
dy = array(dy)
index = array(index)
Beispiel #12
0
# Evaluates the integrand at the provided value of x
def f(x):
    if x == 0: return 0
    else: return (x**4 * exp(x)) / ((exp(x) - 1)**2)


# Sets initial data and creates a list for the values of g(u)
u = arange(0, 1.01, 0.05)
print("    u\t   g(u)")
gu = []

for i in u:
    if i == 0: g = 0.0
    else:
        # Performs Romberg integration on f
        I, nPanels = romberg(f, 0, 1 / i)
        # Evaluates g(i)
        g = i**3 * I
    # Prints the values of u and g(u)
    print('{:6.2f}{:13.6f}'.format(i, g))
    gu.append(g)

# Plots g(u) vs u and saves the plot to prob6_1_14.png
plt.plot(u, gu, "b-")
plt.xlabel("u")
plt.ylabel("g(u)")
plt.xlim(xmin=0.0, xmax=1.0)
plt.ylim(ymin=0.0)
plt.title("Problem 6.1.14")
plt.savefig("prob6_1_14.png")
plt.show()
Beispiel #13
0
## example6_7
from math import cos,sqrt,pi
from romberg import *

def f(x): return 2.0*(x**2)*cos(x**2)

I,n = romberg(f,0,sqrt(pi))
print "Integral =",I
print "numEvals =",n
raw_input("\nPress return to exit")
#Imports

from romberg import * # Have the romberg file and the trapezoid integration file provided on study direct in the same folder as your code.

#Constants
LowerLimit = 0
UpperLimit = 10

#Defining Function
def function(x):
	return (x**2)

#Returns the integral and the number of panels used. Unless stated otherwise, leave the last term as "tol=1.0e-6". tol stands for tolerance, and controls the size of the error.
#Answer[0] = Numerical value of definite integration.
#Answer[1] = Number of panels (Number of trapeziums used).
Answer = romberg(function,LowerLimit,UpperLimit,tol=1.0e-6)

print Answer