#regladesimpson — Public Fediverse posts
Live and recent posts from across the Fediverse tagged #regladesimpson, aggregated by home.social.
-
Eso nos permite evaluar funciones arbitrarias, como por ejemplo la función sombrero entre 0 y 1:
```python
def hat(x,start=0.0,end=1.0):
return 1.0 if x >= start and x <= end else 0.0simpson_func_int(hat,0,1,100)
```
devuelve aproximadamente 0.9933 (el resultado real debería ser 1). Cambiando los límites, mientras incluyamos [0,1], debería dar resultado parecido:```python
simpson_func_int(hat,-3,3,100)
```
da 1.0101 -
Por supuesto, una forma más sencilla de usar la regla de Simpson sobre una función cualquiera es definir algo como:
```python
def simpson_func_int(f,start,stop,count):
x = linspace(start,stop,count)
y = [f(x) for x in x]
return simp_int(x,y)simpson_func_int(sin,0,pi,7)
``` -
Para calcular la integral definida usando esa función, tenemos que definir algo parecido a `linspace`:
```python
def linspace(start,stop,count):
return [x*(stop-start)/(count-1)+start for x in range(0,count)]from math import pi, sin
x = linspace(0,pi,7)
y = [sin(y) for y in x]
print(simp_int(x,y))
```El resultado es 2.0008631896735363, un 0.04% más que el valor real.