{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 1, "metadata": { }, "output_type": "execute_result" }, { "name": "stderr", "output_type": "stream", "text": [ "WARNING: Some output was deleted.\n" ] } ], "source": [ "def picard_iteration(f, a, c, N):\n", " '''\n", " Computes the N-th Picard iterate for the IVP \n", "\n", " x' = f(t,x), x(a) = c.\n", "\n", " EXAMPLES:\n", " sage: var('x t s')\n", " (x, t, s)\n", " sage: a = 0; c = 2\n", " sage: f = lambda t,x: 1-x\n", " sage: picard_iteration(f, a, c, 0)\n", " 2\n", " sage: picard_iteration(f, a, c, 1)\n", " 2 - t\n", " sage: picard_iteration(f, a, c, 2)\n", " t^2/2 - t + 2\n", " sage: picard_iteration(f, a, c, 3)\n", " -t^3/6 + t^2/2 - t + 2\n", " sage: var('x t s')\n", " (x, t, s)\n", " sage: a = 0; c = 2\n", " sage: f = lambda t,x: (x+t)^2\n", " sage: picard_iteration(f, a, c, 0)\n", " 2\n", " sage: picard_iteration(f, a, c, 1)\n", " t^3/3 + 2*t^2 + 4*t + 2\n", " sage: picard_iteration(f, a, c, 2)\n", " t^7/63 + 2*t^6/9 + 22*t^5/15 + 16*t^4/3 + 11*t^3 + 10*t^2 + 4*t + 2\n", "\n", " '''\n", " if N == 0:\n", " return c*t**0\n", " if N == 1:\n", " #print integral(f(s,c*s**0), s, a, t)\n", " x0 = lambda t: c + integral(f(s,c*s**0), s, a, t)\n", " return expand(x0(t))\n", " for i in range(N):\n", " x_old = lambda s: picard_iteration(f, a, c, N-1).subs(t=s)\n", " #print x_old(s)\n", " x0 = lambda t: c + integral(f(s,x_old(s)), s, a, t)\n", " return expand(x0(t))\n", "v=var('x t s')\n", "a = 0; c = 4; N=40; b=8; x1=-5; x2=10;\n", "f = lambda t,x: sin(t)-2*x; assume(t>0) \n", "z=[picard_iteration(f, a, c, i) for i in range(N+1)]\n", "for i in range(N+1):\n", " show(z[i])\n", "from sage.plot.colors import rainbow\n", "c=rainbow(N+1)\n", "where = [x,-2+1.5,b]\n", "p=plot(-1/5*(cos(t)*e^(2*t) - 2*e^(2*t)*sin(t) - 21)*e^(-2*t),where,ymin=x1,ymax=x2,color='gray',gridlines=True) #SoluciĆ³n exacta.\n", "#p+=plot(z[0],where,gridlines=True)\n", "for i in range(1,N+1):\n", " p+=plot(z[i],where,ymin=x1,ymax=x2,color=c[i])\n", "\n", "show(p)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "image/png": "c5c6a0d2418e6e9c19a9734ef637be9fb3549ee9" } } ], "source": [ "N=50;b=1\n", "from sage.plot.colors import rainbow\n", "c=rainbow(N+1)\n", "where = [x,0,b]\n", "p=plot(x^0,where,color='gray',gridlines=True)\n", "for i in range(1,N+1):\n", " p+=plot(x^i,where,color=c[i])\n", "show(p)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ] }, "execution_count": 3, "metadata": { }, "output_type": "execute_result" } ], "source": [ "x = var('x')\n", "y = function('y')(x)\n", "show(desolve(diff(y,x) - exp(x+y), y))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ] }, "execution_count": 4, "metadata": { }, "output_type": "execute_result" } ], "source": [ "x = var('x')\n", "y = function('y')(x)\n", "f = desolve(diff(y,x) -exp(x+y), y, ics=[0,1]); show(f)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "-1/5*(cos(t)*e^(2*t) - 2*e^(2*t)*sin(t) - 21)*e^(-2*t)" ] }, "execution_count": 5, "metadata": { }, "output_type": "execute_result" } ], "source": [ "t = var('t')\n", "x = function('x')(t)\n", "f = desolve(diff(x,t) -sin(t) + 2*x, x, ics=[0,4]); f" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ] }, "execution_count": 6, "metadata": { }, "output_type": "execute_result" } ], "source": [ "t = var('t')\n", "x = function('x')(t)\n", "f = desolve(diff(x,t) -sin(t) + 2*x, x, ics=[0,4]); show(f)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/html": [ "" ] }, "execution_count": 7, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 7, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "execution_count": 7, "metadata": { }, "output_type": "execute_result" }, { "data": { "image/png": "e17113e1c5fe23d9e004a9b531555d51fa44417a" } } ], "source": [ "def picard_iteration(f, a, c, N):\n", " '''\n", " Computes the N-th Picard iterate for the IVP \n", "\n", " x' = f(t,x), x(a) = c.\n", "\n", " EXAMPLES:\n", " sage: var('x t s')\n", " (x, t, s)\n", " sage: a = 0; c = 2\n", " sage: f = lambda t,x: 1-x\n", " sage: picard_iteration(f, a, c, 0)\n", " 2\n", " sage: picard_iteration(f, a, c, 1)\n", " 2 - t\n", " sage: picard_iteration(f, a, c, 2)\n", " t^2/2 - t + 2\n", " sage: picard_iteration(f, a, c, 3)\n", " -t^3/6 + t^2/2 - t + 2\n", " sage: var('x t s')\n", " (x, t, s)\n", " sage: a = 0; c = 2\n", " sage: f = lambda t,x: (x+t)^2\n", " sage: picard_iteration(f, a, c, 0)\n", " 2\n", " sage: picard_iteration(f, a, c, 1)\n", " t^3/3 + 2*t^2 + 4*t + 2\n", " sage: picard_iteration(f, a, c, 2)\n", " t^7/63 + 2*t^6/9 + 22*t^5/15 + 16*t^4/3 + 11*t^3 + 10*t^2 + 4*t + 2\n", "\n", " '''\n", " if N == 0:\n", " return c*t**0\n", " if N == 1:\n", " #print integral(f(s,c*s**0), s, a, t)\n", " assume(s>0) \n", " x0 = lambda t: c + integral(f(s,c*s**0), s, a, t)\n", " return expand(x0(t))\n", " for i in range(N):\n", " x_old = lambda s: picard_iteration(f, a, c, N-1).subs(t=s)\n", " #print x_old(s)\n", " x0 = lambda t: c + integral(f(s,x_old(s)), s, a, t)\n", " return expand(x0(t))\n", "v=var('x t s')\n", "a = 0; c = 1; N=2; b=.5;\n", "f = lambda t,x: exp(x+t); assume(t>0) \n", "z=[picard_iteration(f, a, c, i) for i in range(N+1)]\n", "for i in range(N+1):\n", " show(z[i])\n", "from sage.plot.colors import rainbow\n", "c=rainbow(N+1)\n", "where = [x,-b,b]\n", "p=plot(-log(abs(-1-exp(-1)+exp(t))),where,color='gray',gridlines=True) #SoluciĆ³n exacta.\n", "#p+=plot(z[0],where,gridlines=True)\n", "for i in range(N+1):\n", " p+=plot(z[i],where,color=c[i])\n", "\n", "show(p)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ ] } ], "metadata": { "kernelspec": { "display_name": "SageMath (stable)", "language": "sagemath", "name": "sagemath" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.15" } }, "nbformat": 4, "nbformat_minor": 0 }