<jupyter_start><jupyter_text>trapezoid method<jupyter_code>def trapezoid_core(f,x,h):
    return 0.5*h*(f(x+h) + f(x))
def trapezoid_method(f,a,b,N):
    x = np.linspace(a,b,N)
    h = x[1]-x[0]
    
    Fint = 0.0
    
    for i in range (0,N):
        Fint += trapezoid_core(f,x[i],h)
    
    return Fint
trapz = trapezoid_method(func,0,np.pi,100000)
print(trapz)
def func_integral(x):
    return -1/52 * np.exp(-2*x)*(np.cos(10*x) - 5*np.sin(10*x))
answer = func_integral(np.pi) - func_integral(0)
print(answer)
(trapz - answer)/trapz<jupyter_output><empty_output><jupyter_text>simpsons method<jupyter_code>def simpson_core(f,x,h):
    return h*( f(x) + 4*f(x+h) + f(x+2*h))/3.
def simpsons_method(f,a,b,N):
    x = np.linspace(a,b,N)
    h = x[1]-x[0]
    
    Fint = 0.0
    
    for i in range(0,N):
        Fint += simpson_core(f,x[i],h)
        
    if((N%1)==0):
        Fint += simpson_core(f,x[-1],0.5*h)
        
    return Fint<jupyter_output><empty_output><jupyter_text>romberg integration<jupyter_code>def romberg_core(f,a,b,i):
    h = b-a
    dh = h/2.**(i)
    K = h/2.**(i+1)
    M = 0.0 
    for j in range(2**i):
        M += f(a + 0.5*dh + j*dh)
        
    return K*M
def romberg_integration(f,a,b,tol):
    
    i = 0
    imax = 100000
    delta = 100.0*np.fabs(tol)
    
    I = np.zeros(imax,dtype=float)
    
    I[0] = 0.5*(b-a)*(f(a) +f(b))
    
    i += 1
    
    while(delta>tol):
        I[i] = 0.5*I[i-1] + romberg_core(f,a,b,i)
        
        delta = np.fabs( (I[i]-I[i-1])/I[i] ) 
        print(i,I[i],I[i-1],delta)
        
        if(delta>tol):
            i +=1
            
            if(i>imax):
                print('max iterations reached.')
                raise StopIteration('stopping iterations after ',i)
    
    return I[i]
Answer = func_integral(np.pi)-func_integral(0)
print(Answer)
print("trapezoid")
print(trapezoid_method(func,0,np.pi,100000))
print("simpsons method")
print(simpsons_method(func,0,np.pi,100000))
print("romberg")
tolerance = 1.0e-6
RI = romberg_integration(func,0,np.pi,tolerance)
print(RI, (RI-Answer)/Answer, tolerance)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1835680751173709, 'fraction_numerical': 0.03990610328638498, 'mean_word_length': 2.7125435540069684, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 16, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '373957', 'license': 'MIT', 'n_tokens_mistral': 1022, 'n_tokens_neox': 954, 'n_words': 137, 'path': 'hw-4.ipynb', 'repo_name': 'ashleyparrilla/astr-119-hw-4', 'size': '2130'}