{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 15 - Solution of partial differential equations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example No. 15_01 Pg No. 488"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " The solution of the system is \n",
      " f1 = 75 \n",
      " f2 = 50 \n",
      " f3 = 50 \n",
      " f4 = 24 \n",
      " \n"
     ]
    }
   ],
   "source": [
    "from numpy import zeros,mat\n",
    "from numpy.linalg import solve\n",
    "#Elliptic Equations\n",
    "\n",
    "l = 15\n",
    "h = 5\n",
    "n = 1 + 15/5\n",
    "f=zeros([4,4])\n",
    "f[0,0:3]=100\n",
    "f[0:3,0]=100\n",
    "\n",
    "#At point 1 :  f2 + f3 - 4f1 + 100 + 100 = 0\n",
    "#At point 2 :  f1 + f4 - 4f2 + 100 +   0 = 0\n",
    "#At point 3 :  f1 + f4 - 4f3 + 100 +   0 = 0\n",
    "#At point 4 :  f2 + f3 - 4f4 +   0 +   0 = 0\n",
    "#\n",
    "#Final Equations are\n",
    "#         -4f1 + f2 + f3 + 0 = -200\n",
    "#           f1 - 4f2 + 0 + f4 = -100\n",
    "#           f1 + 0 - 4f3 + f4 = -100\n",
    "#           0  + f2 + f3 - 4f4 = 0\n",
    "A = mat([[ -4, 1 ,1 ,0],[1, -4, 0 ,1],[1, 0 ,-4 ,1],[0, 1, 1, -4 ]])\n",
    "B = mat([[-200],[-100],[-100],[0]])\n",
    "C = solve(A,B)\n",
    "print '\\n The solution of the system is \\n f1 = %d \\n f2 = %d \\n f3 = %d \\n f4 = %d \\n '%(C[0],C[1],C[2],C[3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example No. 15_02 Pg No. 489"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "First row of below matrix represents iteration number:\n",
      "[[  0.   1.   2.   3.   4.]\n",
      " [ 75.  75.  75.  75.  75.]\n",
      " [  0.   0.   0.   0.   0.]\n",
      " [  0.   0.   0.   0.   0.]\n",
      " [  0.  50.  50.  50.  50.]]\n"
     ]
    }
   ],
   "source": [
    "from numpy import zeros,mat\n",
    "from numpy.linalg import solve\n",
    "\n",
    "#Liebmann's Iterative method\n",
    "\n",
    "f=zeros([4,4])\n",
    "f[0,0:3]=100\n",
    "f[0:3,0]=100\n",
    "A=zeros([5,5])\n",
    "A[0,0:5] = range(0,5)\n",
    "for n in range(1,6):\n",
    "    for i in range(2,4):\n",
    "        for j in range(2,4):\n",
    "            if n == 1 and i == 2 and j == 2 :\n",
    "                f[i-1,j-1] = ( f[i,j] + f[i-2,j-2] + f[i-2,j] + f[i,j-2] )/4\n",
    "            else:\n",
    "                f[i,j] = ( f[i,j-1] + f[i-2,j-1] + f[i-1,j]+ f[i-1,j-2] )/4\n",
    "    A[1:5,n-1] = mat([f[1,1],f[1,2],f[2,1],f[2,2] ])\n",
    "\n",
    "\n",
    "print 'First row of below matrix represents iteration number:\\n',A"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example No. 15_03 Pg No. 490"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The solution is \n",
      " f1 = -5.500000 \n",
      " f2 = -10.750000 \n",
      " f3 = -3.250000 \n",
      " f4 = -5.500000 \n",
      " \n"
     ]
    }
   ],
   "source": [
    "from numpy import zeros,mat\n",
    "from numpy.linalg import solve\n",
    "\n",
    "#Poisson's Equation\n",
    "\n",
    "#D2f = 2*x**2 * y**2\n",
    "# f = 0\n",
    "# h = 1 \n",
    "#Point 1 : 0 + 0 + f2 + f3 - 4f1 = 2(1)**2 * 2**2\n",
    "#          f2 + f3 - 4f1 = 8\n",
    "#Point 2 : 0 + 0 + f1 + f4 -4f2 = 2*(2)**2*2**2\n",
    "#           f1 - 4f2 = f4 = 32\n",
    "#Point 3 : 0 + 0 + f1 + f4 - 4f4 = 2*(1**2)*1**2\n",
    "#           f1 -4f3 + f4 = 2\n",
    "#Point 4 : 0 + 0 + f2 + f3 - 4f4 = 2* 2**2 * 1**2\n",
    "#            f2 + f3 - 4f4 = 8\n",
    "#Rearranging the equations\n",
    "#            -4f1 + f2 + f3 = 8\n",
    "#             f1 - 4f2 + f4 = 32\n",
    "#             f1 - 4f3 + f4 = 2\n",
    "#              f2 + f3 - 4f4 = 8\n",
    "A = mat([[ -4, 1, 1, 0],[1, -4 ,0 ,1],[1, 0, -4, 1],[0, 1, 1, -4]])\n",
    "B = mat([[ 8],[32],[2],[8 ]])\n",
    "C = solve(A,B)\n",
    "print 'The solution is \\n f1 = %f \\n f2 = %f \\n f3 = %f \\n f4 = %f \\n '%( C[0],C[1],C[2],C[3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example No. 15_04 Pg No. 491"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " Iteration 1\n",
      " f1 = -2.000000,    f2 = -9.000000,    f3 = -2.000000,    f4 = -2.000000\n",
      "\n",
      "\n",
      " Iteration 2\n",
      " f1 = -5.000000,    f2 = -11.000000,    f3 = -3.000000,    f4 = -5.000000\n",
      "\n",
      "\n",
      " Iteration 3\n",
      " f1 = -6.000000,    f2 = -11.000000,    f3 = -4.000000,    f4 = -6.000000\n",
      "\n",
      "\n",
      " Iteration 4\n",
      " f1 = -6.000000,    f2 = -11.000000,    f3 = -4.000000,    f4 = -6.000000\n",
      "\n"
     ]
    }
   ],
   "source": [
    "#Gauss-Seidel Iteration\n",
    "\n",
    "f2 = 0\n",
    "f3 = 0\n",
    "for i in range(1,5):\n",
    "    f1 = (f2 + f3 - 8)/4\n",
    "    f4 = f1 \n",
    "    f2 = (f1 + f4 -32)/4\n",
    "    f3 = (f1 + f4 - 2)/4\n",
    "    print '\\n Iteration %d\\n f1 = %f,    f2 = %f,    f3 = %f,    f4 = %f\\n'%(i,f1,f2,f3,f4)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example No. 15_05 Pg No. 494"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The final results are \n",
      "[[   0.   150.   100.    50.     0. ]\n",
      " [   0.    50.   100.    50.     0. ]\n",
      " [   0.    50.    50.    50.     0. ]\n",
      " [   0.    25.    50.    25.     0. ]\n",
      " [   0.    25.    25.    25.     0. ]\n",
      " [   0.    12.5   25.    12.5    0. ]\n",
      " [   0.    12.5   12.5   12.5    0. ]]\n"
     ]
    }
   ],
   "source": [
    "from numpy import zeros,mat\n",
    "\n",
    "#Initial Value Problems\n",
    "\n",
    "h = 1 #\n",
    "k = 2 #\n",
    "tau = h**2/(2*k)\n",
    "f=zeros([7,5])\n",
    "for i in range(1,5):\n",
    "    f[0,i] = 50*( 4 - (i) )\n",
    "\n",
    "f[0:7,0] = 0\n",
    "f[0:7,4] = 0 \n",
    "for j in range(0,6):\n",
    "    for i in range(1,4):\n",
    "        f[j+1,i] = ( f[j,i-1] + f[j,i+1] )/2 \n",
    "print 'The final results are \\n',f"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example No. 15_06 Pg No. 497"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The final solution using crank nicholson implicit method is :\n",
      "[[   0.          150.          100.           50.            0.        ]\n",
      " [   0.           42.85714286   71.42857143   42.85714286    0.        ]\n",
      " [   0.           26.53061224   34.69387755   26.53061224    0.        ]\n",
      " [   0.           13.70262391   20.11661808   13.70262391    0.        ]\n",
      " [   0.            7.70512287   10.70387339    7.70512287    0.        ]]\n"
     ]
    }
   ],
   "source": [
    "from numpy import zeros,mat,linalg\n",
    "#Crank-Nicholson Implicit Method\n",
    "\n",
    "h = 1 #\n",
    "k = 2 #\n",
    "tau = h**2/(2*k)\n",
    "f=zeros([5,5])\n",
    "for i in range(1,4):\n",
    "    f[0,i] = 50*( 4 - (i) )\n",
    "f[0:5,0] = 0 \n",
    "f[0:5,4] = 0 \n",
    "A = mat([[4,  -1,  0 ],[-1 , 4 , -1 ],[0 , -1 , 4]])\n",
    "B=zeros([3,1])\n",
    "for j in range(0,4):\n",
    "    for i in range(1,4):\n",
    "        B[i-1,0] = f[j,i-1] + f[j,i+1]\n",
    "    \n",
    "    C = linalg.solve(A,B)\n",
    "    f[j+1,1] = C[0]\n",
    "    f[j+1,2] = C[1]\n",
    "    f[j+1,3] = C[2]\n",
    "\n",
    "print 'The final solution using crank nicholson implicit method is :\\n',f"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example No. 15_07 Pg No. 500"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The values estimated are :\n",
      "[[ 0.  4.  6.  6.  4.  0.]\n",
      " [ 0.  3.  5.  5.  3.  0.]\n",
      " [ 0.  1.  2.  2.  1.  0.]\n",
      " [ 0. -1. -2. -2. -1.  0.]\n",
      " [ 0. -3. -5. -5. -3.  0.]\n",
      " [ 0. -4. -6. -6. -4.  0.]]\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from numpy import sqrt\n",
    "#Hyperbolic Equations\n",
    "\n",
    "h = 1\n",
    "Tbyp = 4\n",
    "tau = sqrt(h**2 /4)\n",
    "r = int(1+(2.5 - 0)/tau)\n",
    "c = int(1+(5 - 0)/h)\n",
    "\n",
    "f=zeros([6,6])\n",
    "for i in range(1,int(c)-1):\n",
    "    f[0,i] = (i)*(5 - (i) )\n",
    "\n",
    "f[0:r-1,0] = 0\n",
    "f[0:r-1,c-1] = 0\n",
    "g=[]\n",
    "for i in range(1,c-1):\n",
    "    g.append(0)\n",
    "    f[1,i] = (f[0,i+1] + f[0,i-1])/2 + tau*g[i-1] \n",
    "\n",
    "for j in range(1,r-1):\n",
    "    for i in range(1,c-1):\n",
    "        f[j+1,i] = -f[j-1,i] + f[j,i+1] + f[j,i-1]\n",
    "    \n",
    "\n",
    "print 'The values estimated are :\\n',f"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "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.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}