<jupyter_start><jupyter_text>trapezoid method<jupyter_code>def trapezoid_core(f,x,h):        #x left side, h right side (width), f is function
    return 0.5*h*(f(x+h) + f(x))  #midpoint sums (19B)
def trapezoid_method(f,a,b,N):    #wrapper function
    
    x = np.linspace(a,b,N)
    h = x[1] - x[0]
    
    Fint = 0.0                    # F integer
    
    for i in range(0,len(x)-1,1):
        Fint+= trapezoid_core(f,x[i],h)         #x[i], x sub i, number of intervals
        
    return Fint
#check
print(trapezoid_method(func,0,1,10))<jupyter_output><empty_output><jupyter_text>simpson's method<jupyter_code>def simpson_core(f,x,h):
    return h*( f(x) + 4*f(x+h) + f(x+2*h))/3.   #simpson rule equation, integration from x[i] to x[i+2]
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,len(x)-2,2):
        Fint += simpson_core(f,x[i],h)
        
    if((N%2)==0):                              #if N/2 remainder is 0 (if N is even), apply rule again for last seg
        Fint += simpson_core(f,x[-2],0.5*h)    #half of h of last piece
        
    return Fint
#check
print(simpsons_method(func,0,1,10))<jupyter_output><empty_output><jupyter_text>romberg method(aka trapezoid method until tolerance)<jupyter_code>def romberg_core(f,a,b,i):
    h = b-a                                   #upper - lower limit = width
    dh = h/2.**(i)                            #increment btwn new func evals
    K = h/2.**(i+1)
    M = 0.0                                   #function evaluations
    for j in range(2**i):
        M += f(a + 0.5*dh + j*dh)
        
    return K*M
def romberg_integration(f,a,b,tol):           #wrapper function
    i = 0
    
    imax = 1000
    
    delta = 100.0*np.fabs(tol)                #error estimate, large value
    
    I = np.zeros(imax,dtype=float)            #array of integral answers
    
    I[0] = 0.5*(b-a)*(f(a) + f(b))            # I at 0
    
    i += 1                                    #iterate by 1
    
    while(delta>tol):                         #while loop
        
        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]
#check
Answer = func_integral(1) - func_integral(0)
tolerance = 1.0e-6
RI = romberg_integration(func,0,1,tolerance)
print(RI, (RI-Answer)/Answer, tolerance)<jupyter_output><empty_output><jupyter_text>comparison of answers<jupyter_code>print("Answer")
print(Answer)
print("Trapezoid Method")
print(trapezoid_method(func,0,1,100))
print("Simpson's Rule")
print(simpsons_method(func,0,1,30))
print("Romberg Integration")
print(RI, (RI-Answer)/Answer, tolerance)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14468371467025573, 'fraction_numerical': 0.02759084791386272, 'mean_word_length': 2.0904365904365902, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '276687', 'license': 'MIT', 'n_tokens_mistral': 1183, 'n_tokens_neox': 1116, 'n_words': 221, 'path': 'hw-4.ipynb', 'repo_name': 'nncastil/astr-119-hw-4', 'size': '2972'}