def integral_4(x_min, x_max, n_particoes, equacao): a = x_min h = (x_max - x_min) / n_particoes b = a + h w1 = (18 + sqrt(30)) / 36 w2 = w1 w3 = (18 - sqrt(30)) / 36 w4 = w3 a1 = sqrt((3 - 2 * sqrt(6 / 5)) / 7) a2 = -sqrt((3 - 2 * sqrt(6 / 5)) / 7) a3 = sqrt((3 + 2 * sqrt(6 / 5)) / 7) x4 = -sqrt((3 + 2 * sqrt(6 / 5)) / 7) soma_part = 0 area = 0 for i in range(n_particoes): area = (b - a) / 2 * ( w1 * calc(equacao, (b - a) / 2 * a1 + (b + a) / 2) + w2 * calc(equacao, (b - a) / 2 * a2 + (b + a) / 2) + w3 * calc(equacao, (b - a) / 2 * a3 + (b + a) / 2) + w4 * calc(equacao, (b - a) / 2 * x4 + (b + a) / 2)) soma_part += area a = b b = b + h return soma_part
def integral_3(x_min, x_max, n_particoes, equacao): a = x_min h = (x_max - x_min) / n_particoes b = a + h w1 = 5 / 9 w2 = 8 / 9 w3 = w1 a1 = -sqrt(3 / 5) a2 = 0 a3 = sqrt(3 / 5) soma_part = 0 area = 0 for i in range(n_particoes): area = (b - a) / 2 * (w1 * calc(equacao, (b - a) / 2 * a1 + (b + a) / 2) + w2 * calc(equacao, (b - a) / 2 * a2 + (b + a) / 2) + w3 * calc(equacao, (b - a) / 2 * a3 + (b + a) / 2)) soma_part += area a = b b = b + h return soma_part
def integral_2(x_min, x_max, n_particoes, equacao): a = x_min h = (x_max - x_min) / n_particoes b = a + h soma_part = 0 area = 0 for i in range(n_particoes): #area = (b-a)/2 * ( cos( (b-a)/2*-1/sqrt(3) + (b+a)/2 ) + # cos( (b-a)/2*+1/sqrt(3) + (b+a)/2 )) area = (b - a) / 2 * (calc(equacao, (b - a) / 2 * -1 / sqrt(3) + (b + a) / 2) + calc(equacao, (b - a) / 2 * +1 / sqrt(3) + (b + a) / 2)) soma_part += area a = b b = b + h return soma_part
def integral(x_inicial, x_final, equacao, tolerancia=0.000001, grau=1, filosofia='fechada'): ''' Calcula a integral pelo metodo de Newton-Cotes com base em uma tolerancia ''' delta_x = x_final - x_inicial I_anterior = 0 I = delta_x / 2 * (calc(equacao, x_inicial) + calc(equacao, x_final)) erro = abs((I - I_anterior) / I) n = 1 contador = 1 while erro > tolerancia: n = n * 2 contador += 1 I_anterior = I I = integral_n(x_inicial, x_final, n, equacao, filosofia, grau) erro = abs((I - I_anterior) / I) return I, contador
def integral_n(a, b, n, equacao, filosofia, grau=1): ''' Calcula a integral pelo metodo de Newton-Cotes com base na quantidade de iteracoes n ''' I = 0 delta_x = (b - a) / n if filosofia is 'fechada': h = delta_x / grau if filosofia is 'aberta': h = delta_x / (grau + 2) for k in range(n): x_inicial = a + k * delta_x x_final = x_inicial + delta_x if filosofia is 'fechada' and grau == 1: I += h/2 * \ ( calc(equacao, x_inicial) \ + calc(equacao, x_final)) if filosofia is 'fechada' and grau == 2: I += h/3 * \ ( calc(equacao, x_inicial) \ + 4*calc(equacao, x_inicial + h) \ + calc(equacao, x_final)) if filosofia is 'fechada' and grau == 3: I += 3*h/8 * \ ( calc(equacao, x_inicial) \ + 3*calc(equacao, x_inicial + h) \ + 3*calc(equacao, x_inicial + 2*h) \ + calc(equacao, x_final)) if filosofia is 'fechada' and grau == 4: I += 2*h/45 * \ ( 7*calc(equacao, x_inicial) \ + 32*calc(equacao, x_inicial + h) \ + 12*calc(equacao, x_inicial + 2*h) \ + 32*calc(equacao, x_inicial + 3*h) \ + 7*calc(equacao, x_final)) if filosofia is 'aberta' and grau == 1: I += delta_x/2 * \ ( calc(equacao, x_inicial + h) \ + calc(equacao, x_inicial + 2*h)) if filosofia is 'aberta' and grau == 2: I += 4*h/3 * \ ( 2*calc(equacao, x_inicial + h) \ - calc(equacao, x_inicial + 2*h) \ + 2*calc(equacao, x_inicial + 3*h)) if filosofia is 'aberta' and grau == 3: I += 5*h/24 * \ ( 11*calc(equacao, x_inicial + h) \ + calc(equacao, x_inicial + 2*h) \ + calc(equacao, x_inicial + 3*h) \ + 11*calc(equacao, x_inicial + 4*h)) if filosofia is 'aberta' and grau == 4: I += 3*h/10 * \ ( 11*calc(equacao, x_inicial + h) \ - 14*calc(equacao, x_inicial + 2*h) \ + 26*calc(equacao, x_inicial + 3*h) \ - 14*calc(equacao, x_inicial + 4*h) \ + 11*calc(equacao, x_inicial + 5*h)) return I