<jupyter_start><jupyter_text>Numeric Integration  trapezoidal rule<jupyter_code>def trapezoidal(t):
    # Pega valores de x
    x = list(t.keys())
    # Descobre h
    h=(x[-1]-x[0])/(len(x)-1)
    tmp=0
    for i in range(1,len(x)-1):
        tmp += t[x[i]]
    tmp = 2 * tmp
    tmp += t[x[0]]
    tmp += t[x[-1]]
    resultado = (h/2)*tmp
    print((h/2),'*','[',t[x[0]],'+',tmp,'+',t[x[-1]],']')
    return resultado
# teste
t = {0:1.5,1:2.5,2:3.5,3:4.5,4:5.5}
# Regra dos trapézios
#t = {1:1.8816,1.5:1.5401,2:1.5318,2.5:1.9996,3:1.4452}
trapezoidal(t)<jupyter_output>0.5 * [ 1.5 + 28.0 + 5.5 ]<jupyter_text>1/3 Simpson simple<jupyter_code>def simpson13s(a, c, b, t=None):
    altura = (b-a)/2
    print((altura/3),'*','[',t[a],'+',t[b]*4,'+',t[c],']')
    return (altura/3)*((t[a]+(4*t[c])+t[b]))
    pass
# test
t = {0:1.5,1:2.5,2:3.5,3:4.5,4:5.5}
# 1/3 simple
#t = {1:1.8816,2:1.5318,3:1.4452}
# 1/3 composed
#t = {0:1,0.5:2,1:3,1.5:4,2:5}
#simpson13s(0,1,2,t)
simpson13s(0,2,4,t)<jupyter_output>0.6666666666666666 * [ 1.5 + 22.0 + 3.5 ]<jupyter_text>1/3 Simpson composed (TEST)<jupyter_code>def simpson13c(t=None):
    # Get x values
    x = list(t.keys())
    # Distance betwen the point 0 and 1
    altura = abs(x[0]-x[1])
    tmp=0
    fxn2=0
    fxn4=0
    for i in range(1,len(x)-1):
        # Odd
        if(i%2!=0):
            fxn4 += t[x[i]]
    fxn4 = 4 * fxn4
    for i in range(2,len(x)-2):
        # Even
        if(i%2==0):
            fxn2 += t[x[i]]
    fxn2 = 2 * fxn2
    tmp = t[x[0]]+t[x[-1]]+fxn4+fxn2
    print((altura/3),'*','[',t[x[0]]+t[x[-1]],'+',fxn4,'+',fxn2,']')
    return (altura/3)*tmp
# test
t = {0:1.5,1:2.5,2:3.5,3:4.5,4:5.5}
# 1/3 simple
#t = {1:1.8816,2:1.5318,3:1.4452}
# 1/3 compose
#t = {0:1,0.5:2,1:3,1.5:4,2:5}
simpson13c(t)<jupyter_output>0.3333333333333333 * [ 7.0 + 28.0 + 7.0 ]
StarcoderdataJupyterStructuredCleanDedup