{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 10 : Fourier Series" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 10.1, page no. 327" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "finding the fourier series of given function\n", "enter the no of terms up to each of sin or cos terms in the expansion : 3\n", "0.158857730350203*sin(x) + 0.127086184280162*sin(2*x) + 0.0953146382101218*sin(3*x) + 0.158857730350203*cos(x) + 0.0635430921400812*cos(2*x) + 0.0317715460700406*cos(3*x) + 0.158857730350203\n" ] } ], "source": [ "import sympy,math,numpy\n", "\n", "print \"finding the fourier series of given function\"\n", "x = sympy.Symbol('x')\n", "ao = 1/math.pi*sympy.integrate(sympy.exp(-1*x),(x,0,2*math.pi))\n", "s = ao/2\n", "n = int(raw_input(\"enter the no of terms up to each of sin or cos terms in the expansion : \"))\n", "for i in range (1,n+1):\n", " ai = 1/math.pi*sympy.integrate(sympy.exp(-x)*sympy.cos(i*x),(x,0,2*math.pi))\n", " bi = 1/math.pi*sympy.integrate(sympy.exp(-x)*sympy.sin(i*x),(x,0,2*math.pi))\n", " s = s+float(ai)*sympy.cos(i*x)+float(bi)*sympy.sin(i*x)\n", "print s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 10.2, page no. 328" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "To find the fourier transform of given function\n", "Piecewise((2, s == 0), (1.0*I*exp(-1.0*I*s)/s - 1.0*I*exp(1.0*I*s)/s, True))\n", "1.57079632679490\n" ] } ], "source": [ "import numpy,math,sympy\n", "\n", "print \"To find the fourier transform of given function\"\n", "x = sympy.Symbol('x')\n", "s = sympy.Symbol('s')\n", "F = sympy.integrate(sympy.exp(1j*s*x),(x,-1,1))\n", "print F\n", "F1 = sympy.integrate(sympy.sin(x)/x,(x,0,numpy.inf))\n", "print F1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 10.3, page no. 328" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "finding the fourier series of given function\n", "enter the no of terms up to each of sin or cos terms in the expansion : 3\n", "3.0*sin(x) - 0.5*sin(2*x) + 1.0*sin(3*x) - 0.636619772367581*cos(x) - 0.0707355302630646*cos(3*x) - 0.785398163397448\n" ] } ], "source": [ "import numpy,sympy,math\n", "\n", "print \"finding the fourier series of given function\"\n", "x = sympy.Symbol('x')\n", "ao = 1/math.pi*(sympy.integrate(-1*math.pi*x**0,(x,-math.pi,0))+sympy.integrate(x,(x,0,math.pi)))\n", "s = ao/2\n", "n = int(raw_input(\"enter the no of terms up to each of sin or cos terms in the expansion : \"))\n", "for i in range (1,n+1):\n", " ai = 1/math.pi*(sympy.integrate(-1*math.pi*sympy.cos(i*x),(x,-1*math.pi,0))+sympy.integrate(x*sympy.cos(i*x),(x,0,math.pi)))\n", " bi = 1/math.pi*(sympy.integrate(-1*math.pi*x**0*sympy.sin(i*x),(x,-1*math.pi,0))+sympy.integrate(x*sympy.sin(i*x),(x,0,math.pi)))\n", " s = s+float(ai)*sympy.cos(i*x)+float(bi)*sympy.sin(i*x)\n", "print s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 10.4, page no. 329" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "finding the fourier series of given function\n", "enter the no of terms up to each of sin or cos terms in the expansion :3\n", "(exp(l) - exp(-l))/(2*l)\n" ] } ], "source": [ "import sympy,math\n", "\n", "print \"finding the fourier series of given function\"\n", "x = sympy.Symbol('x')\n", "l = sympy.Symbol('l')\n", "ao = 1/l*sympy.integrate(sympy.exp(-1*x),(x,-l,l))\n", "s = ao/2\n", "n = int(raw_input(\"enter the no of terms up to each of sin or cos terms in the expansion :\"))\n", "for i in range (1,n+1):\n", " ai = 1/l*sympy.integrate(sympy.exp(-x)*sympy.cos(i*math.pi*x/l),(x,-l,l))\n", " bi = 1/l*sympy.integrate(sympy.exp(-x)*sympy.sin(i*math.pi*x/l),(x,-l,l))\n", " s = s+float(ai)*sympy.cos(i*math.pi*x/l)+float(bi)*sympy.sin(i*math.pi*x/l)\n", "print s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 10.5, page no. 330" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "finding the fourier series of given function\n", "enter the no of terms up to each of terms in the expansion : 3\n", "2.0*sin(x) - 1.0*sin(2*x) + 0.666666666666667*sin(3*x)\n" ] } ], "source": [ "import math,sympy\n", "\n", "print \"finding the fourier series of given function\"\n", "x = sympy.Symbol('x')\n", "l = sympy.Symbol('l')\n", "s = 0\n", "n = int(raw_input(\"enter the no of terms up to each of terms in the expansion : \"))\n", "for i in range(1,n+1):\n", " bi = 2/math.pi*sympy.integrate(x*sympy.sin(i*x),(x,0,math.pi))\n", " s = s+float(bi)*sympy.sin(i*x)\n", "print s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 10.6, page no. 332" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "finding the fourier series of given function\n", "enter the no of terms up to each of sin or cos terms in the expansion :3\n", "l**2/3\n" ] } ], "source": [ "import math,sympy\n", "\n", "print \"finding the fourier series of given function\"\n", "x = sympy.Symbol('x')\n", "l = sympy.Symbol('l')\n", "ao = 2/l*sympy.integrate(x**2,(x,0,l))\n", "s = ao/2\n", "n = int(raw_input(\"enter the no of terms up to each of sin or cos terms in the expansion :\"))\n", "for i in range(1,n+1):\n", " ai = 2/l*sympy.integrate(x**2*sympy.cos(i*math.pi*x/l),(x,0,l))\n", " s = s+float(ai)*sympy.cos(i*math.pi*x/l)\n", "print s" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Example 10.7, page no. 333" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "finding the fourier series of given function\n", "enter the no of terms up to each of sin or cos terms in the expansion :2\n", "7.06789929214115e-17*cos(x) + 0.424413181578387*cos(2*x) + 0.636619772367581\n" ] } ], "source": [ "import sympy,math\n", "\n", "print \"finding the fourier series of given function\"\n", "x = sympy.Symbol('x')\n", "ao = 2/math.pi*(sympy.integrate(sympy.cos(x),(x,0,math.pi/2))+sympy.integrate(-sympy.cos(x),(x,math.pi/2,math.pi)))\n", "s = ao/2\n", "n = int(raw_input(\"enter the no of terms up to each of sin or cos terms in the expansion :\"))\n", "for i in range(1,n+1):\n", " ai = 2/math.pi*(sympy.integrate(sympy.cos(x)*sympy.cos(i*x),(x,0,math.pi/2))+sympy.integrate(-sympy.cos(x)*sympy.cos(i*x),(x,math.pi/2,math.pi)))\n", " s = s+float(ai)*sympy.cos(i*x)\n", "print s" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Example 10.8, page no. 334" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "finding the fourier series of given function\n", "enter the no of terms up to each of sin or cos terms in the expansion : 3\n", "0.810569469138702*cos(x) + 7.79634366503875e-17*cos(2*x) + 0.0900632743487446*cos(3*x)\n" ] } ], "source": [ "import sympy,math\n", "\n", "print \"finding the fourier series of given function\"\n", "x = sympy.Symbol('x')\n", "ao = 2/math.pi*(sympy.integrate((1-2*x/math.pi),(x,0,math.pi)))\n", "s = ao/2\n", "n = int(raw_input(\"enter the no of terms up to each of sin or cos terms in the expansion : \"))\n", "for i in range(1,n+1):\n", " ai = 2/math.pi*(sympy.integrate((1-2*x/math.pi)*sympy.cos(i*x),(x,0,math.pi)))\n", " s = s+float(ai)*sympy.cos(i*x)\n", "print s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 10.9, page no. 336" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "finding the fourier series of given function\n", "enter the no of terms up to each of sin or cos terms in the expansion : 3\n", "0.0\n" ] } ], "source": [ "import sympy,math\n", "\n", "print \"finding the fourier series of given function\"\n", "x = sympy.Symbol('x')\n", "l = sympy.Symbol('l')\n", "s = 0\n", "n = int(raw_input(\"enter the no of terms up to each of sin or cos terms in the expansion : \"))\n", "for i in range(1,n+1):\n", " bi = sympy.integrate(x*sympy.sin(i*math.pi*x/2),(x,0,2)) \n", " s = s+float(bi)*sympy.sin(i*math.pi*x/2)\n", "print float(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 10.10, page no. 337" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "finding the fourier series of given function\n", "enter the no of terms up to each of sin or cos terms in the expansion : 5\n", "1.0\n" ] } ], "source": [ "import sympy,math\n", "\n", "print \"finding the fourier series of given function\"\n", "x = sympy.Symbol('x')\n", "ao = 2/2*(sympy.integrate(x,(x,0,2)))\n", "s = ao/2\n", "n = int(raw_input(\"enter the no of terms up to each of sin or cos terms in the expansion : \"))\n", "for i in range(1,n+1):\n", " ai = 2/2*(sympy.integrate(x*sympy.cos(i*math.pi*x/2),(x,0,2)))\n", " s = s +float(ai)*sympy.cos(i*math.pi*x/2)\n", "print float(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 10.11, page no. 338" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "finding the fourier series of given function\n", "enter the no of terms up to each of sin or cos terms in the expansion : 2\n", "0.0\n" ] } ], "source": [ "import sympy,math\n", "\n", "print \"finding the fourier series of given function\"\n", "x = sympy.Symbol('x')\n", "ao = 0\n", "s = ao\n", "n = int(raw_input(\"enter the no of terms up to each of sin or cos terms in the expansion : \"))\n", "for i in range(1,n+1):\n", " bi = 2/1*(sympy.integrate((1/4-x)*sympy.sin(i*math.pi*x),(x,0,1/2))+sympy.integrate((x-3/4)*sympy.sin(i*math.pi*x),(x,1/2,1)))\n", " s = s+float(bi)*sympy.sin(i*math.pi*x)\n", "print float(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 10.12, page no. 339" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "finding the fourier series of given function\n", "enter the no of terms up to each of sin or cos terms in the expansion : 3\n", "-4.0*cos(x) + 0.999999999999999*cos(2*x) - 0.444444444444444*cos(3*x) + 3.28986813369645\n" ] } ], "source": [ "import sympy,math\n", "\n", "print \"finding the fourier series of given function\"\n", "x = sympy.Symbol('x')\n", "ao = 1/math.pi*sympy.integrate(x**2,(x,-math.pi,math.pi))\n", "s = ao/2\n", "n = int(raw_input(\"enter the no of terms up to each of sin or cos terms in the expansion : \"))\n", "for i in range(1,n+1):\n", " ai = 1/math.pi*sympy.integrate((x**2)*sympy.cos(i*x),(x,-math.pi,math.pi))\n", " bi = 1/math.pi*sympy.integrate((x**2)*sympy.sin(i*x),(x,-math.pi,math.pi))\n", " s = s+float(ai)*sympy.cos(i*x)+float(bi)*sympy.sin(i*x)\n", "print s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 10.13, page no. 341" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The complex form of series is summation of f(n,x) where n varies from −%inf to %inf and f(n,x) is given by : \n", "0\n" ] } ], "source": [ "import sympy,math\n", "\n", "print \"The complex form of series is summation of f(n,x) where n varies from −%inf to %inf and f(n,x) is given by : \"\n", "n = sympy.Symbol('n')\n", "x = sympy.Symbol('x')\n", "a = sympy.exp(-x)\n", "b = sympy.exp(-1j*math.pi*n*x)\n", "cn = 1/2*sympy.Integral(sympy.exp(-x)*sympy.exp(-1j*math.pi*n*x),(x,-sympy.oo,sympy.oo))\n", "fnx = float(cn)*sympy.exp(1j*n*math.pi*x) \n", "print fnx" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 10.14, page no. 342" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Practical harmoninc analysis\n", "No of sin or cos term in expansion : 5\n", "-0.522944001594253*sin(x) - 0.410968418886797*sin(2*x) + 0.0927272727272729*sin(3*x) + 0.111796006670355*sin(4*x) - 0.126146907496654*sin(5*x) - 0.373397459621556*cos(x) + 0.159090909090909*cos(2*x) - 0.258181818181819*cos(3*x) - 0.257272727272728*cos(4*x) - 0.546602540378445*cos(5*x) + 1.30454545454545\n" ] } ], "source": [ "import sympy, math, numpy\n", "\n", "print \"Practical harmoninc analysis\"\n", "x = sympy.Symbol('x')\n", "xo = numpy.array([math.pi/6, math.pi/3, math.pi/2, 2*math.pi/3, 5*math.pi/6, math.pi, 7*math.pi/6, 4*math.pi/3, \\\n", " 3*math.pi/2, 5*math.pi/3, 11*math.pi/6])\n", "yo = numpy.array([1.10, 0.30, 0.16, 1.50, 1.30, 2.16, 1.25, 1.30, 1.52, 1.76, 2.00])\n", "ao = 2*numpy.sum(yo)/len(xo)\n", "s = ao/2\n", "n = int(raw_input('No of sin or cos term in expansion : '))\n", "for i in range(1,n+1):\n", " an = 2*sum(yo*numpy.cos(i*xo))/len(yo)\n", " bn = 2*sum(yo*numpy.sin(i*xo))/len(yo)\n", " s = s+float(an)*sympy.cos(i*x)+float(bn)*sympy.sin(i*x)\n", "print s" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Example 10.15, page no. 342" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Practical harmonic analysis\n", "No of sin or cos term in expansion :5\n", "-1.61653377487451e-16*sin(6.28318530717959*x/T) + 1.5*cos(6.28318530717959*x/T) + 0.75\n", "1.5 -1.61653377487e-16\n", "Direct current :\n", "1.5\n" ] } ], "source": [ "import math,sympy,numpy\n", "\n", "print \"Practical harmonic analysis\"\n", "x = sympy.Symbol('x')\n", "T = sympy.Symbol('T')\n", "xo = numpy.array([1/6,1/3,1/2,2/3,5/6,1])\n", "yo = numpy.array([1.30,1.05,1.30,-0.88,-0.25,1.98])\n", "ao = 2*sum(yo)/len(xo)\n", "s = ao/2\n", "n = int(raw_input(\"No of sin or cos term in expansion :\"))\n", "i = 1\n", "an = 2*sum(yo*numpy.cos(i*xo*2*math.pi))/len(yo)\n", "bn = 2*sum(yo*numpy.sin(i*xo*2*math.pi))/len(yo)\n", "s = s+float(an)*sympy.cos(i*x*2*math.pi/T)+float(bn)*sympy.sin(i*x*2*math.pi/T)\n", "print s\n", "print \"Direct current :\"\n", "i = math.sqrt(an**2+bn**2)\n", "print i" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 10.16, page no. 343" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Practical harmonic analysis\n", "Input xo matrix (in factor of T):1\n", "No of sin or cos term in expansion :5\n", "-3.67394039744206e-16*sin(6.28318530717959*x/T) + 1.5*cos(6.28318530717959*x/T) + 4.5\n", "Direct current :\n", "1.5\n" ] } ], "source": [ "import math,sympy,numpy\n", "\n", "print \"Practical harmonic analysis\"\n", "x = sympy.Symbol('x')\n", "T = sympy.Symbol('T')\n", "xo = int(raw_input(\"Input xo matrix (in factor of T):\"))\n", "yo = numpy.array([1.30,1.05,1.30,-0.88,-0.25,1.98])\n", "ao = 2*sum(yo)/xo \n", "s = ao/2\n", "n = int(raw_input(\"No of sin or cos term in expansion :\"))\n", "i = 1\n", "an = 2*sum(yo*numpy.cos(i*xo*2*math.pi))/len(yo)\n", "bn = 2*sum(yo*numpy.sin(i*xo*2*math.pi))/len(yo)\n", "s = s+float(an)*sympy.cos(i*x*2*math.pi/T)+float(bn)*sympy.sin(i*x*2*math.pi/T)\n", "print s\n", "print \"Direct current :\"\n", "i = math.sqrt(an**2+bn**2)\n", "print i" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 10.17, page no. 344" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Practical harmonic analysis\n", "Input xo matrix (in factor of T):1\n", "No of sin or cos term in expansion :4\n", "-3.67394039744206e-16*sin(6.28318530717959*x/T) + 1.5*cos(6.28318530717959*x/T) + 4.5\n", "Direct current :\n", "1.5\n" ] } ], "source": [ "import math,sympy,numpy\n", "\n", "print \"Practical harmonic analysis\"\n", "x = sympy.Symbol('x')\n", "T = sympy.Symbol('T')\n", "xo = int(raw_input(\"Input xo matrix (in factor of T):\"))\n", "yo = numpy.array([1.30,1.05,1.30,-0.88,-0.25,1.98])\n", "ao = 2*sum(yo)/xo \n", "s = ao/2\n", "n = int(raw_input(\"No of sin or cos term in expansion :\"))\n", "i = 1\n", "an = 2*sum(yo*numpy.cos(i*xo*2*math.pi))/len(yo)\n", "bn = 2*sum(yo*numpy.sin(i*xo*2*math.pi))/len(yo)\n", "s = s+float(an)*sympy.cos(i*x*2*math.pi/T)+float(bn)*sympy.sin(i*x*2*math.pi/T)\n", "print s\n", "print \"Direct current :\"\n", "i = math.sqrt(an**2+bn**2)\n", "print i" ] } ], "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.11+" } }, "nbformat": 4, "nbformat_minor": 0 }