Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. {
  2. "metadata": {
  3. "name": "",
  4. "signature": "sha256:cac4e3dc962008166ff81aa0a428feaa75df59426425b8a0f466a7af39f93873"
  5. },
  6. "nbformat": 3,
  7. "nbformat_minor": 0,
  8. "worksheets": [
  9. {
  10. "cells": [
  11. {
  12. "cell_type": "code",
  13. "collapsed": false,
  14. "input": [
  15. "%pylab inline"
  16. ],
  17. "language": "python",
  18. "metadata": {},
  19. "outputs": []
  20. },
  21. {
  22. "cell_type": "code",
  23. "collapsed": false,
  24. "input": [
  25. "y0 = 2.0\n",
  26. "y1 = 2.0\n",
  27. "L = 0.0\n",
  28. "R = 1.0\n",
  29. "\n",
  30. "def adamsMethod(N):\n",
  31. " h = (R - L) / N\n",
  32. " t0 = L\n",
  33. " t1 = L + h\n",
  34. " aT = [t0, t1]\n",
  35. " aY = [y0, y0]\n",
  36. " for n in range(1, N):\n",
  37. " t = t1 + n * h\n",
  38. " y = aY[n] + h * (3 * n * h * (1 - aY[n]) - h * (n - 1) * (1 - aY[n - 1]))\n",
  39. " aT.append(t)\n",
  40. " aY.append(y)\n",
  41. " return aT, aY\n",
  42. "\n"
  43. ],
  44. "language": "python",
  45. "metadata": {},
  46. "outputs": []
  47. },
  48. {
  49. "cell_type": "code",
  50. "collapsed": false,
  51. "input": [
  52. "ts, ys = adamsMethod(50)\n",
  53. "def getY(t):\n",
  54. " return 1 + exp(-t**2)\n",
  55. "pylab.plot(ts, ys, 'p')\n",
  56. "pylab.plot(ts, [getY(t) for t in ts], 'y')"
  57. ],
  58. "language": "python",
  59. "metadata": {},
  60. "outputs": []
  61. },
  62. {
  63. "cell_type": "heading",
  64. "level": 4,
  65. "metadata": {},
  66. "source": [
  67. "\u041f\u043e\u0445\u043e\u0436\u0435, \u0447\u0442\u043e \u0441\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043a y(t) = 1 + e^(-(t^2))"
  68. ]
  69. },
  70. {
  71. "cell_type": "code",
  72. "collapsed": false,
  73. "input": [
  74. "import pandas as pd\n",
  75. "def calcOrderConvergence(method, N):\n",
  76. " K = 50\n",
  77. " L = 1\n",
  78. " R = N\n",
  79. " errors = []\n",
  80. " ns = []\n",
  81. " for n in range(L, N, (R - L + 1) / K):\n",
  82. " tS, yS = method(int(n))\n",
  83. " mxError = 0\n",
  84. " for i in range(len(tS)):\n",
  85. " mxError = max(mxError, abs(yS[i] - getY(tS[i])))\n",
  86. " errors.append(mxError)\n",
  87. " ns.append(n)\n",
  88. " ts = pd.Series(errors, index = ns)\n",
  89. " ts.plot(loglog = True)\n",
  90. " # we would like to find p: errors[i] = C*(ns[i]**p) => log(errors[i])=log(C) + plog(ns[i]) => log(errors[i]) - log(errors[i - 1]) = p * (log(ns[i]) - log(ns[i - 1]))\n",
  91. " ps = []\n",
  92. " for i in range(len(ns) - 1):\n",
  93. " ps.append( (log(errors[i + 1]) - log(errors[i])) / (log(ns[i + 1]) - log(ns[i])) )\n",
  94. " print mean(ps)\n",
  95. " print var(ps)"
  96. ],
  97. "language": "python",
  98. "metadata": {},
  99. "outputs": []
  100. },
  101. {
  102. "cell_type": "code",
  103. "collapsed": false,
  104. "input": [
  105. "calcOrderConvergence(adamsMethod, 500)"
  106. ],
  107. "language": "python",
  108. "metadata": {},
  109. "outputs": []
  110. },
  111. {
  112. "cell_type": "heading",
  113. "level": 4,
  114. "metadata": {},
  115. "source": [
  116. "\u041c\u0435\u0442\u043e\u0434 2 \u043f\u043e\u0440\u044f\u0434\u043a\u0430 \u0441\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438"
  117. ]
  118. }
  119. ],
  120. "metadata": {}
  121. }
  122. ]
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement