<jupyter_start><jupyter_text>Function<jupyter_code># Define fxn to integrate
def func(x):
    a = -2
    b = 10
    return np.exp(a*x)*np.cos(b*x)<jupyter_output><empty_output><jupyter_text>Core of Trapezoid Method<jupyter_code>def trapezoid_core(f, x, h):
    return 0.5*h*(f(x + h) + f(x))<jupyter_output><empty_output><jupyter_text>Wrapper Fxn to Perform Trapezoid Method<jupyter_code># f == fxn to integrate
# a == lower bound of integration
# b == upper bound of integration
# N == # of fxn evaluations to use
def trapezoid_method(f, a, b, N):
    
    # Define x values on grid
    x = np.linspace(a, b, N)
    h = x[1]- x[0]
    
    #Define value of integral
    Fint = 0.0
    
    # Perform integral
    for i in range(0, len(x)-1, 1):
        Fint += trapezoid_core(f, x[i], h)
        
    # Return answer
    return Fint
print(trapezoid_method(func, 0, (np.pi), 3400))
# print("Number of iterations is ", x[i])<jupyter_output><empty_output><jupyter_text>Simpsons Method  Core of Simpsons Method<jupyter_code>def simpson_core(f, x, h):
    return h*(f(x) + 4*f(x+h) + f(x + 2*h))/3.<jupyter_output><empty_output><jupyter_text>Define Wrapper Fxn for Simpson Method<jupyter_code># f == fxn to integrate
# a == lower bound of integration
# b == upper bound of integration
# N == # of fxn evaluations to use
def simpson_method(f, a, b, N):
    
    # Define x values on grid
    x = np.linspace(a, b, N)
    h = x[1] - x[0]
    
    # Define integral
    Fint = 0.0
    
    # Perform integral 
    for i in range(0, len(x) - 2, 2):
        Fint += simpson_core(f, x[i], h)
        
    # Apply Simpson method over last inetegral
    if((N%2) == 0):
        Fint += simpson_core(f, x[-2], 0.5*h)
        
    # Return answer
    return Fint
        
    
print(simpson_method(func, 0, np.pi, 150))<jupyter_output><empty_output><jupyter_text>Romberg Method  Define Romberg Core<jupyter_code>def romberg_core(f, a, b, i):
    
    # Diff between a and b
    h = b-a
    
    # Interval between fxn evaluations
    dh = h/2.**(i)
    
    # Cofactor
    K = h/2.**(i+1)
    
    # Fxn evaluations
    M = 0.0
    for j in range(2**i):
        M += f(a + 0.5*dh + j*dh)
        
    # Return answer
    return K*M<jupyter_output><empty_output><jupyter_text>Wrapper Fxn to Perform Romberg Method<jupyter_code>def romberg_integration(f, a, b, tol):
    
    # Define iteration variable
    i = 0
    
    # Definition of max iterations
    imax = 10000
    
    # Error estimate set to large value
    delta = 100*np.fabs(tol)
    
    # Array of integral results
    I = np.zeros(imax, dtype=float)
    
    # Zeroth Romberg iteration
    I[0] = 0.5*(b-a)*(f(a)+f(b))
    
    # Iterate by i
    i += 1
    
    while(delta>tol):
        
        # Find this Romberg iteration 
        I[i] = 0.5*I[i-1] + romberg_core(f, a, b, i)
        
        # Find new fractional error estimate
        delta = np.fabs((I[i]-I[i-1])/I[i])
        print(i,I[i],I[i-1],delta)
        
        if(delta>tol):
            
            # Iterate
            i += 1
            
            # If max # of iterations reached
            if(i>imax):
                
                print("Max number of iterations reached")
                raise StopIteration('Stopping iterations after ',i)
                
    
    # Return answer
    return I[i]
                
print("Romberg result")
tolerance = 1.0e-6
RI = romberg_integration(func, 0, np.pi, tolerance)
print(RI)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12560940636650417, 'fraction_numerical': 0.019214224261542873, 'mean_word_length': 2.2967863894139886, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '250167', 'license': 'MIT', 'n_tokens_mistral': 1366, 'n_tokens_neox': 1299, 'n_words': 326, 'path': 'astr-119-hw-5/astr-119-hw-5.ipynb', 'repo_name': 'jjohnst6260/astr-119', 'size': '3487'}