{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 14 - Boundary value and eigen value problems"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example No. 14_01 Pg No. 467"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "B1 =  7.75\n",
      "Since B1 is less than B , let z(1) = y(1) = 4*(M2)\n",
      "B2 =  9.75\n",
      "Since B2 is larger than B ,let us have third estimate of z(1) = M3 \n",
      "B3 =  9.0\n",
      "The solution is  [2, 4.375, 9.0]\n"
     ]
    }
   ],
   "source": [
    "#Shooting Method\n",
    "\n",
    "def heun(f,x0,y0,z0,h,xf):\n",
    "    x = [x0] #\n",
    "    global y\n",
    "    y = [y0] #\n",
    "    z = [z0] #\n",
    "    n = (xf - x0)/h\n",
    "    m1=[0,0];m2=[0,0];m=[0,0]\n",
    "    for i in range(0,int(n)):\n",
    "        m1[0] = z[i] \n",
    "        m1[1] = f(x[i],y[i])\n",
    "        m2[0] = z[i] + h*m1[1]\n",
    "        m2[1] = f(x[i]+h,y[i]+h*m1[0])\n",
    "        m[0] = (m1[0] + m2[0])/2 \n",
    "        m[1] = ( m1[1] + m2[1] )/2\n",
    "        x.append(x[(i)] + h)\n",
    "        y.append(y[(i)] + h*m[0])\n",
    "        z.append(z[(i)] + h*m[1])\n",
    "    \n",
    "    B = y[int(n)]\n",
    "    return B\n",
    "\n",
    "\n",
    "def f(x,y):\n",
    "    F = 6*x\n",
    "    return F\n",
    "\n",
    "x0 = 1 #\n",
    "y0 = 2 #\n",
    "h = 0.5 #\n",
    "z0 = 2\n",
    "M1 = z0 \n",
    "xf = 2\n",
    "B = 9\n",
    "B1 = heun(f,x0,y0,z0,h,xf)\n",
    "print 'B1 = ',B1\n",
    "\n",
    "if B1 != B:\n",
    "    print 'Since B1 is less than B , let z(1) = y(1) = 4*(M2)'\n",
    "    z0 = 4\n",
    "    M2 = z0\n",
    "    B2 = heun(f,x0,y0,z0,h,xf)\n",
    "    print 'B2 = ',B2\n",
    "    if B2 != B:\n",
    "        print 'Since B2 is larger than B ,let us have third estimate of z(1) = M3 '\n",
    "        M3 = M2 - (B2 - B)*(M2 - M1)/(B2 - B1)\n",
    "        z0 = M3 \n",
    "        B3= heun(f,x0,y0,z0,h,xf)\n",
    "        print 'B3 = ',B3\n",
    "        print 'The solution is ',y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example No. 14_02 Pg No. 470"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " \n",
      " The solution is \n",
      " y1 = y(0.25) = -0.100203 \n",
      " y2 = y(0.5) = -0.137907 \n",
      " y3 = y(0.75) = -0.109079 \n",
      " \n"
     ]
    }
   ],
   "source": [
    "from numpy import array,exp,zeros,vstack,hstack,linalg\n",
    "#Finite Difference Method\n",
    "\n",
    "def d2y(x):\n",
    "    D2Y = exp(x**2)\n",
    "    return D2Y\n",
    "x_1 = 0#\n",
    "y_0 = 0 #\n",
    "y_1  = 0 #\n",
    "h = 0.25\n",
    "xf = 1\n",
    "n = (xf-x_1)/h\n",
    "A=zeros([int(n-1),int(n-1)])\n",
    "B=zeros([int(n-1),1])\n",
    "for i in range(0,int(n-1)):\n",
    "    A[i,:] = [1,  -2,   1]\n",
    "    B[i,0] = exp((x_1 + i*h)**2)*h**2\n",
    "\n",
    "A[0,0] = 0 # #since we know y0 and y4\n",
    "A[2,2] = 0 #\n",
    "A[0,:] = hstack([ A[0,1:3], [0]]) #rearranging terms\n",
    "A[2,0:3] = hstack([[ 0], A[2,0:2]])  \n",
    "C=linalg.solve(A,B)\n",
    "print ' \\n The solution is \\n y1 = y(0.25) = %f \\n y2 = y(0.5) = %f \\n y3 = y(0.75) = %f \\n '%(C[0],C[1],C[2]) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example No. 14_03 Pg No. 473"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " The roots are \n",
      " lamda1 = 4.000000 \n",
      " lamda2 = 6.000000 \n",
      " \n",
      "X1 =  Matrix([[1.00000000000000], [1]])\n",
      "X2 =  Matrix([[2.00000000000000], [1]])\n"
     ]
    }
   ],
   "source": [
    "#Eigen Vectors\n",
    "from numpy import eye\n",
    "from sympy import symbols,Matrix,det,solve\n",
    "A = [[8 ,-4],[ 2, 2 ] ]\n",
    "A=Matrix(A)\n",
    "lamd = symbols('lamd')\n",
    "p = det(A - lamd*eye(2))\n",
    "root = solve(p,lamd)\n",
    "print '\\n The roots are \\n lamda1 = %f \\n lamda2 = %f \\n '%(root[0],root[1])\n",
    "A1 = A - root[0]*eye(2)\n",
    "X1 = Matrix([[-1*A1[0,1]/A1[0,0]],[1]])\n",
    "print 'X1 = ',X1\n",
    "A2 = A - root[1]*eye(2)\n",
    "X2 = Matrix([[-1*A2[0,1]/A2[0,0]],[ 1]])\n",
    "print 'X2 = ',X2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example No. 14_04 Pg No. 474"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "A2 = \n",
      "[[-5.  0.  0.]\n",
      " [ 1. -8.  9.]\n",
      " [ 0.  4. -9.]]\n",
      "\n",
      "p2 = -11.000000\n",
      "\n",
      "\n",
      "A3 = \n",
      "[[ -6.   0.   0.]\n",
      " [  1.  -6.  27.]\n",
      " [  0.   8.  -6.]]\n",
      "\n",
      "p3 = -6.000000\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from numpy import array,shape,trace,poly\n",
    "#Fadeev - Leverrier method\n",
    "\n",
    "A = [[ -1, 0, 0],[ 1, -2, 3],[ 0, 2, -3 ]]\n",
    "A=array(A)\n",
    "r,c= shape(A)[0],shape(A)[1]\n",
    "A1 = A\n",
    "p= [trace(A1)]\n",
    "for i in range(1,r):\n",
    "    A1 = A*( A1 - p[(i-1)]*eye(3))\n",
    "    p.append(trace(A1)/(i+1))\n",
    "    print '\\nA%d = '%(i+1)\n",
    "    print A1\n",
    "    print '\\np%d = %f\\n'%((i+1),p[i])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example No. 14_05 Pg No. 476"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " Eigen vector - 1 \n",
      " for lamda1 = 0.000000 \n",
      " X1 = \n",
      "[-inf  nan  nan]\n",
      "\n",
      " Eigen vector - 2 \n",
      " for lamda2 = 0.000000 \n",
      " X2 = \n",
      "[ 0.  0.  0.]\n",
      "\n",
      " Eigen vector - 3 \n",
      " for lamda3 = 0.000000 \n",
      " X3 = \n",
      "[ 0.  0. -1.]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "np.seterr(divide='ignore', invalid='ignore')\n",
    "from __future__ import division\n",
    "from numpy.linalg import eig\n",
    "from numpy import array,diag\n",
    "#Eigen Vectors\n",
    "\n",
    "A = [[ -1, 0, 0],[1, -2, 3],[0, 2, -3]]\n",
    "A=array(A)\n",
    "evectors,evalues = eig(A)\n",
    "evectors=diag(evectors)\n",
    "for i in range(0,3):\n",
    "    print '\\n Eigen vector - %d \\n for lamda%d = %f \\n X%d = '%(i+1,i+1,evalues[0,0],i+1)\n",
    "    evectors[:,0] = evectors[:,0]/evectors[1,i]\n",
    "    print evectors[:,i]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example No. 14_06 Pg No. 478"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Iterations:\n",
      "  0      1       2      3           4            5            6             7 \n",
      "X =  [[ 0.          1.          0.8         1.          0.97560976  1.\n",
      "   0.99726027  1.        ]\n",
      " [ 1.          0.5         1.          0.92857143  1.          0.99180328\n",
      "   1.          0.99908592]\n",
      " [ 0.          0.          0.          0.          0.          0.          0.\n",
      "   0.        ]]\n",
      "\n",
      "Y =  [[None 2.0 2.0 2.8 2.8571428571428577 2.975609756097561 2.9836065573770494\n",
      "  2.9972602739726026]\n",
      " [None 1.0 2.5 2.6 2.928571428571429 2.951219512195122 2.9918032786885247\n",
      "  2.9945205479452053]\n",
      " [None 0.0 0.0 0.0 0.0 0.0 0.0 0.0]]\n"
     ]
    }
   ],
   "source": [
    "from numpy import array, zeros,expand_dims, dot, hstack\n",
    "A = array([[ 1, 2, 0],[2, 1 ,0],[0, 0, -1 ]])\n",
    "X=zeros([3,8])\n",
    "Y=zeros([3,7])\n",
    "X[:,0] =[0,1,0]\n",
    "\n",
    "for i in range(1,8):\n",
    "    Y[:,i-1] = [xx for xx in A.dot(expand_dims(X[:,i-1], axis=1))]\n",
    "    \n",
    "    X[:,i] = Y[:,i-1]/max(Y[:,i-1])\n",
    "\n",
    "print 'Iterations:\\n  0      1       2      3           4            5            6             7 '\n",
    "print 'X = ',X\n",
    "Y=hstack([[[None],[None],[None]],Y])\n",
    "print '\\nY = ',Y\n"
   ]
  }
 ],
 "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
}