Skip to content

Instantly share code, notes, and snippets.

@CD-Ceballos
Created January 18, 2020 17:12
Show Gist options
  • Save CD-Ceballos/ca3b2fc9382511adb75533d1c8ea2c78 to your computer and use it in GitHub Desktop.
Save CD-Ceballos/ca3b2fc9382511adb75533d1c8ea2c78 to your computer and use it in GitHub Desktop.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Integración por regla del trapecio\n",
"\n",
"**En la ecuación:**\n",
"\n",
"$\\int_{1}^{2} (x+\\frac{2}{x})^2 dx$\n",
"\n",
"**Con n = 1, 2, 3 y 4**\n"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ingrese limite inferior: 1\n",
"Ingrese limite superior: 2\n",
"Ingrese cantidad de segmentos: 4\n",
"\n",
"Valor encontrado para la funcion desde 1.0 hasta 2.0, utilizando 4 segmentos, es '8.379725056689344' \n",
"Con un error relativo verdadero porcentual de 0.5567006802721153%\n"
]
}
],
"source": [
"def f(x):\n",
" return ((x) + (2/x))**2\n",
"\n",
"def t(a,b,n):\n",
" vi=f(a) + f(b)\n",
" h=(b-a)/n\n",
" for i in range(1,n):\n",
" x = a + (h*i)\n",
" vi += 2*f(x)\n",
" return (vi*h) /2\n",
"\n",
"vv = 25/3\n",
"\n",
"True \n",
"while True:\n",
" try:\n",
" a = float(input(\"Ingrese limite inferior: \"))\n",
" b = float(input(\"Ingrese limite superior: \"))\n",
" n = int(input(\"Ingrese cantidad de segmentos: \"))\n",
" break\n",
" except ValueError:\n",
" input(\"Dato ingresado invalido. Presione enter para reintentar...\")\n",
"vc = t(a,b,n)\n",
"et = abs((vv-vc)/(vv))*100\n",
"print(f\"\"\"\n",
"Valor encontrado es '{vi}' para la funcion desde {a} hasta {b}, utilizando {n} segmentos. \n",
"Con un error relativo verdadero porcentual de {et}%\"\"\")\n"
]
}
],
"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