Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "metadata": {
- "name": "",
- "signature": "sha256:cac4e3dc962008166ff81aa0a428feaa75df59426425b8a0f466a7af39f93873"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "%pylab inline"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "y0 = 2.0\n",
- "y1 = 2.0\n",
- "L = 0.0\n",
- "R = 1.0\n",
- "\n",
- "def adamsMethod(N):\n",
- " h = (R - L) / N\n",
- " t0 = L\n",
- " t1 = L + h\n",
- " aT = [t0, t1]\n",
- " aY = [y0, y0]\n",
- " for n in range(1, N):\n",
- " t = t1 + n * h\n",
- " y = aY[n] + h * (3 * n * h * (1 - aY[n]) - h * (n - 1) * (1 - aY[n - 1]))\n",
- " aT.append(t)\n",
- " aY.append(y)\n",
- " return aT, aY\n",
- "\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "ts, ys = adamsMethod(50)\n",
- "def getY(t):\n",
- " return 1 + exp(-t**2)\n",
- "pylab.plot(ts, ys, 'p')\n",
- "pylab.plot(ts, [getY(t) for t in ts], 'y')"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "heading",
- "level": 4,
- "metadata": {},
- "source": [
- "\u041f\u043e\u0445\u043e\u0436\u0435, \u0447\u0442\u043e \u0441\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043a y(t) = 1 + e^(-(t^2))"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "import pandas as pd\n",
- "def calcOrderConvergence(method, N):\n",
- " K = 50\n",
- " L = 1\n",
- " R = N\n",
- " errors = []\n",
- " ns = []\n",
- " for n in range(L, N, (R - L + 1) / K):\n",
- " tS, yS = method(int(n))\n",
- " mxError = 0\n",
- " for i in range(len(tS)):\n",
- " mxError = max(mxError, abs(yS[i] - getY(tS[i])))\n",
- " errors.append(mxError)\n",
- " ns.append(n)\n",
- " ts = pd.Series(errors, index = ns)\n",
- " ts.plot(loglog = True)\n",
- " # 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",
- " ps = []\n",
- " for i in range(len(ns) - 1):\n",
- " ps.append( (log(errors[i + 1]) - log(errors[i])) / (log(ns[i + 1]) - log(ns[i])) )\n",
- " print mean(ps)\n",
- " print var(ps)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "calcOrderConvergence(adamsMethod, 500)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "heading",
- "level": 4,
- "metadata": {},
- "source": [
- "\u041c\u0435\u0442\u043e\u0434 2 \u043f\u043e\u0440\u044f\u0434\u043a\u0430 \u0441\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438"
- ]
- }
- ],
- "metadata": {}
- }
- ]
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement