{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 1 : Mathematical Preliminaries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 1.1,Page Number 45" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "magnitude of vector: 3.74\n", "direction of vector [ 0.267 0.535 0.802]\n" ] } ], "source": [ "import numpy as np\n", "\n", "#variable Declaration\n", "A = np.array([1,2,3]) # A is a vector\n", "\n", "#calculations\n", "l=np.linalg.norm(A) # magnitude or length of vector A\n", "a=A/l # direction of vector A\n", "\n", "#results\n", "print \"magnitude of vector:\",round(l,2)\n", "print \"direction of vector\",np.around(a,3)\n" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Example 1.2,Page Number 45" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "summation of two vectors: [ 3 2 12]\n", "subtraction of two vectors: [1 8 0]\n" ] } ], "source": [ "import numpy as np\n", "\n", "#variable Declaration\n", "A=np.array([2,5,6]) # vector A\n", "B=np.array([1,-3,6]) # vector B\n", "\n", "#calculations\n", "Sum = A+B # summation of two vectors\n", "Sub = A-B # subtraction of two vectors\n", "\n", "#results\n", "print \"summation of two vectors:\",Sum\n", "print \"subtraction of two vectors:\",Sub\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 1.3,Page Number 45" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dot product of vector A and B: 5\n" ] } ], "source": [ "import numpy as np\n", "\n", "#Variable Declaration\n", "\n", "A = np.array([1,1,2]) # vector A\n", "B = np.array([2,1,1]) # vector B\n", "\n", "#Calculations\n", "k = np.dot(A,B) # dot product of vector A and B\n", "\n", "#Results\n", "\n", "print \"dot product of vector A and B:\",k\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 1.4,Page Number 45" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cross product of vector A and B: [-3 0 3]\n" ] } ], "source": [ "import numpy as np\n", "\n", "#Variable Declaration\n", "\n", "A = np.array([2,1,2]) # vector A\n", "B = np.array([1,2,1]) # vector B\n", "\n", "#Calculations\n", "Cross = np.cross(A,B) # dot product of vector A and B\n", "\n", "#Results\n", "\n", "print \"cross product of vector A and B:\",Cross\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 1.5,Page Number 46" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dot product of vector A and B: 9\n" ] } ], "source": [ "import numpy as np\n", "\n", "#Variable Declaration\n", "\n", "A = np.array([1,3,4]) # vector A\n", "B = np.array([1,0,2]) # vector B\n", "\n", "#Calculations\n", "k = np.dot(A,B) # dot product of vector A and B\n", "\n", "#Results\n", "\n", "print \"dot product of vector A and B:\",k\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 1.9,Page Number 46" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "radius of cylinder is: 2.24 m\n", "azimuthal angle is: 63.43 degrees\n", "z coordinate is: 3 m\n" ] } ], "source": [ "from __future__ import division\n", "import math\n", "\n", "#variable declaration\n", "p = [1,2,3] # coordinates of point p\n", "x = 1 # x coordinate of P\n", "y = 2 # y coordinate of P\n", "z = 3 # z coordinate of P\n", "\n", "#Calculations\n", "rho = math.sqrt(x**2+y**2) #radius of cylinder in m\n", "phi = (math.atan(y/x))*(180/math.pi) # azimuthal angle in degrees\n", "z = 3 # in m\n", "\n", "\n", "#results\n", "print \"radius of cylinder is:\",round(rho,2),\"m\"\n", "print \"azimuthal angle is:\",round(phi,2),\"degrees\"\n", "print \"z coordinate is:\",z,\"m\"\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 1.10,Page Number 47" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cylindrical coordinates of vector A: [ 4.472 0. 1. ]\n" ] } ], "source": [ "from __future__ import division\n", "from math import cos,sin,pi,atan\n", "import numpy as np\n", "\n", "#Variable Declaration\n", "\n", "A = np.array([4,2,1]) # vector A\n", "A_x = 4 # x coordinate of P\n", "A_y = 2 # y coordinate of P\n", "A_z = 1 # z coordinate of P\n", "\n", "\n", "#calculations\n", "phi = atan(A_y/A_x) # azimuthal in radians\n", "A_rho = (A_x*cos((phi)))+(A_y*sin((phi))) # x coordinate of cylinder\n", "A_phi = (-A_x*sin(phi))+(A_y*cos(phi)) # y coordinate of cylinder\n", "A_z = 1 # z coordinate of cylinder\n", "A = [A_rho,A_phi,A_z] # cylindrical coordinates if vector A\n", "\n", "#Result\n", "print \"cylindrical coordinates of vector A:\",np.around(A,3)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 1.12,Page Number 47" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "radius of sphere is: 3.742 m\n", "angle of elevation is: 0.641 radians\n", "azimuthal angle is: 0.464 radians\n" ] } ], "source": [ "from __future__ import division\n", "from math import sqrt,acos,atan\n", "import numpy as np\n", "\n", "#Variable Declaration\n", "P = np.array([1,2,3]) # coordinates of point P in cartezian system\n", "x = 1 # x coordinate of point P in cartezian system\n", "y = 2 # y coordinate of point P in cartezian system\n", "z = 3 # z coordinate of point P in cartezian system\n", "\n", "#calculations\n", "r = sqrt(x**2+y**2+z**2) # radius of sphere in m\n", "theta = acos(z/r) # angle of elevation in degrees\n", "phi = atan(x/y) # azimuthal angle in degrees\n", "\n", "#results\n", "print \"radius of sphere is:\",round(r,3),\"m\"\n", "print \"angle of elevation is:\",round(theta,3),\"radians\"\n", "print \"azimuthal angle is:\",round(phi,3),\"radians\"\n", "\n", "\n", "# note : answer in the book is incomplete they find only one coordinate but there are three\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 1.17,Page Number 48" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "power gain is: 13.424 dB\n" ] } ], "source": [ "import numpy as np\n", "\n", "#variable declaration\n", "A_p=22 # power gain\n", "\n", "#calulation\n", "A_p_dB=10*(np.log10(A_p)) # power gain in dB\n", "\n", "#result\n", "print \"power gain is:\",round(A_p_dB,3),\"dB\"\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 1.18,Page Number 48" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "voltage gain is: 39.554 dB\n" ] } ], "source": [ "import numpy as np\n", "\n", "#variable declaration\n", "A_v=95 # voltage gain\n", "\n", "#calculation\n", "A_v_dB=20*(np.log10(A_v)) # voltage gain in dB\n", "\n", "#result\n", "print \"voltage gain is:\",round(A_v_dB,3),\"dB\"\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 1.19,Page Number 48" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "power gain is: 1.386 Np\n" ] } ], "source": [ "import numpy as np\n", "from math import sqrt\n", "\n", "#variable declaration\n", "A_p = 16 # power gain\n", "\n", "#calculations\n", "A_p_Np = np.log(sqrt(A_p)) # power gain in Np\n", "\n", "#results\n", "print \"power gain is:\",round(A_p_Np,3),\"Np\"\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 1.20,Page Number 49" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "power gain is: 3.526 Np\n" ] } ], "source": [ "import numpy as np\n", "\n", "#variable declaration\n", "A_i = 34 # current gain\n", "\n", "#calculations\n", "A_i_Np = np.log(A_i) # current gain in Nepers\n", "\n", "#result\n", "print \"power gain is:\",round(A_i_Np,3),\"Np\"\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 1.21,Page Number 49" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "magnitude of complex number A is: 4.472\n", "phase of complex number A is: 63.435 degrees\n" ] } ], "source": [ "from __future__ import division\n", "import cmath \n", "from math import sqrt,pi\n", "\n", "\n", "#variable declaration\n", "A=2+4j # complex number A\n", "\n", "#calculations\n", "magnitude = abs(A) # magnitude of complex number A\n", "phi = cmath.phase(A)*(180/pi) # phase of complex number A in degrees\n", "\n", "#results\n", "print \"magnitude of complex number A is:\",round(magnitude,3)\n", "print \"phase of complex number A is:\",round(phi,3),\"degrees\"\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 1.22,Page Number 49" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "magnitude of complex number A is: 3.162\n", "phase of complex number A in degrees: 71.565\n", "conjugate of complex no. A: (1-3j)\n" ] } ], "source": [ "from __future__ import division\n", "from math import pi\n", "import cmath\n", "\n", "#variable declaration\n", "A = 1+3j # complex no. A\n", "\n", "#calculations\n", "c = A.conjugate() # conjugate of complex no. A\n", "magnitude = abs(A) # magnitude of complex number A\n", "phi = cmath.phase(A)*(180/pi) # phase of complex number A in degrees\n", "\n", "\n", "#results\n", "print \"magnitude of complex number A is:\",round(magnitude,3)\n", "print \"phase of complex number A in degrees:\",round(phi,3)\n", "print \"conjugate of complex no. A:\",c\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 1.23,Page Number 49" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "real part of complex number A: 3.536\n", "imaginary part of complex number A: 3.536\n", "complex number A: (3.536+3.536j)\n" ] } ], "source": [ "from __future__ import division\n", "from math import cos,sin,radians\n", "import numpy as np\n", "\n", "#variable declaration\n", "rho = 5 # magnitude of the complex number A\n", "phi = 45 # phase of a complex number A in Degrees\n", "\n", "#calculations\n", "x = rho*cos(radians(phi)) # real part of complex number A\n", "y = rho*sin(radians(phi)) # imaginary part of complex number A\n", "A = complex(x,y) # complex number A\n", "\n", "#results\n", "print \"real part of complex number A:\",round(x,3)\n", "print \"imaginary part of complex number A:\",round(y,3)\n", "print \"complex number A:\",np.around(A,3)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 1.24,Page Number 49" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sum of complex numbers A_1 and A_2 is: (6+8j)\n" ] } ], "source": [ "#Variable Declaration\n", "\n", "A_1 = 2+3j # complex number A_1\n", "A_2 = 4+5j # complex number A_2\n", "\n", "\n", "#calculation\n", "A = A_1 + A_2\n", "\n", "#Result\n", "print \"sum of complex numbers A_1 and A_2 is:\",A\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 1.25,Page Number 49" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Difference of complex numbers A_1 and A_2 is: (-1+8j)\n" ] } ], "source": [ "#Variable Declaration\n", "\n", "A_1 = 6j # complex number A_1\n", "A_2 = 1-2j # complex number A_2\n", "\n", "\n", "#calculation\n", "A = A_1 - A_2\n", "\n", "#Result\n", "print \"Difference of complex numbers A_1 and A_2 is:\",A\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 1.26,Page Number 49" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Product of complex numbers A and B is: (-14.2+11.2j)\n" ] } ], "source": [ "#Variable Declaration\n", "\n", "A = 0.4 + 5j # complex number A\n", "B = 2+3j # complex number B\n", "\n", "\n", "#calculation\n", "P = A*B\n", "\n", "#Result\n", "print \"Product of complex numbers A and B is:\",P\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 1.27,Page Number 50" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Division of complex numbers A and B is: (0.154+3.231j)\n" ] } ], "source": [ "import numpy as np\n", "\n", "#Variable Declaration\n", "\n", "A = 10+6j # complex number A\n", "B = 2-3j # complex number B \n", "\n", "#calculation\n", "D = A/B\n", "\n", "#Result\n", "print \"Division of complex numbers A and B is:\",np.around(D,3)\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 1.28,Page Number 50" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The roots of the given quadratic equation are: [-1 - sqrt(3)*I, -1 + sqrt(3)*I]\n", "Root 1 = -1.0 - 1.732*I\n", "Root 2 = -1.0 + 1.732*I\n" ] } ], "source": [ "from sympy import Symbol,solve\n", "\n", "#variable Declaration\n", "\n", "x = Symbol('x')\n", "p = (x)**2 + 2*x + 4\n", "\n", "#calculations\n", "Roots = solve(p,x)\n", "\n", "\n", "#result\n", "print \"The roots of the given quadratic equation are:\",Roots\n", "\n", "for i in range(len(Roots)):\n", " print \"Root %i = %s\" % (i + 1, str(Roots[i].n(5)))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Example 1.29,Page Number 50" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The minor of 8 is: [[1 3]\n", " [4 6]]\n", "the determinant of minor of 8 is: -6.0\n", "The minor of 8 is: [[1 2]\n", " [4 5]]\n", "the determinant of minor of 9 is: -3.0\n" ] } ], "source": [ "import numpy as np\n", "\n", "#variable Declaration\n", "a = np.array([[1,2,3],[4,5,6],[7,8,9]]) # Determinant\n", "i = 2 # Third row of the determinant \n", "j = 1 # second column of the determinant\n", "\n", "#Calculations\n", "\n", "b = np.delete(np.delete(a, i, axis=0), j, axis=1) # minor of 8\n", "m = np.linalg.det(b) # determinant of minor of 8\n", "\n", "i = 2 # Third row of the determinant \n", "j = 2 # Third column of the determinant\n", "c = np.delete(np.delete(a, i, axis=0), j, axis=1) # minor of 9\n", "n = np.linalg.det(c) # determinant of minor of 9\n", "\n", "\n", "#Result\n", "print \"The minor of 8 is:\",b\n", "print \"the determinant of minor of 8 is:\",m\n", "print \"The minor of 8 is:\",c\n", "print \"the determinant of minor of 9 is:\",n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Example 1.30,Page Number 50-51" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The cofactor of 6 is: [[-3 -2]\n", " [-9 -8]]\n", "The cofactor of 5 is: [[-1 -3]\n", " [-7 -9]]\n" ] } ], "source": [ "import numpy as np\n", "\n", "#variable declaration and Calculations\n", "a = np.array([[1,3,2],[6,1,5],[7,9,8]]) # Determinant\n", "i = 1 # second row of the determinant \n", "j = 0 # first column of the determinant\n", "b = np.delete(np.delete(a, i, axis=0), j, axis=1) # minor of 6\n", "b_c = (-1)**(i+j) * b #cofactor of 6 \n", "\n", "\n", "i = 1 # second row of the determinant \n", "j = 2 # Third column of the determinant\n", "c = np.delete(np.delete(a, i, axis=0), j, axis=1) # minor of 5\n", "c_c = (-1)**(i+j) * c #cofactor of 5\n", "\n", "#Result\n", "print \"The cofactor of 6 is:\",b_c\n", "print \"The cofactor of 5 is:\",c_c\n", "\n", "\n", "## note : the answer of cofactor of 5 is wrong in the book.The sign should be negative. \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 1.31,Page Number 51" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "factorial of 4 is: 24\n", "factorial of 6 is: 720\n" ] } ], "source": [ "from math import factorial\n", "\n", "f1 = factorial(4) # factorial of 4\n", "f2 = factorial(6) # factorial of 6\n", "print \"factorial of 4 is:\",f1\n", "print \"factorial of 6 is:\",f2\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.8" } }, "nbformat": 4, "nbformat_minor": 0 }