<jupyter_start><jupyter_text>Trapezoid Method<jupyter_code>#trapezoid method
def trap(f,a,b,N):
    AreaTrap = 0    #initial Area
    x = np.linspace(a,b,N)
    dx = x[1]-x[0]  #change in width
    
    for i in range(0,len(x)-1,1):
        AreaTrap += dx * ((f(x+dx) + f(x)))/2  #adding the area of more trapeziod
                                               #to the initial area 
    return AreaTrap<jupyter_output><empty_output><jupyter_text>Simpson's Method<jupyter_code>#Simpon's Rule
def Simp(f,a,b,N):
    AreaSimp = 0    #initial Area
    x = np.linspace(a,b,N)
    dx = x[1]-x[0]  #change in width
    
    for i in range(0,len(x)-2,2):
        AreaSimp += dx * ((f(x) + (4 * f(x+dx)) + f(x + 2 * dx))/3)  #adding the area of more trapeziod
                                                                    #to the initial area 
    
    return AreaSimp<jupyter_output><empty_output><jupyter_text>Romberg Integration<jupyter_code>def Rom_func(f,a,b,i):
    x = b - a
    dx = x/2.**(i)
    K = x/2.**(i+1)
    M = 0.0
    for j in range(2**i):
        M += f(a + 0.5*dx + j*dx)
        
    return K*M
def Rom_int(f,a,b,tol):
    i = 0
    imax = 100
    diff = 100.0*np.fabs(tol)
    I = np.zeros(imax,dtype=float)
    I[0] = 0.5*(b-a)*(f(a) + f(b))
    i += 1
    
    while(diff>tol):
        
        I[i] = 0.5*I[i-1] + Rom_func(f,a,b,i)
        diff = np.fabs((I[i]-I[i-1])/I[i])
        
        print(i,I[i],I[i-1],diff)
        
        if(diff>tol):
            
            i+=1
            
            if(i>imax):
                print("Used All Iterations Allowed")
                raise StopIteration('Stop after reaching',i)
                
    return I[i]<jupyter_output><empty_output><jupyter_text>Lets check if we did a good job<jupyter_code>True_A= f_integral(np.pi)-f_integral(0)
print(True_A)
print("Trapezoid Method")
print(trap(f,0,np.pi,2000))

print("Simpson's Method")
print(Simp(f,0,np.pi,2000))

print("Romberg Integration")
tol = 1.0e-6
Rom_A = Rom_int(f,0,np.pi,tol)
print(Rom_A, (Rom_A - True_A)/True_A,tol)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1603456553048488, 'fraction_numerical': 0.027364378300528083, 'mean_word_length': 2.13855421686747, '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': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '409196', 'license': 'MIT', 'n_tokens_mistral': 892, 'n_tokens_neox': 854, 'n_words': 155, 'path': 'HW#5.ipynb', 'repo_name': 'AndrewS619/-Astr-119-hw-5', 'size': '2083'}