Skip to content

Instantly share code, notes, and snippets.

@CD-Ceballos
Created January 20, 2020 22:13
Show Gist options
  • Save CD-Ceballos/5354a26d9b1b34e312fb17ff384404f6 to your computer and use it in GitHub Desktop.
Save CD-Ceballos/5354a26d9b1b34e312fb17ff384404f6 to your computer and use it in GitHub Desktop.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Simpson 1/3 múltiple n = 4\n",
"\n",
"###### Se recomienta utilizar el limite de 0 a 3. Implementnado 4 segmentos\n",
"\n",
"* Ecuación: \n",
"$\\int_{}^{}x^2e^x dx$"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ingrese limite inferior: 0\n",
"Ingrese limite superior: 3\n",
"Ingrese numero de segmentos: 4\n",
"\n",
"Valor numerico: 99.45683346221226 \n",
"Valor analítico: 98.42768461593835\n",
" Et: 0.010455887998276278 %\n"
]
}
],
"source": [
"import math\n",
"\n",
"def f(x):\n",
" return (x**2)*(math.exp(x))\n",
"\n",
"def S13M(a, b, n):\n",
" vn = 0\n",
" h = (b-a)/n\n",
" for i in range(1,n-1,2):\n",
" x = a + i*h\n",
" vn = vn + 4*f(x) + (2*f(x+h))\n",
" vn = vn + 4*f(b-h) + f(b)\n",
" vn = h*vn/3\n",
" return vn\n",
"\n",
"def va(n):\n",
" return (n**2)*(math.exp(n)) - (2*(((math.exp(n))*n) - math.exp(n)))\n",
"\n",
"a = float(input(\"Ingrese limite inferior: \"))\n",
"b = float(input(\"Ingrese limite superior: \"))\n",
"n = int(input(\"Ingrese numero de segmentos: \"))\n",
"\n",
"vn = S13M(a, b, n)\n",
"va = va(b) - va(a)\n",
"et = abs((va-vn)/va)\n",
"\n",
"print(f\"\"\"\n",
"Valor numerico: {vn} \n",
"Valor analítico: {va}\n",
" Et: {et} %\"\"\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment