im trying to write a program that gives the integral approximation of e(x^2) between 0 and 1 based on this integral formula: Formula
i've done this code so far but it keeps giving the wrong answer (Other methods gives 1.46 as an answer, this one gives 1.006).
I think that maybe there is a problem with the two for cycles that does the Riemman sum, or that there is a problem in the way i've wrote the formula. I also tried to re-write the formula in other ways but i had no success
Any kind of help is appreciated.
import math import numpy as np def f(x): y = np.exp(x**2) return y a = float(input("¿Cual es el limite inferior? \n")) b = float(input("¿Cual es el limite superior? \n")) n = int(input("¿Cual es el numero de intervalos? ")) x = np.zeros([n+1]) y = np.zeros([n]) z = np.zeros([n]) h = (b-a)/n print (h) x[0] = a x[n] = b suma1 = 0 suma2 = 0 for i in np.arange(1,n): x[i] = x[i-1] + h suma1 = suma1 + f(x[i]) alfa = (x[i]-x[i-1])/3 for i in np.arange(0,n): y[i] = (x[i-1]+ alfa) suma2 = suma2 + f(y[i]) z[i] = y[i] + alfa int3 = ((b-a)/(8*n)) * (f(x[0])+f(x[n]) + (3*(suma2+f(z[i]))) + (2*(suma1))) print (int3) https://stackoverflow.com/questions/66739301/simpsons-rule-3-8-for-n-intervals-in-python March 22, 2021 at 09:34AM
没有评论:
发表评论