<jupyter_start><jupyter_text>Implement different integration schemes<jupyter_code>%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Define a function to integrate<jupyter_code>def func(x):
    a = 1.01
    b = -3.04
    c = 2.07
    return a*x**2 + b*x + c<jupyter_output><empty_output><jupyter_text>define it's integral so we know the right answer<jupyter_code>def func_integral(x):
    a = 1.01
    b = -3.04
    c = 2.07
    return (a*x**3)/3 + (b*x**2)/2 + c*x<jupyter_output><empty_output><jupyter_text>define the core of the 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>define a wrapper function to perform trapzoid method<jupyter_code>def trapezoid_method(f,a,b,N):
    # f - function to integrate
    # a - lower limit of integration
    # b - upper limit of integration
    # N - number of function evalutions to use
    
    # define x values to perform trapezoid rule
    x = np.linspace(a,b,N)
    h = x[1]-x[0]
    
    # define the value of the integral
    Fint = 0.0
    
    # perform the integral using the trapezoid method
    for i in range(0,len(x)-1,1):
        Fint += trapezoid_core(f,x[i],h)
    
    #return the answer
    return Fint<jupyter_output><empty_output><jupyter_text>define the core of Simpson's 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 a wrapper for simpson's method<jupyter_code>def simpsons_method(f,a,b,N):
    # f - function to integrate
    # a - lower limit of integration
    # b - upper limit of integration
    # N - number of function evalutions to use
    # note the number of chunks will be N-1
    # so if N is odd, then we don't need to adjust the last segment
    
    # define x values to perform trapezoid rule
    x = np.linspace(a,b,N)
    h = x[1]-x[0]
    
    # define the value of the integral
    Fint = 0.0
    
    # perform the integral using the Simpson's method
    for i in range(0,len(x)-2,2):
        Fint += simpson_core(f,x[i],h)
    
    # apply simpson's rule over the last interval if N is even
    if((N%2)==0):
        Fint += simpson_core(f,x[-2],0.5*h)
        
    #return the answer
    return Fint<jupyter_output><empty_output><jupyter_text>Check our answer<jupyter_code>Answer = func_integral(1)-func_integral(0)
print(Answer)
print("Trapezoid")
print(trapezoid_method(func,0,1,10))
print("Simpson's Method")
print(simpsons_method(func,0,1,10))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11647509578544062, 'fraction_numerical': 0.022222222222222223, 'mean_word_length': 3.6625, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '331131', 'license': 'MIT', 'n_tokens_mistral': 1014, 'n_tokens_neox': 975, 'n_words': 243, 'path': 'integration.ipynb', 'repo_name': 'QuinnZel/astr-119-session-9', 'size': '2610'}