summaryrefslogtreecommitdiff
path: root/Numerical_Methods_by_E._Balaguruswamy/chapter14.ipynb
blob: dbc3001f5cc2ece85af5c1ceffef0928305f4127 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
{
 "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
}