Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### Resolver la siguiente integral con la Regla del Simpson:"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "$$\\int_{0}^{3}x^{2}e^{x}$$"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "=================================================\n",
- "Cálculo numérico de la regla del simpson\n",
- "=================================================\n",
- "Dame el número de segmentos : 4\n",
- "Dame el valor del límite inferior : 0\n",
- "Dame el valor del límite superior : 3\n",
- "-----------------------------------------------------------------------------------------------\n",
- " n integral et\n",
- "-----------------------------------------------------------------------------------------------\n",
- " 1 180.769832309 83.657665440\n",
- " 2 110.552516971 12.318614871\n",
- " 3 122.993435332 24.958279316\n",
- " 4 99.456833462 1.045675666\n",
- "----------------------------------------------------------------------------------------------\n"
- ]
- }
- ],
- "source": [
- "import math\n",
- "\n",
- "def f(x):\n",
- " z=(x**2)*(math.exp(x))\n",
- " return z\n",
- " \n",
- "print(\"=================================================\")\n",
- "print(\"Cálculo numérico de la regla del simpson\")\n",
- "print(\"=================================================\")\n",
- "\n",
- "\n",
- "\n",
- "\n",
- "n=int(input(\"Dame el número de segmentos : \"))\n",
- "a=float(input(\"Dame el valor del límite inferior : \"))\n",
- "b=float(input(\"Dame el valor del límite superior : \"))\n",
- "\n",
- "print(\"-----------------------------------------------------------------------------------------------\")\n",
- "print(\"{0:>10s}{1:>20s}{2:>20s}\".format(\"n\", \"integral\", \"et\"))\n",
- "print(\"-----------------------------------------------------------------------------------------------\")\n",
- "\n",
- "vv= 98.4276\n",
- "\n",
- "h = (b-a)/n\n",
- " \n",
- "\n",
- "for j in range (n): \n",
- " sum = f(a)\n",
- " h = (b-a)/(j+1)\n",
- " for i in range (1,j,2) :\n",
- " xi=a + (i*h)\n",
- " sum = sum + (4 * f(xi)) + (2 * f(xi+h))\n",
- " \n",
- " \n",
- " sum = sum + (4 * f(b-h)) + f(b)\n",
- " sum = h * sum/3\n",
- " et=abs((vv-sum)/vv)*100\n",
- " print(\"{0:10d}{1:20.9f}{2:20.9f}\".format(j+1, sum , et))\n",
- " \n",
- "\n",
- "\n",
- "print(\"----------------------------------------------------------------------------------------------\")"
- ]
- }
- ],
- "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
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement