Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "#### Regla de Simpson 1/3 para la integral $$\\int_{0}^{3}x^{2}e^{x}$$\n"
  8. ]
  9. },
  10. {
  11. "cell_type": "code",
  12. "execution_count": 3,
  13. "metadata": {},
  14. "outputs": [
  15. {
  16. "name": "stdout",
  17. "output_type": "stream",
  18. "text": [
  19. "=======================================================================\n",
  20. "Calculo numérico de la regla del trapecio múltiple\n",
  21. "=======================================================================\n",
  22. "número de segmentos = 4\n",
  23. "valor del límite inferior = 0\n",
  24. "valor del límite superior = 3\n",
  25. "-----------------------------------------------------------------------------------------------\n",
  26. " n integral et\n",
  27. "-----------------------------------------------------------------------------------------------\n",
  28. " 1 180.769832309 83.657665440\n",
  29. " 2 110.552516971 12.318614871\n",
  30. " 3 122.993435332 24.958279316\n",
  31. " 4 99.456833462 1.045675666\n",
  32. "----------------------------------------------------------------------------------------------\n"
  33. ]
  34. }
  35. ],
  36. "source": [
  37. "import math\n",
  38. "\n",
  39. "def f(x):\n",
  40. " z=(x**2)*(math.exp(x))\n",
  41. " return z\n",
  42. " \n",
  43. "print(\"=======================================================================\")\n",
  44. "print(\"Calculo numérico de la regla del trapecio múltiple\")\n",
  45. "print(\"=======================================================================\")\n",
  46. "\n",
  47. "\n",
  48. "\n",
  49. "\n",
  50. "n=int(input(\"número de segmentos = \"))\n",
  51. "a=float(input(\"valor del límite inferior = \"))\n",
  52. "b=float(input(\"valor del límite superior = \"))\n",
  53. "\n",
  54. "print(\"-----------------------------------------------------------------------------------------------\")\n",
  55. "print(\"{0:>10s}{1:>20s}{2:>20s}\".format(\"n\", \"integral\", \"et\"))\n",
  56. "print(\"-----------------------------------------------------------------------------------------------\")\n",
  57. "\n",
  58. "vv= 98.4276\n",
  59. "\n",
  60. "h = (b-a)/n\n",
  61. " \n",
  62. "\n",
  63. "for k in range (n): \n",
  64. " sum = f(a)\n",
  65. " h = (b-a)/(k+1)\n",
  66. " for i in range (1,k,2) :\n",
  67. " xi=a + (i*h)\n",
  68. " sum = sum + (4 * f(xi)) + (2 * f(xi+h))\n",
  69. " \n",
  70. " \n",
  71. " sum = sum + (4 * f(b-h)) + f(b)\n",
  72. " sum = h * sum/3\n",
  73. " et=abs((vv-sum)/vv)*100\n",
  74. " print(\"{0:10d}{1:20.9f}{2:20.9f}\".format(k+1, sum , et))\n",
  75. " \n",
  76. "\n",
  77. "\n",
  78. "print(\"----------------------------------------------------------------------------------------------\")"
  79. ]
  80. },
  81. {
  82. "cell_type": "code",
  83. "execution_count": null,
  84. "metadata": {},
  85. "outputs": [],
  86. "source": []
  87. }
  88. ],
  89. "metadata": {
  90. "kernelspec": {
  91. "display_name": "Python 3",
  92. "language": "python",
  93. "name": "python3"
  94. },
  95. "language_info": {
  96. "codemirror_mode": {
  97. "name": "ipython",
  98. "version": 3
  99. },
  100. "file_extension": ".py",
  101. "mimetype": "text/x-python",
  102. "name": "python",
  103. "nbconvert_exporter": "python",
  104. "pygments_lexer": "ipython3",
  105. "version": "3.6.7"
  106. }
  107. },
  108. "nbformat": 4,
  109. "nbformat_minor": 2
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement