summaryrefslogtreecommitdiff
path: root/Elements_of_Electromagnetics/chapter_3.ipynb
blob: c8dcfb121e98907e087cdbc7ee9e55b4b3f31317 (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
{
 "metadata": {
  "name": "",
  "signature": "sha256:02fdf3d71909ffefa3365717544dbd108f534a28cb15d60e71866e85ef9ac76f"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h1>Chapter 3: Vector Calculus<h1>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 3.1, Page number: 58<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "\n",
      "import scipy\n",
      "from numpy import *\n",
      "import scipy.integrate\n",
      "\n",
      "#Variable Declaration\n",
      "\n",
      "A=array([5,0,0])\n",
      "B=array([0,5,0])\n",
      "C=array([0,5,10])\n",
      "D=array([5,0,10])\n",
      "\n",
      "#Calculations\n",
      "\n",
      " #A,B,C,D in cylindrical coordinates\n",
      " \n",
      "A=array([5,0,0])\n",
      "B=array([5,scipy.pi,0])\n",
      "C=array([5,scipy.pi,10])\n",
      "D=array([5,0,10])\n",
      "\n",
      "p=5\n",
      "\n",
      "def BC(z): \n",
      " return 1\n",
      "ansa, erra = scipy.integrate.quad(BC, 0, 10)\n",
      " \n",
      "def CD(phi): \n",
      " return p\n",
      "ansb, errb = scipy.integrate.quad(CD, 0, scipy.pi/2)\n",
      "ansbb=ansb/scipy.pi         #answer in multiples of pi\n",
      "\n",
      "def ABCD(phi,z): \n",
      " return p\n",
      "ansc, errc = scipy.integrate.dblquad(lambda z , phi: ABCD(phi,z),    \n",
      "             0, scipy.pi/2, lambda z: 0, lambda z: 10) \n",
      "anscc=ansc/scipy.pi         #answer in multiples of pi\n",
      "             \n",
      "def ABO(phi,rho): \n",
      " return rho\n",
      "ansd, errd = scipy.integrate.dblquad(lambda rho , phi: ABO(phi,rho),    \n",
      "             0, scipy.pi/2, lambda rho: 0, lambda rho: 5)\n",
      "ansdd=ansd/scipy.pi         #answer in multiples of pi\n",
      "\n",
      "def AOFD(rho,z): \n",
      " return 1\n",
      "anse, erre = scipy.integrate.dblquad(lambda z , rho: AOFD(rho,z),    \n",
      "             0, 10, lambda z: 0, lambda z: 5)\n",
      "             \n",
      "def ABDCFO(z,phi,rho):\n",
      " return rho\n",
      "ansf, errf=scipy.integrate.tplquad(ABDCFO,0,5,lambda rho:0,\n",
      "  lambda rho:scipy.pi/2,lambda rho,phi:0,lambda rho,phi:10)\n",
      "ansff=ansf/scipy.pi         #answer in multiples of pi\n",
      "\n",
      "#Results\n",
      "\n",
      "print 'The distance BC =',ansa\n",
      "print 'The distance CD =',ansbb,'pi'\n",
      "print 'The surface area ABCD =',anscc,'pi'\n",
      "print 'The surface area ABO =',ansdd,'pi'\n",
      "print 'The surface area AOFD =',anse\n",
      "print 'The volume ABDCFO =',ansff,'pi'\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The distance BC = 10.0\n",
        "The distance CD = 2.5 pi\n",
        "The surface area ABCD = 25.0 pi\n",
        "The surface area ABO = 6.25 pi\n",
        "The surface area AOFD = 50.0\n",
        "The volume ABDCFO = 62.5 pi\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 3.2, Page number: 61<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "import scipy\n",
      "from numpy import *\n",
      "import scipy.integrate\n",
      "from fractions import Fraction\n",
      "\n",
      "#Variable Declaration\n",
      "\n",
      "ax=array([1,0,0])             #Unit vector along x direction\n",
      "ay=array([0,1,0])             #Unit vector along y direction\n",
      "az=array([0,0,1])             #Unit vector along z direction\n",
      "\n",
      "#Calculations\n",
      "\n",
      "def C1(x): \n",
      " return x**2\n",
      "seg1, err1 = scipy.integrate.quad(C1, 1, 0)     #segment 1\n",
      "Seg1=Fraction(seg1).limit_denominator(100)      #converting to fraction\n",
      "\n",
      "def C2(y): \n",
      " return 0\n",
      "seg2, err2 = scipy.integrate.quad(C2, 0, 1)     #segment 2\n",
      "\n",
      "def C3(x): \n",
      " return (x**2-1)\n",
      "seg3, err3 = scipy.integrate.quad(C3, 0, 1)     #segment 3\n",
      "Seg3=Fraction(seg3).limit_denominator(100)      #converting to fraction\n",
      "\n",
      "def C4(y): \n",
      " return (-y-y**2)\n",
      "seg4, err4 = scipy.integrate.quad(C4, 1, 0)     #segment 4\n",
      "Seg4=Fraction(seg4).limit_denominator(100)      #converting to fraction\n",
      "\n",
      "seg=Seg1+seg2+Seg3+Seg4                         #total circulation around path\n",
      "Seg=Fraction(seg).limit_denominator(100)        #converting to fraction\n",
      "\n",
      "#Results\n",
      "\n",
      "print 'F along segment 1 is',Seg1\n",
      "print 'F along segment 2 is',seg2\n",
      "print 'F along segment 3 is',Seg3\n",
      "print 'F along segment 4 is',Seg4\n",
      "print 'Circulation of F around the path is',Seg"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "F along segment 1 is -1/3\n",
        "F along segment 2 is 0.0\n",
        "F along segment 3 is -2/3\n",
        "F along segment 4 is 5/6\n",
        "Circulation of F around the path is -1/6\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 3.4, Page number: 68"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "import scipy\n",
      "from fractions import Fraction\n",
      "\n",
      "#Variable Declaration\n",
      "\n",
      "ax=array([1,0,0])             #Unit vector along x direction\n",
      "ay=array([0,1,0])             #Unit vector along y direction\n",
      "az=array([0,0,1])             #Unit vector along z direction\n",
      "Al=array([3,4,12])\n",
      "x=2\n",
      "y=-1\n",
      "z=0\n",
      "\n",
      "#Calculations\n",
      "\n",
      "gradW=(2*x*y**2+y*z)*ax+(2*x**2*y+x*z)*ay+(x*y)*az\n",
      "gradWl=Fraction(dot(gradW,Al)/scipy.sqrt(dot(Al,Al))).limit_denominator(1000)\n",
      "\n",
      "#Result\n",
      "\n",
      "print 'dW/dl =',gradWl\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "dW/dl = -44/13\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 3.7, Page number: 74"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "import scipy\n",
      "import scipy.integrate\n",
      "\n",
      "#Variable Declaration\n",
      "\n",
      "ap=array([1,0,0])              #Unit vector along radial direction\n",
      "az=array([0,0,1])              #Unit vector along z direction\n",
      "\n",
      "#Calculations\n",
      "\n",
      "def psi1(phi,p): \n",
      " return 10*scipy.e**(-2)*p\n",
      "psit, errt = scipy.integrate.dblquad(lambda p , phi: psi1(phi,p),    #flux through top\n",
      "             0, 2*scipy.pi, lambda p: 0, lambda p: 1) \n",
      "\n",
      "def psi2(phi,p): \n",
      " return -10*p\n",
      "psib, errb = scipy.integrate.dblquad(lambda p , phi: psi2(phi,p),    #flux through bottom\n",
      "             0, 2*scipy.pi, lambda p: 0, lambda p: 1) \n",
      "\n",
      "def psi3(phi,z): \n",
      " return 10*scipy.exp(-2*z)\n",
      "psis, errs = scipy.integrate.dblquad(lambda z , phi: psi3(phi,z),    #flux through side\n",
      "             0, scipy.pi*2, lambda z: 0, lambda z: 1) \n",
      "\n",
      "psi=psit+psib+psis    #total flux through cylinder\n",
      "\n",
      "#Results\n",
      "\n",
      "print 'The total flux through the cylinder is',psi\n",
      "print 'The total flux through cylinder using divergence theorem is also 0'\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The total flux through the cylinder is 0.0\n",
        "The total flux through cylinder using divergence theorem is also 0\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 3.9, Page number: 81<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "import scipy\n",
      "from numpy import *\n",
      "import scipy.integrate\n",
      "\n",
      "#Variable Declaration\n",
      "\n",
      "ap=array([1,0,0])              #Unit vector along radial direction\n",
      "ath=array([0,1,0])             #Unit vector along theta direction\n",
      "aph=array([0,0,1])             #Unit vector along phi direction\n",
      "\n",
      "#Calculations\n",
      "\n",
      " #segment 1\n",
      "def ab(phi): \n",
      " return 2*scipy.sin(phi)\n",
      "seg1,err1 = scipy.integrate.quad(ab,scipy.pi*60/180,scipy.pi*30/180) \n",
      "\n",
      " #segment 2\n",
      "def bc(p): \n",
      " return p*scipy.cos(scipy.pi*30/180)\n",
      "seg2,err2 = scipy.integrate.quad(bc,2,5)    \n",
      "\n",
      " #segment 3\n",
      "def cd(phi): \n",
      " return 5*scipy.sin(phi)\n",
      "seg3,err3 = scipy.integrate.quad(cd,-scipy.pi*30/180,scipy.pi*60/180)\n",
      "\n",
      " #segment 4\n",
      "def da(p): \n",
      " return p*scipy.cos(scipy.pi*60/180)\n",
      "seg4,err4 = scipy.integrate.quad(da,5,2)\n",
      "\n",
      "I1=seg1+seg2+seg3+seg4\n",
      "\n",
      " #using stoke's theorem\n",
      "\n",
      "def curlA(phi,p): \n",
      " return ((1+p)*scipy.sin(phi))\n",
      "I2, err = scipy.integrate.dblquad(lambda p , phi: curlA(phi,p),    \n",
      "             scipy.pi*30/180, scipy.pi*60/180, lambda p: 2, lambda p: 5)\n",
      "\n",
      "#Results\n",
      "\n",
      "print 'The integral calculated segment wise =',round(I1,3)\n",
      "print 'The integral calculated using Stokes Theorem =',round(I2,3)\n",
      "print 'Since I1 = I2, Stokes theorem is verified'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The integral calculated segment wise = 4.941\n",
        "The integral calculated using Stokes Theorem = 4.941\n",
        "Since I1 = I2, Stokes theorem is verified\n"
       ]
      }
     ],
     "prompt_number": 3
    }
   ],
   "metadata": {}
  }
 ]
}