From 5b5005e310733f262a679f4396f8f02dd8714bc5 Mon Sep 17 00:00:00 2001 From: Trupti Kini Date: Wed, 13 Apr 2016 23:30:24 +0600 Subject: Added(A)/Deleted(D) following books A Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER01_2.ipynb A Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER03_2.ipynb A Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER04_2.ipynb A Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER05_2.ipynb A Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER06_2.ipynb A Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER08_2.ipynb A Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER09_2.ipynb A Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER10_2.ipynb A Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER11_2.ipynb A Analog_and_Digital_Communications_by_H_P_Hsu/screenshots/Plot_ex_8.24_1.png A Analog_and_Digital_Communications_by_H_P_Hsu/screenshots/Screenshot04_2.png A Analog_and_Digital_Communications_by_H_P_Hsu/screenshots/Screenshot10_2.png A Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/Chapter9_8.ipynb A Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter1_8.ipynb A Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter2_8.ipynb A Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter3_8.ipynb A Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter4_8.ipynb A Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter6_8.ipynb A Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter7_8.ipynb A Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter8_8.ipynb A Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter_5_8.ipynb A Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/screenshots/ex1.2_3.png A Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/screenshots/ex3.7.png A Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/screenshots/ex6.5.png --- .../CHAPTER01_2.ipynb | 178 ++ .../CHAPTER03_2.ipynb | 143 ++ .../CHAPTER04_2.ipynb | 680 ++++++ .../CHAPTER05_2.ipynb | 820 ++++++++ .../CHAPTER06_2.ipynb | 380 ++++ .../CHAPTER08_2.ipynb | 1088 ++++++++++ .../CHAPTER09_2.ipynb | 552 +++++ .../CHAPTER10_2.ipynb | 567 +++++ .../CHAPTER11_2.ipynb | 986 +++++++++ .../screenshots/Plot_ex_8.24_1.png | Bin 0 -> 20463 bytes .../screenshots/Screenshot04_2.png | Bin 0 -> 83561 bytes .../screenshots/Screenshot10_2.png | Bin 0 -> 57187 bytes .../Chapter9_8.ipynb | 419 ++++ .../chapter1_8.ipynb | 625 ++++++ .../chapter2_8.ipynb | 2189 ++++++++++++++++++++ .../chapter3_8.ipynb | 1134 ++++++++++ .../chapter4_8.ipynb | 881 ++++++++ .../chapter6_8.ipynb | 1072 ++++++++++ .../chapter7_8.ipynb | 773 +++++++ .../chapter8_8.ipynb | 1090 ++++++++++ .../chapter_5_8.ipynb | 357 ++++ .../screenshots/ex1.2_3.png | Bin 0 -> 18718 bytes .../screenshots/ex3.7.png | Bin 0 -> 17630 bytes .../screenshots/ex6.5.png | Bin 0 -> 32808 bytes 24 files changed, 13934 insertions(+) create mode 100644 Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER01_2.ipynb create mode 100644 Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER03_2.ipynb create mode 100644 Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER04_2.ipynb create mode 100644 Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER05_2.ipynb create mode 100644 Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER06_2.ipynb create mode 100644 Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER08_2.ipynb create mode 100644 Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER09_2.ipynb create mode 100644 Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER10_2.ipynb create mode 100644 Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER11_2.ipynb create mode 100644 Analog_and_Digital_Communications_by_H_P_Hsu/screenshots/Plot_ex_8.24_1.png create mode 100644 Analog_and_Digital_Communications_by_H_P_Hsu/screenshots/Screenshot04_2.png create mode 100644 Analog_and_Digital_Communications_by_H_P_Hsu/screenshots/Screenshot10_2.png create mode 100644 Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/Chapter9_8.ipynb create mode 100644 Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter1_8.ipynb create mode 100644 Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter2_8.ipynb create mode 100644 Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter3_8.ipynb create mode 100644 Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter4_8.ipynb create mode 100644 Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter6_8.ipynb create mode 100644 Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter7_8.ipynb create mode 100644 Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter8_8.ipynb create mode 100644 Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter_5_8.ipynb create mode 100644 Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/screenshots/ex1.2_3.png create mode 100644 Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/screenshots/ex3.7.png create mode 100644 Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/screenshots/ex6.5.png diff --git a/Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER01_2.ipynb b/Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER01_2.ipynb new file mode 100644 index 00000000..4e39befa --- /dev/null +++ b/Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER01_2.ipynb @@ -0,0 +1,178 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:d8cd8a374e9d2a1423160b0751a9337230518753ef3ce782a87efd8053140cf4" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER01:SIGNALS AND SPECTRA" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E09 : Pg 1.13" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 1.13\n", + "# Example 1.9\n", + "# Given,\n", + "# Signal is x(t)= e^(-at) * u(t)\n", + "# unity function u(t)=1 for 0 to infinity \n", + "# therefore\n", + "import math,numpy\n", + "x=1;\n", + "# We assume 'infinity' value as 10 and the value of 'a' is 1\n", + "#t= 0:1:10;\n", + "t=numpy.linspace(0,10,num=11)\n", + "a=1;# a >0\n", + "z=((math.e)**(-a*t) * x);\n", + "y=numpy.fft.fft(z);\n", + "print 'fourier transform of x(t)=',y" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "fourier transform of x(t)= [ 1.58195029+0.j 1.33722176-0.38516024j 1.02105993-0.4033185j\n", + " 0.84862839-0.29364174j 0.76732853-0.17191927j 0.73478625-0.05628763j\n", + " 0.73478625+0.05628763j 0.76732853+0.17191927j 0.84862839+0.29364174j\n", + " 1.02105993+0.4033185j 1.33722176+0.38516024j]\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E10 : Pg 1.14" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 1.14\n", + "# Example 1.10\n", + "# Given,\n", + "# Signal is x(t)= e**|-a|t * u(t)\n", + "# unity function u(t)=1 for 0 to infinity \n", + "# therefore\n", + "import numpy,math\n", + "x=1;\n", + "# We assume 'infinity' value as 10 and the value of 'a' is 1\n", + "t= numpy.linspace(0,10,num=11);\n", + "a1=1;# For a >0\n", + "a2=-1; # For a <0\n", + "z=((math.e)**(a2*t) * x)+((math.e)**(a1*t) * x);\n", + "y=numpy.fft.fft(z);\n", + "print'fourier transform of x(t)=',y" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "fourier transform of x(t)= [ 34846.35579562 +0.j 20193.20071216+23060.75353691j\n", + " 1262.96607876+24147.94540875j -9061.39666752+17581.25336274j\n", + " -13929.23742795+10293.34682733j -15877.71059326 +3370.11697016j\n", + " -15877.71059326 -3370.11697016j -13929.23742795-10293.34682733j\n", + " -9061.39666752-17581.25336274j 1262.96607876-24147.94540875j\n", + " 20193.20071216-23060.75353691j]\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E11 : Pg 1.14" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 1.14\n", + "# Example 1.11\n", + "# (a)\n", + "# Given\n", + "# Signal is x(t) = rect(t)\n", + "# rect(t) = 1 for -a< |t| < a and 0 elsewhere\n", + "# Therefore\n", + "# We find out fourier transform of x(t)= 1 for -a< |t| < a thus,\n", + "import math,numpy\n", + "x=([1]);\n", + "a= 200; # Assume \n", + "t= numpy.linspace(-a,a,num=2*a+1); # range for fourier transform\n", + "y=numpy.fft.fft(x);\n", + "print'Fourier transform of x(t)=',y\n", + "# (b)\n", + "# Given\n", + "# Signal is x(t) = rect(t)\n", + "# rect(t) = 1 for -a/4< |t| < a/4 and 0 elsewhere\n", + "# Therefore\n", + "# We find out fourier transform of x(t)= 1 for -a/4< |t| < a/4 thus,\n", + "x=([1]);\n", + "a= 200; # Assume \n", + "t= numpy.linspace(-a/4,a/4,num=(a/2)+1);# range for fourer transform\n", + "y=numpy.fft.fft(x);\n", + "print'Fourier transform of x(t)=',y\n", + "# (c)\n", + "# Given\n", + "# Signal is x(t) = rect(t)\n", + "# rect(t) = 1 for b < |t| < b + a/2 and 0 elsewhere\n", + "# Therefore\n", + "# We find out fourier transform of x(t)= 1 for b < |t| < b+ a/2 thus,\n", + "x=([1]);\n", + "a= 200; # Assume \n", + "b=100; # Assume\n", + "t=numpy.linspace(b,(b+(a/2)),num=((a/2)+1)) ;# range for fourer transform\n", + "y=numpy.fft.fft(x);\n", + "print'Fourier transform of x(t)=',y" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Fourier transform of x(t)= [ 1.+0.j]\n", + "Fourier transform of x(t)= [ 1.+0.j]\n", + "Fourier transform of x(t)= [ 1.+0.j]\n" + ] + } + ], + "prompt_number": 1 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER03_2.ipynb b/Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER03_2.ipynb new file mode 100644 index 00000000..0f40c83a --- /dev/null +++ b/Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER03_2.ipynb @@ -0,0 +1,143 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:7baac9edb2128ede2f66bcc8790788a8e9bd4ebfa3d83c78364ed1ad1c37e3be" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER03:AMPLITUDE" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E07 : Pg 3.14" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 3.14\n", + "# Example 3.7\n", + "# Given\n", + "# (a)efficiency=((u**2)/(2+(u**2)))\n", + "u=0.5;\n", + "n=((u**2.)/(2.+(u**2.)));\n", + "np=n*100.;\n", + "print\"Efficiney: \",round(np,2),\"%\"\n", + "\n", + "# (b)nmax,\n", + "# nmax occurs at u=1;\n", + "u1=1.;\n", + "nmax=((u1**2.)/(2.+(u1**2.)));\n", + "nmaxp=nmax*100.;\n", + "print\"Efficiney max: \",nmaxp,\"%\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Efficiney: 11.11 %\n", + "Efficiney max: 33.3333333333 %\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E08 : Pg 3.14" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 3.14\n", + "# Example 3.8\n", + "# Given\n", + "# From waveform\n", + "Amax=150.;\n", + "Amin=30.;\n", + "\n", + "# (a)Modulation index\n", + "u=((Amax-Amin)/(Amax+Amin));\n", + "print\"Modulation Index: \",round(u,2)\n", + "\n", + "Ac=(Amax/(1.+u));\n", + "# (b)\n", + "# Carrier Power\n", + "Pc=(Ac**2.)/2.;\n", + "print\"Carrier Power: \",round(Pc,2),\"W\"\n", + "\n", + "# Side band Power\n", + "PSB=(Amin**2.)/2.;\n", + "print\"USB=LSB Power: \",round(PSB,2),\"W\"\n", + "\n", + "# Total Average power\n", + "Pt=Pc+(2.*PSB);\n", + "print\"Total Average Power: \",round(Pt,2),\"W\"\n", + "\n", + "# (c)Peak Envelope Power\n", + "# Given\n", + "R=60; # Ohm\n", + "PEP=(Amax**2)/(2*R);\n", + "print\"Peak Envelope Power: \",round(PEP),\"W\"\n", + "\n", + "# (d) Modulation Efficieny\n", + "n=PSB/Pt;\n", + "print\"Modulation efficieny: \",round(n,2)\n", + "\n", + "# (e) Given\n", + "# (i) u=0.2\n", + "u1=0.2;\n", + "A1=(60./u1)-Ac;\n", + "print\"A=\",round(A1)\n", + "\n", + "# (ii) u=0.8\n", + "u2=0.8;\n", + "A2=(60./u2)-Ac;\n", + "print \"A=\",round(A2)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Modulation Index: 0.67\n", + "Carrier Power: 4050.0 W\n", + "USB=LSB Power: 450.0 W\n", + "Total Average Power: 4950.0 W\n", + "Peak Envelope Power: 188.0 W\n", + "Modulation efficieny: 0.09\n", + "A= 210.0\n", + "A= -15.0\n" + ] + } + ], + "prompt_number": 2 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER04_2.ipynb b/Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER04_2.ipynb new file mode 100644 index 00000000..62bae8a2 --- /dev/null +++ b/Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER04_2.ipynb @@ -0,0 +1,680 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:a49a8ec12fc3b73c3ede4bd700b2981c396abe3970143184cc460eca3744fac1" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER04:ANGLE MODULATION" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E03 : Pg 4.9" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 4.9\n", + "# Example 4.3\n", + "# Given\n", + "fc=1.*10.**6.; # Hz\n", + "kf=5.;\n", + "mt=1.*10.**5.; # Hz\n", + "\n", + "# (a) mi(t) with fm\n", + "mi=(fc+(kf*mt));\n", + "print\"Max, Inst. Frequency with FM\",mi,\"Hz\"\n", + "import math \n", + "kp=3.;\n", + "# (b) mi2(t) with pm\n", + "mi2=fc+(mt*(kp/(2*math.pi)));\n", + "\n", + "print\"Max, Inst. Frequency with PM\",mi2,\"Hz\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Max, Inst. Frequency with FM 1500000.0 Hz\n", + "Max, Inst. Frequency with PM 1047746.48293 Hz\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E09 : Pg 4.13" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 4.13\n", + "# Example 4.9\n", + "# Given\n", + "delf=20.*10.**3.; # hz\n", + "fm=10.*10.**3.; # Hz\n", + "\n", + "B=delf/fm;\n", + "print\"Beta: \",B" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Beta: 2.0\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E13 : Pg 4.16" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 4.16\n", + "# Example 4.13\n", + "# Given\n", + "# x(t)=10cos((2*pi*10**8*t)+(200cos(2*pi*10**3*t)))\n", + "# on differentiating\n", + "# wi=2*pi*(1D+8)-4*pi*sin(2*pi*(1D+3)*t)\n", + "# Therefore\n", + "import math \n", + "delw=4.*math.pi*(1.*10.**5.);\n", + "wm=2.*math.pi*(1.*10.**3.);\n", + "B=delw/wm;\n", + "wb=2.*(B+1.)*wm;\n", + "fb=wb/2.*math.pi;\n", + "print\"Wb\",wb,\"rad/s\"\n", + "print\"Fb\",fb,\"Hz\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Wb 2525840.49349 rad/s\n", + "Fb 3967580.96924 Hz\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E14 : Pg 4.17" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 4.17\n", + "# Example 4.14\n", + "# Given\n", + "delf=100.*10.**3; # Hz\n", + "fc=20.*10.**6.; # Hz\n", + "\n", + "# As B=delf/fm;\n", + "# (a) fm1=1*10.**3hz\n", + "print'Part a'\n", + "fm1=1.*10.**3.; # Hz\n", + "B1=delf/fm1;\n", + "print'Modulation Index',B1\n", + "fb1=2.*delf;\n", + "print'Bandwidth',fb1,'Hz'\n", + "# (b) fm2=100*10.**3hz\n", + "print'\\nPart b'\n", + "fm2=100.*10.**3.; # Hz\n", + "B2=delf/fm2;\n", + "print'Modulation Index',B2\n", + "fb2=2.*(B2+1.)*fm2;\n", + "print'Bandwidth',fb2,'Hz'\n", + "# (c) fm3=500*10.**3hz\n", + "print'\\nPart c'\n", + "fm3=500.*10.**3.; # Hz\n", + "B3=delf/fm3;\n", + "print'Modulation Index',B3\n", + "fb3=2.*fm3;\n", + "print'Bandwidth',fb3,'Hz'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Part a\n", + "Modulation Index 100.0\n", + "Bandwidth 200000.0 Hz\n", + "\n", + "Part b\n", + "Modulation Index 1.0\n", + "Bandwidth 400000.0 Hz\n", + "\n", + "Part c\n", + "Modulation Index 0.2\n", + "Bandwidth 1000000.0 Hz\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E15 : Pg 4.17" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 4.17\n", + "# Example 4.15\n", + "# Given\n", + "# x(t)=10cos(wct+3sinwmt)\n", + "# Comparing with standard equation\n", + "B=3.;\n", + "fm=1.*10.**3.; # hz\n", + "fb=2.*(B+1.)*fm;\n", + "\n", + "# (a)fm is doubled\n", + "fma=2.*fm;\n", + "fba=2.*(B+1.)*fma;\n", + "print\"fb with 2fm: \",fba\n", + "\n", + "# (b)fm is one halved\n", + "fmb=fm/2.;\n", + "fbb=2.*(B+1.)*fmb;\n", + "print\"fb with 0.5fm: \",fbb" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "fb with 2fm: 16000.0\n", + "fb with 0.5fm: 4000.0\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E16 : Pg 4.18" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 4.18\n", + "# Example 4.16\n", + "# Given\n", + "# x(t)=10cos(wct+3sinwmt)\n", + "# Comparing with standard equation of fm\n", + "B=3.;\n", + "fm=1.*10.**3.; # hz\n", + "fb=2.*(B+1.)*fm;\n", + "\n", + "# B is inversaly proportional to fm\n", + "\n", + "# (a)fm is doubled\n", + "Ba=B/2.;\n", + "fma=2.*fm;\n", + "fba=2.*(Ba+1.)*fma;\n", + "print\"fb with 2fm: \",fba\n", + "\n", + "\n", + "\n", + "# (b)fm is one halved\n", + "Bb=2.*B;\n", + "fmb=fm/2.;\n", + "fbb=2.*(Bb+1.)*fmb;\n", + "print\"fb with 0.5fm: \",fbb" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "fb with 2fm: 10000.0\n", + "fb with 0.5fm: 7000.0\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E17 : Pg 4.18" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 4.18\n", + "# Example 4.17\n", + "\n", + "# Given\n", + "fm=2.*10.**3.; # Hz\n", + "delf=5.*10.**3.; # Hz\n", + "\n", + "# (a) Bandwidth of modulated signal\n", + "B=delf/fm;\n", + "\n", + "fb=2.*(B+1.)*fm;\n", + "print'Bandwidth',fb,'Hz'\n", + "\n", + "# (b)Max. frequency deviation and Bandwidth of new signal\n", + "# Given\n", + "fm1=fm-(1.*10.**3.);\n", + "delf1=3.*delf;\n", + "\n", + "B1=delf1/fm1;\n", + "\n", + "fd=B1*fm1;\n", + "print'Maximum frequency deviation',fd,'Hz'\n", + "\n", + "fb1=2.*(B1+1.)*fm1;\n", + "print'Bandwidth',fb1,'Hz'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Bandwidth 14000.0 Hz\n", + "Maximum frequency deviation 15000.0 Hz\n", + "Bandwidth 32000.0 Hz\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E18 : Pg 4.19" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 4.19\n", + "# Example 4.18\n", + "# Given\n", + "delf=75.*10.**3.; # Hz\n", + "fM=15.*10.**3.; # Hz\n", + "\n", + "D=delf/fM;\n", + "# Given formula fb=2(*10.**2)*fM\n", + "fb1=2.*10.**2.*fM;\n", + "print'BW uing formula',fb1,'Hz'\n", + "\n", + "# Carsons Rule\n", + "fb2=2.*10.**1.*fM;\n", + "print'BW uing Carsons Rule',fb2,'Hz'\n", + "\n", + "# High quality Fm radios require minimum 200kHz\n", + "# Therefore, carsons rule underestimates bandwidth" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "BW uing formula 3000000.0 Hz\n", + "BW uing Carsons Rule 300000.0 Hz\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E19 : Pg 4.19" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 4.19\n", + "# Example 4.19\n", + "# Given\n", + "fm1=50.; # Hz\n", + "fm2=15.*10.**3.; # Hz\n", + "\n", + "delf=75.*10.**3.; # Hz\n", + "\n", + "# As B=delf/fm\n", + "Bmin=delf/fm2;\n", + "Bmax=delf/fm1;\n", + "\n", + "# Let B1=0.5\n", + "B1=0.5;\n", + "n=(Bmax/B1);\n", + "print'Multiplication factor',n\n", + "\n", + "delf1=(delf/n);\n", + "print'Max allowed frequency deviation',delf1,'Hz'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Multiplication factor 3000.0\n", + "Max allowed frequency deviation 25.0 Hz\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E20 : Pg 4.20" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 4.20\n", + "# Example 4.20\n", + "# Given\n", + "f1=2.*10.**5.; # Hz\n", + "fLO=10.8*10.**6.; # Hz\n", + "delf1=25.; # Hz\n", + "n1=64.;\n", + "n2=48.;\n", + "\n", + "delf=(delf1*n1*n2);\n", + "print'Maximum frequency deviation',delf,'Hz'\n", + "\n", + "f2=n1*f1;\n", + "\n", + "f3a=f2+fLO;\n", + "f3b=f2-fLO;\n", + "\n", + "# For f3a\n", + "fca=n2*f3a;\n", + "print'Carrier frequency 1',fca,'Hz'\n", + "\n", + "# For f3b\n", + "fcb=n2*f3b;\n", + "print'Carrier frequency 2',fcb,'Hz'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Maximum frequency deviation 76800.0 Hz\n", + "Carrier frequency 1 1132800000.0 Hz\n", + "Carrier frequency 2 96000000.0 Hz\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E21 : Pg 4.20" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 4.20\n", + "# Example 4.21\n", + "# Given\n", + "delf=20.*10.**3.; # Hz\n", + "fc=200.*10.**3.; # Hz\n", + "of=96.*10.**6.; # hz\n", + "# delf=n1*n2 and as only doublers are used, n1*n2 has to be power of 2\n", + "# By trail and error, we find\n", + "n1=64.;\n", + "n2=32.;\n", + "# Output of first Multiplier\n", + "o1=n1*fc;\n", + "print'Output of first multiplier: ',o1,'Hz'\n", + "i2=of/n2;\n", + "flo=o1-i2;\n", + "print'fLO',flo,'Hz'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Output of first multiplier: 12800000.0 Hz\n", + "fLO 9800000.0 Hz\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E22 : Pg 4.20" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 4.20\n", + "# Example 4.22\n", + "# Given\n", + "B=0.2; \n", + "f1=200.*10.**3.; # Hz\n", + "fml=50.; # Hz\n", + "fmh=15.*10.**3.; # Hz\n", + "delf=75.*10.**3.; # hz\n", + "fc=108.*10.**6.; # Hz\n", + "\n", + "delf1=B*fml;\n", + "n1n2=delf/delf1;\n", + "\n", + "# Let n2=150\n", + "n2=150.;\n", + "flo=((delf*f1)-fc)/n2;\n", + "print'fLO',flo,'Hz'\n", + "\n", + "n1=n1n2/n2;\n", + "print\"n1 with n2=150:\",n1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "fLO 99280000.0 Hz\n", + "n1 with n2=150: 50.0\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E23 : Pg 4.21" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 4.21\n", + "# Example 4.23\n", + "# Given,\n", + "\n", + "delfd1=50.; # Hz\n", + "f1=120.; # Hz\n", + "\n", + "delfd2=20000.; # Hz\n", + "f2=240.; # Hz\n", + "# (a)PM\n", + "delf1=(f2/f1)*delfd1;\n", + "n1=delfd2/delf1;\n", + "print'Frequency multiplication factor in PM',n1\n", + "\n", + "# (b)FM\n", + "n2=delfd2/delfd1;\n", + "print'Frequency multiplication factor in FM',n2" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Frequency multiplication factor in PM 200.0\n", + "Frequency multiplication factor in FM 400.0\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E29 : Pg 4.25" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 4.25\n", + "# Example 4.29\n", + "# Given,\n", + "f1=108.; # MHz\n", + "f2=157.; # MHz\n", + "\n", + "# (a) Image frequency overlaps RF band\n", + "fIF=12.; # MHz\n", + "\n", + "fL01=f1-fIF;\n", + "print'fL01',fL01,'MHz'\n", + "fim1=fL01-fIF;\n", + "print'fim1',fim1,'MHz'\n", + "\n", + "fL02=f2-fIF;\n", + "print'fL02',fL02,'MHz'\n", + "fim2=fL02-fIF;\n", + "print'fim2',fim2,'MHz'\n", + "\n", + "# Clearly image and RF band overlap" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "fL01 96.0 MHz\n", + "fim1 84.0 MHz\n", + "fL02 145.0 MHz\n", + "fim2 133.0 MHz\n" + ] + } + ], + "prompt_number": 15 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER05_2.ipynb b/Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER05_2.ipynb new file mode 100644 index 00000000..d917ea6c --- /dev/null +++ b/Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER05_2.ipynb @@ -0,0 +1,820 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:d2b10e85dc4e5ecd691f760bb8b2905ecd459bb295630a40b80f0bb469c6ca0a" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER05:PULSE MODULATION" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E09 : Pg 5.26" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 5.26\n", + "# Example 5.9\n", + "# Given,\n", + "# m(t)=10cos(2000*pi*t)cos(8000*pi*t)\n", + "# or 5 cos (6000*pi*t) +5*cos(10000*pi*t)\n", + "# (a) Minimum sampling rate\n", + "# we have\n", + "fM=5000.; # Hz\n", + "fs=2.*fM;\n", + "print'Minimum sampling rate',fs,'Hz'\n", + "\n", + "# (b)bandpass sampling theoram\n", + "fu=fM;\n", + "fb=fM-3000.; # Hz\n", + "# As fu/fb is 2.5\n", + "# We have\n", + "k=2.;\n", + "fs2=(2.*fu)/k;\n", + "print'Minimum sampling rate by sampling theoram',fs2,'Hz'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Minimum sampling rate 10000.0 Hz\n", + "Minimum sampling rate by sampling theoram 5000.0 Hz\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E14 : Pg 5.31" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 5.31\n", + "# Example 5.14\n", + "\n", + "# Given,\n", + "Rb=36000.; # (b/s)\n", + "fM=3200.; # Hz\n", + "fs=2.*fM;\n", + "n=Rb/fs;\n", + "# As n should be less than Rs/fs\n", + "\n", + "nn=round(n,2)-1;\n", + "print'Binary digits',nn\n", + "\n", + "L=2**nn;\n", + "print'Quantizing level',L\n", + "\n", + "fs=Rb/nn;\n", + "print'Sampling Rate',fs,'Hz'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Binary digits 4.63\n", + "Quantizing level 24.7610398967\n", + "Sampling Rate 7775.37796976 Hz\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E15 : Pg 5.31" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 5.31\n", + "# Example 5.15\n", + "mp=1.; # Assume peak amplitude is unity\n", + "# Given\n", + "dela=0.02*mp;\n", + "\n", + "L=(mp*2)/dela;\n", + "\n", + "#for i in range(0,10):\n", + "# j=2**i;\n", + "# if(j>=L)\n", + "# L1=j;\n", + "# break;\n", + "import math \n", + "#n=math.log(L1,2);# bits per sample\n", + "n=7.;\n", + "print'Number of bits',n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Number of bits 7.0\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E18 : Pg 5.31" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 5.31\n", + "# Example 5.18\n", + "# Given,\n", + "import math \n", + "\n", + "SbyN=40.; # db\n", + "SbyN0=10.**(SbyN/10);\n", + "\n", + "# As sbyn=3L**2/2\n", + "L=math.sqrt((2.*(SbyN0))/3.);\n", + "LL=round(L);\n", + "\n", + "n=(math.log(LL))/math.log(2);\n", + "\n", + "nn=(round(n))+1; # Upper limit\n", + "\n", + "print'Binary digits',nn\n", + "LL=2.**nn;\n", + "\n", + "print'Number of levels',LL\n", + "# As SQN= 1.76+6.02(n)\n", + "SQN= 1.76+6.02*(nn);\n", + "print'Signal to quantizin ratio',SQN,'db'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Binary digits 7.0\n", + "Number of levels 128.0\n", + "Signal to quantizin ratio 43.9 db\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E19 : Pg 5.33" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 5.33\n", + "# Example 5.19\n", + "# Given,\n", + "n=16.;\n", + "Rb=44100.; # b/s\n", + "# (a) Output signal to quantizing ratio\n", + "SNQ=1.76+(6.02*n);\n", + "print'Output signal to quantizing ratio: ',SNQ,'db'\n", + "\n", + "# (b)Input Bit Rate\n", + "IBR=2.*Rb*n;\n", + "print'Input bit rate: ',IBR,'B/s'\n", + "OBR=2.*IBR;\n", + "print'Output bit rate: ',OBR,'B/s'\n", + "\n", + "# (c)Number of bits recorded\n", + "# Given, an hours time\n", + "# Therefore, time\n", + "t=60.*60.;\n", + "NBR=OBR*t;\n", + "print'Number of bits recorded: ',NBR,'Bytes'\n", + "\n", + "# (d) Dictionary\n", + "# Given\n", + "p=1500.;\n", + "c=2.;\n", + "l=100.;\n", + "w=8.;\n", + "let=6.;\n", + "b=7.;\n", + "d=p*c*l*w*let*b;\n", + "print'Number of bits required',d,'Bytes'\n", + "\n", + "x=NBR/(2.*d);\n", + "y=round(x);\n", + "print'Number of comparable books',y" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Output signal to quantizing ratio: 98.08 db\n", + "Input bit rate: 1411200.0 B/s\n", + "Output bit rate: 2822400.0 B/s\n", + "Number of bits recorded: 10160640000.0 Bytes\n", + "Number of bits required 100800000.0 Bytes\n", + "Number of comparable books 50.0\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E22 : Pg 5.35" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 5.35\n", + "# Example 5.22\n", + "# Given,\n", + "f1=200.; # Hz\n", + "f2=3300.; # Hz\n", + "fs=8000.; # Samples/s\n", + "SQN=30.; # dB\n", + "import math \n", + "# (a)Minimum number of quantizin levels and bits per sample\n", + "# From SQN=1.76+20log L\n", + "La=10.**((SQN-1.76)/20.);\n", + "L=round(La);\n", + "print'Minimum number of quantizing levels',L\n", + "n=math.log(L)/math.log(2);\n", + "nn=round(n);\n", + "print'Minimum number of bits per sample',nn\n", + "\n", + "# (b)Minimum system bandwidth\n", + "Fpcm=(nn*fs)/2.;\n", + "print'Minimum system Bandwidth',Fpcm,'Hz'\n", + "\n", + "# (c)For u=255\n", + "# SQN=20logL-10.1\n", + "La1=10.**((SQN+10.1)/20.);\n", + "L1=(round(La1))+1.; # Upper Limit\n", + "print'Minimum number of quantizing levels for u=255',L1\n", + "n1=math.log(L1)/math.log(2);\n", + "nn1=(round(n1));\n", + "print'Minimum number of bits per sample',nn1\n", + "\n", + "# Minimum system bandwidth\n", + "Fpcm1=(nn1*fs)/2.;\n", + "print'Minimum system Bandwidth',Fpcm1,'Hz'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Minimum number of quantizing levels 26.0\n", + "Minimum number of bits per sample 5.0\n", + "Minimum system Bandwidth 20000.0 Hz\n", + "Minimum number of quantizing levels for u=255 102.0\n", + "Minimum number of bits per sample 7.0\n", + "Minimum system Bandwidth 28000.0 Hz\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E25 : Pg 5.37" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 5.37\n", + "# Example 5.25\n", + "# Given,\n", + "import math \n", + "fs=32.*10.**+3.; # Hz\n", + "fm=1000.; # Hz\n", + "fM=4.*10.**3.; # Hz\n", + "# As SNR=(3*(fs**3))/(8*pi*pi*(fm**2)*fM)\n", + "SNR=(3.*(fs**3.))/(8.*math.pi*math.pi*(fm**2.)*fM);\n", + "SNRdb=(math.log(SNR))/math.log(10);\n", + "print'Output SNR',SNRdb,'dB'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Output SNR 2.49312146597 dB\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E26 : Pg 5.38" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 5.38\n", + "# Example 5.26\n", + "# Given,\n", + "n=4.;\n", + "SNQ=1.76+(6.02*n);\n", + "print'Output SNR',SNQ,'dB'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Output SNR 25.84 dB\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E27 : Pg 5.38" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 5.38\n", + "# Example 5.27\n", + "import math \n", + "# Given,\n", + "bw=3.*10.**3.; # Hz\n", + "n=3.;\n", + "fs=(n*2.*bw);\n", + "dela=250.*10.**3; # mV\n", + "fm=1000.; # Hz\n", + "# (a) Maximum amplitude\n", + "Amax=(dela*fs)/(2.*math.pi*fm);\n", + "print'Maximum Amplitude',Amax,'V'\n", + "\n", + "# (b) Output signal to quantizing ratio\n", + "SNRO=(3.*(fs**3.))/(8.*math.pi*math.pi*(fm**3.));\n", + "SNRdb=10.*(math.log(SNRO))/math.log(10);\n", + "print'Output SNR',SNRdb,'dB'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Maximum Amplitude 716197.243914 V\n", + "Output SNR 23.4554903765 dB\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E32 : Pg 5.40" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 5.40\n", + "# Example 5.32\n", + "# Given,\n", + "m1=3.*10.**3.; # Hz\n", + "m2=3.5*10.**3.; # Hz\n", + "# Since highest frequency is of m2\n", + "Sr=2.*m2; \n", + "print'Sampling Rate',Sr,'Samples/s'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sampling Rate 7000.0 Samples/s\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E33 : Pg 5.40" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 5.40\n", + "# Example 5.33\n", + "# Given,\n", + "import math \n", + "m1=3.6*10.**3.; # Hz\n", + "m2=1.2*10.**3.; # Hz\n", + "m3=m2;\n", + "m4=m2;\n", + "# (a)Nyquist rate\n", + "nr1=2.*m1;\n", + "print'Nyquist Rate of m1(t)',nr1\n", + "nr2=2.*m2;\n", + "print'Nyquist Rate of m2(t)',nr2\n", + "nr3=2.*m3;\n", + "print'Nyquist Rate of m3(t)',nr3\n", + "nr4=2.*m4;\n", + "print'Nyquist Rate of m4(t)',nr4\n", + "\n", + "# (b) Speed of commutator\n", + "c=nr1+nr2+nr3+nr4;\n", + "print'Speed of commutator',c,'samples/s'\n", + "\n", + "# (c)Output bit rate\n", + "# Given, \n", + "L=1024.;\n", + "n=math.log(L)/math.log(2);\n", + "OBR=n*c;\n", + "print'Output bit rate',OBR,'b/s'\n", + "\n", + "# (d)Minimum channel bandwidth\n", + "fB=c/2.;\n", + "print'Minimum Channel Bandwidth',fB,'Hz'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Nyquist Rate of m1(t) 7200.0\n", + "Nyquist Rate of m2(t) 2400.0\n", + "Nyquist Rate of m3(t) 2400.0\n", + "Nyquist Rate of m4(t) 2400.0\n", + "Speed of commutator 14400.0 samples/s\n", + "Output bit rate 144000.0 b/s\n", + "Minimum Channel Bandwidth 7200.0 Hz\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E34 : Pg 5.41" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 5.41\n", + "# Example 5.34\n", + "\n", + "# Given,\n", + "fs=8000.; # Hz\n", + "m=24.;\n", + "n=8.;\n", + "# (a) Duration of each bit\n", + "t1=1./fs;\n", + "t2=(m*n)+1.; # Extra bit for synchronization\n", + "Tb=t1/t2;\n", + "print'Duration of each bit',Tb,'seconds'\n", + "\n", + "# (b) Transmission Rate\n", + "Rb=1./Tb;\n", + "print'Transmission Rate',Rb,'b/s'\n", + "\n", + "# (c)Minimum transmission bandwidth\n", + "fT1=1./(2.*Tb);\n", + "print'Minimum transmission bandwidth',fT1,'Hz'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Duration of each bit 6.47668393782e-07 seconds\n", + "Transmission Rate 1544000.0 b/s\n", + "Minimum transmission bandwidth 772000.0 Hz\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E35 : Pg 5.42" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 5.42\n", + "# Example 5.35\n", + "# Given,\n", + "n=24.;\n", + "f=3.4*10.**3; # Hz\n", + "ts=1.*10**6.; # Second\n", + "# (a) Spacing between succesive multiplexed pulses\n", + "fs=8000.; # Samples per second\n", + "t1=1./fs;\n", + "t2=n+1.; # One synchronizing bit\n", + "Tb=t1/t2;\n", + "# Actual Tb, as actual duration of each pulse is 1us\n", + "ATb=Tb-ts;\n", + "print'Spacing between succesive multiplexed pulses',ATb,'Seconds'\n", + "# (b) Nyquist Rate of Sampling\n", + "f1=2.*f;\n", + "T=1./f1; # Seconds\n", + "Tb1=T/t2;\n", + "ATb1=Tb1-ts;\n", + "print'Spacing between succesive multiplexed pulses using Nyquist rate of sampling',ATb1,'Seconds'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Spacing between succesive multiplexed pulses -999999.999995 Seconds\n", + "Spacing between succesive multiplexed pulses using Nyquist rate of sampling -999999.999994 Seconds\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E37 : Pg 5.43" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 5.43\n", + "# Example 5.37\n", + "# Given,\n", + "bw=3.5*10.**3.; # Hz\n", + "# Roll off factor\n", + "a=0.25;\n", + "Rb=(2.*bw)/(1.+a);\n", + "print'Data Rate',Rb,'b/s'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Data Rate 5600.0 b/s\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E38 : Pg 5.43" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 5.43\n", + "# Example 5.38\n", + "# Given,\n", + "fB=75.*10.**3.; # Hz\n", + "Rb=0.1*10.**6.; # B/s\n", + "Tb=1./Rb;\n", + "a=(2.*fB*Tb)-1.;\n", + "print'Roll off factor',a" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Roll off factor 0.5\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E39 : Pg 5.43" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 5.43\n", + "# Example 5.39\n", + "# Given,\n", + "import math \n", + "m=8.;\n", + "fM=2.*10.**3.; # Hz\n", + "a=0.2;\n", + "# Here we choose L=128;\n", + "L=128.;\n", + "n=math.log(L)/math.log(2);\n", + "Sr=2.*fM;\n", + "fs=1.25*Sr;\n", + "\n", + "# For n tdm signals\n", + "x=m*fs;\n", + "\n", + "# Resultant bit rate\n", + "br=7.*x;\n", + "\n", + "# Minimum Transmission bandwidth\n", + "fB=((1.+a)*br)/2.;\n", + "print'Minimum Transmission bandwidth',fB,'Hz'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Minimum Transmission bandwidth 168000.0 Hz\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E40 : Pg 5.44" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 5.44\n", + "# Example 5.40\n", + "# Given,\n", + "import math \n", + "M=16.;\n", + "sr=40000.;\n", + "a=0.3;\n", + "# (a) Bit Rate\n", + "n=math.log(M)/math.log(2);\n", + "br=sr*n;\n", + "print'Bit Rate: ',br,'b/s'\n", + "\n", + "# (b)\n", + "# As 2*fB=(1+a)*R/log2M\n", + "# Given \n", + "bw=110.*10.**3.; # Hz # =2*fB\n", + "\n", + "M=2.**(((1.+a)*br)/bw);\n", + "MM=round(M);\n", + "print'Value of M',MM\n", + "\n", + "# (c)Band Rate\n", + "band=br/(math.log(n))/math.log(2);\n", + "print'Band Rate',band,'Symbols/s'\n", + "\n", + "# (d) Spectral efficiency\n", + "BT=((1.+a)*br)/2.;\n", + "Eff=br/BT;\n", + "print'Spectral efficiency',Eff,'b/s Hz'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Bit Rate: 160000.0 b/s\n", + "Value of M 4.0\n", + "Band Rate 166509.51848 Symbols/s\n", + "Spectral efficiency 1.53846153846 b/s Hz\n" + ] + } + ], + "prompt_number": 17 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER06_2.ipynb b/Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER06_2.ipynb new file mode 100644 index 00000000..3fe5c8b4 --- /dev/null +++ b/Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER06_2.ipynb @@ -0,0 +1,380 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:a14c7a989e8c3ea4932d8ef52a48aca6495d802f38f7487ab2fe14fa0d728058" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER06:PROBABILITY AND RANDOM VARIABLES" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E07 : Pg 6.16" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 6.16\n", + "# Example 6.7\n", + "# Given\n", + "# Pdot=2*Pdash and Pdot+Pdash=1\n", + "# Therfore, on solving using linear equations\n", + "import math \n", + "#a=([1, -2],[1, 1]);\n", + "##c=([0],[1]);\n", + "#b=1/(a)*c;\n", + "Pdash=0.3333333;#b(1,1);\n", + "Pdot=0.6666667;#b(2,1);\n", + "print'Pdot:',Pdot\n", + "print'Pdash:',Pdash" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Pdot: 0.6666667\n", + "Pdash: 0.3333333\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E14 : Pg 6.18" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 6.18\n", + "# Example 6.14\n", + "# Given\n", + "p=0.1;\n", + "q=0.2;\n", + "Pm0=0.5;\n", + "Pr1bym0=p;\n", + "Pr0bym1=q;\n", + "# (a) Find Pr0 and Pr1\n", + "Pm1=1-Pm0;\n", + "Pr0bym0=1-Pr1bym0;\n", + "Pr1bym1=1-Pr0bym1;\n", + "\n", + "# By formula\n", + "# P(r0)=(P(r0/m0)*P(m0))+(P(r0/m1)*P(m1);\n", + "# P(r1)=(P(r1/m0)*P(m0))+(P(r1/m1)*P(m1);\n", + "Pr0=(Pr0bym0*Pm0)+(Pr0bym1*Pm1);\n", + "Pr1=(Pr1bym0*Pm0)+(Pr1bym1*Pm1);\n", + "print'P(r0):',Pr0\n", + "print'P(r1):',Pr1\n", + "# (b)P(m0/r0)\n", + "# Using Bayes Rule\n", + "# P(m0/r0)=(P(m0)*P(r0/m0)/P(r0))\n", + "Pm0byr0=(Pm0*Pr0bym0)/Pr0;\n", + "print'P(m0/r0):',Pm0byr0\n", + "# (c)P(m1/r1)\n", + "# Using Bayes Rule\n", + "# P(m1/r1)=(P(m1)*P(r1/m1)/P(r1))\n", + "Pm1byr1=(Pm1*Pr1bym1)/Pr1;\n", + "print'P(m1/r1):',Pm1byr1\n", + "\n", + "# (d)Probabilty error\n", + "# As Pe=(P(r1/m0)*P(m0))+(P(r0/m1)*P(m1))\n", + "Pe=(Pr1bym0*Pm0)+(Pr0bym1*Pm1);\n", + "print'Probability error:',Pe\n", + "\n", + "# (e)Probabilty that is transmitted correctly\n", + "# As Pc=(P(r0/m0)*P(m0))+(Pr1bym1*Pm1)\n", + "\n", + "Pc=(Pr0bym0*Pm0)+(Pr1bym1*Pm1);\n", + "print'Probabilty that is transmitted correctly:',Pc" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "P(r0): 0.55\n", + "P(r1): 0.45\n", + "P(m0/r0): 0.818181818182\n", + "P(m1/r1): 0.888888888889\n", + "Probability error: 0.15\n", + "Probabilty that is transmitted correctly: 0.85\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E16 : Pg 6.21" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 6.21\n", + "# Example 6.16\n", + "# Given\n", + "p=0.4;\n", + "Pp=p;\n", + "q=0.3;\n", + "Pn=q;\n", + "a=1.; # i start value\n", + "b=6.; # i end value\n", + "# (a)Probabilty that all pulses are positive\n", + "s=1.;\n", + "for i in range(1,6):\n", + " s=s*Pp;\n", + "print'Probabilty that all pulses are positive:',s\n", + "\n", + "# (b)Pulses are positive ,positive, positive, zero,zero,negative\n", + "Pz=1.-(p+q);\n", + "s1=(Pp**3.)*(Pz**2.)*Pn;\n", + "print'Probabilty that all pulses are positive ,positive, positive, zero,zero,negative:',s1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Probabilty that all pulses are positive: 0.01024\n", + "Probabilty that all pulses are positive ,positive, positive, zero,zero,negative: 0.001728\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E17 : Pg 6.21" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 6.21\n", + "# Example 6.17\n", + "# Given\n", + "import math \n", + "P1=0.6;\n", + "P0=0.4;\n", + "n=5.; # Five digit sequence\n", + "j=2.; # two outcomes 0 and 1\n", + "\n", + "# (a)1,1,0,0,0\n", + "xf=(math.factorial(n))/((math.factorial(j))*(math.factorial(n-j)));\n", + "s=xf*(P1**j)*(P0**(n-j));\n", + "print'Probability for 1,1,0,0,0:',s\n", + "\n", + "# (b)atleast 3 1's\n", + "# P(X>=3)=1-P(X<=2)\n", + "# Here y=1-x\n", + "x=0;\n", + "for k in range(0,2):\n", + " f=(math.factorial(n))/((math.factorial(k))*(math.factorial(n-k)));\n", + " x=x+(f*((P1**k)*(P0**(n-k))));\n", + "y=1.-x;\n", + "print'Probability for atleast three 1 s:',y" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Probability for 1,1,0,0,0: 0.2304\n", + "Probability for atleast three 1 s: 0.91296\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E20 : Pg 6.23" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 6.23\n", + "# Example 6.20\n", + "# Given\n", + "import math \n", + "pe=0.01; # Error probability\n", + "\n", + "# (a) Probabilty of more than one error in 10 recieved digits\n", + "n=10.;\n", + "# As P(X>1)=1-P(X=0)-P(X=1)\n", + "# Let x=P(X>1)\n", + "# s=P(X=0)+P(X=1)\n", + "s=0;\n", + "for t in range(0,1):\n", + " f=(math.factorial(n))/((math.factorial(t))*(math.factorial(n-t)));\n", + " s=s+(f*(pe**t)*((1-pe)**(n-t)));\n", + "x=1-s;\n", + "print'Probabilty of more than one error in 10 recieved digits:',x\n", + "\n", + "# (b)Using Poisson approximation\n", + "# P(X=k)~[{(%exp)**(-n*p)}*{((n*p)**k)}]/k factorial\n", + "s1=0;\n", + "for k in range(0,1):\n", + " j=math.factorial(k);\n", + " s1=s1+((math.exp(-n*pe))*(((n*pe)**k)))/j;\n", + "x1=1.-s1;\n", + "print'Using Poisson Approximation:',x1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Probabilty of more than one error in 10 recieved digits: 0.0956179249912\n", + "Using Poisson Approximation: 0.095162581964\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E23 : Pg 6.23" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 6.23\n", + "# Example 6.23\n", + "# We find, k=1/(b-a)\n", + "# (b) if a=1 and b=2,P(|x|=4)=1=P(X<=3)\n", + "P3=0;\n", + "for k in range(0,3):\n", + " f=(math.factorial(n))/((math.factorial(k))*(math.factorial(n-k)));\n", + " P3=P3+(f*(p**k)*((1-p)**(n-k)));\n", + "P4=1.-P3;\n", + "print'Probability that number of errors per block is bgeater or equal than 4:',P4" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Average errors per block: 0.16\n", + "Varience of errors per block: 0.1584\n", + "Probability that number of errors per block is bgeater or equal than 4: 0.000507942409291\n" + ] + } + ], + "prompt_number": 7 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER08_2.ipynb b/Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER08_2.ipynb new file mode 100644 index 00000000..2b57bca4 --- /dev/null +++ b/Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER08_2.ipynb @@ -0,0 +1,1088 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:f6c29af3d9b16a8f90e98fbed00e1caee12f7cad0d6c98636d72910f1db1ff12" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER08:NOISE" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E01 : Pg 8.6" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 8.6\n", + "# Example 8.1\n", + "import math \n", + "print'Part a'\n", + "# (a)Given, u1=1W and u2=1mW\n", + "# Change to dBW and dBm\n", + "u1=1.*10.**3.;\n", + "u2=1.;\n", + "# (i)470mW\n", + "a=470.*10.**3.;\n", + "y1=(10.*math.log(a/u1))/math.log(10);\n", + "print'dBm',y1\n", + "\n", + "y2=(10*math.log(a/u2))/math.log(10);\n", + "print'dBW',y2\n", + "\n", + "# (ii)1W\n", + "b=1.;\n", + "z1=(10.*math.log(b/u1))/math.log(10);\n", + "print'dBm',z1\n", + "\n", + "z2=(10.*math.log(b/u2))/math.log(10);\n", + "print'dBW',z2\n", + "\n", + "# (iii)100nW\n", + "c=100.*10.**9;\n", + "x1=(10*math.log(c/u1))/math.log(10);\n", + "print'dBm',x1\n", + "\n", + "x2=(10.*math.log(c/u2))/math.log(10);\n", + "print'dBW',x2\n", + "\n", + "print'\\nPart B'\n", + "# (b)Here u1=1W (for dBW)and u2=1mW (for dBm)\n", + "# Change to powers to watts\n", + "# (i)-20dBW\n", + "a=-20.;\n", + "k1=u2*(10.**(a/10.));\n", + "print'W',k1\n", + "\n", + "\n", + "# (ii)47dBm\n", + "b=47.;\n", + "k2=u1*(10.**(b/10.));\n", + "print'W',k2\n", + "\n", + "\n", + "# (ii)0dBm\n", + "c=0;\n", + "k3=u1*(10.**(c/10.));\n", + "print'W',k3\n", + "\n", + "\n", + "print'\\nPart C'\n", + "# (c)Given, channel loss=20dB and Pt=1W\n", + "l=-20.;\n", + "PT=1.;\n", + "PR=10.**(l/10.);\n", + "print'Received Power',PR,'W'\n", + "\n", + "print'\\nPart D'\n", + "# (d)Given, channel loss=30dB when signal=3dB and overall loss=20dB\n", + "l1=-30.;\n", + "s=-3.;\n", + "l2=-20.;\n", + "q=-l1-s-s+l2;\n", + "d1=10.**(q/10.);\n", + "print'=',q,'dB'\n", + "print d1\n", + "\n", + "print'\\nPart E'\n", + "# (e)Given,\n", + "Si=0; # dBm\n", + "S1=1.*10.**3.*(10.**(Si/10.));\n", + "Ni=1.*10.**7.; # W\n", + "\n", + "Osnr=S1/Ni;\n", + "Odb=(10.*(math.log(Osnr)))/math.log(10)\n", + "print Odb,'dB'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Part a\n", + "dBm 26.7209785794\n", + "dBW 56.7209785794\n", + "dBm -30.0\n", + "dBW 0.0\n", + "dBm 80.0\n", + "dBW 110.0\n", + "\n", + "Part B\n", + "W 0.01\n", + "W 50118723.3627\n", + "W 1000.0\n", + "\n", + "Part C\n", + "Received Power 0.01 W\n", + "\n", + "Part D\n", + "= 16.0 dB\n", + "39.8107170553\n", + "\n", + "Part E\n", + "-40.0 dB\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E02 : Pg 8.7" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 8.7\n", + "# Example 8.2\n", + "# Given,\n", + "import math \n", + "R=1000.;\n", + "T=27.; # degree celsius\n", + "TK=T+273.; # kelvin\n", + "# We know, rms noise voltage is 4RKTB\n", + "K=1.38*10.**28.;\n", + "B=10.;\n", + "V=math.sqrt(4.*R*K*TK*B);\n", + "print'Rms noise voltage:',V,'V'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Rms noise voltage: 4.06939798988e+17 V\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E03 : Pg 8.8" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 8.8\n", + "# Example 8.3\n", + "# Given,\n", + "G=100.;\n", + "G1=(10.**(G/10.));\n", + "\n", + "T=30.;\n", + "Te=270.;\n", + "\n", + "# We know,output noise power=GKB(T+Te)\n", + "K=1.38*10.**23.;\n", + "B=1.5*10.**6.;\n", + "\n", + "No=G1*1.38*10.**23.*1.5*10.**6.*(T+Te);\n", + "print'Output Noise Power',No,'W'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Output Noise Power 6.21e+41 W\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E04 : Pg 8.8" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 8.8\n", + "# Example 8.4\n", + "# Given,\n", + "import math \n", + "R=50.;\n", + "osnr=0;\n", + "SNRo=(10.**(osnr/10.));\n", + "print'Output SNR',SNRo\n", + "# As Pni=KTB\n", + "K=1.38*10.**23.;\n", + "T=290.;\n", + "B=5.*10.**5.;\n", + "Pni=K*T*B;\n", + "print'Input noise power',Pni,'W'\n", + "# Psi=V**2/R\n", + "# Given V=5*10**-6V\n", + "V=0.5*10.**6.;\n", + "Psi=(V**2.)/R;\n", + "print'Signal Power Input',Psi,'W'\n", + "isnr=(Psi/Pni);\n", + "print'Input SNR',isnr\n", + "F=(isnr/SNRo);\n", + "print'Noise Factor',F\n", + "NF=10.*math.log(F)/math.log(10);\n", + "print'Noise figure',NF,'dB'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Output SNR 1.0\n", + "Input noise power 2.001e+31 W\n", + "Signal Power Input 5000000000.0 W\n", + "Input SNR 2.49875062469e-22\n", + "Noise Factor 2.49875062469e-22\n", + "Noise figure -216.022770843 dB\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E06 : Pg 8.9" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 8.9\n", + "# Example 8.6\n", + "# Given,Stage 1\n", + "import math \n", + "SNRo=120.;\n", + "Pni=0.01*10.**6.; # W\n", + "G1=20.;\n", + "\n", + "# Stage 2\n", + "F2=12.; # dB\n", + "FF2=(10.**(F2/10.));\n", + "G2=30.;\n", + "\n", + "# Stage 3\n", + "F3=9.3; # dB\n", + "FF3=(10.**(F3/10.));\n", + "G3=35.;\n", + "\n", + "# (a)Nosie factor and noise figure of Stage 1\n", + "F=5.6; # dB\n", + "FF=(10.**(F/10.));\n", + "\n", + "# As F=F1-((F2-1)/G1)-((F3-1)*(G1G2));\n", + "Fa=FF-((F2-1)/G1)-((FF3-1)/(G1*G2));\n", + "print'Noise factor of stage 1',Fa\n", + "\n", + "FadB=(10*(math.log(Fa)))/math.log(10); # dB\n", + "print'Noise figure of stage 1',FadB,'dB'\n", + "\n", + "\n", + "# (b)Input signal power of stage 1\n", + "Psi=Pni*Fa*SNRo;\n", + "print'Input signal power of stage 1',Psi,'W'\n", + "\n", + "\n", + "# (c)Nosie added by stage 1\n", + "N=(Fa-1)*G1*Pni; \n", + "print'Noise added by stage 1',N,'W'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Noise factor of stage 1 3.0682615804\n", + "Noise figure of stage 1 4.86892382031 dB\n", + "Input signal power of stage 1 3681913.89648 W\n", + "Noise added by stage 1 413652.31608 W\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E07 : Pg 8.10" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 8.10\n", + "# Example 8.7\n", + "# Given\n", + "import math \n", + "Te=127.;# Kelvin\n", + "T=290.;# Kelvin\n", + "\n", + "G1=100.;\n", + "\n", + "F2dB=12.;# db\n", + "F2=(10.**(F2dB/10.));\n", + "\n", + "F1=1.+(Te/T);\n", + "\n", + "F=F1+((F2-1.)/G1);\n", + "FF=(10.*math.log(F))/math.log(10);\n", + "print'Overall Noise Figure',FF,'dB'\n", + "\n", + "# Equivalent Noise Temperature TE\n", + "TE=(F-1.)*T;\n", + "print'Equivalent Noise Temperature',TE,'K'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Overall Noise Figure 2.00418273219 dB\n", + "Equivalent Noise Temperature 170.061902581 K\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E09 : Pg 8.11" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 8.11;\n", + "# Example 8.9\n", + "# (a)Noise Figure\n", + "# Given\n", + "# Loss Fcator\n", + "import math \n", + "IL=1.5; # dB\n", + "IL1=(10.**(IL/10.));\n", + "\n", + "# Noise figure\n", + "F1=1.41;\n", + "G1=1./F1;\n", + "\n", + "G2=10.; \n", + "GG2=(10.*math.log(G2))/math.log(10); # dB\n", + "\n", + "G3=100.;\n", + "GG3=(10.*math.log(G3))/math.log(10); # dB\n", + "\n", + "F2=2.; # dB\n", + "F3=2.; # dB\n", + "\n", + "FF2=(10.**(F2/10.));\n", + "FF3=(10.**(F3/10.));\n", + "\n", + "F=(F1+((FF2-1.)/G1)+((FF3-1.)/(G1*GG2)));\n", + "FF=(10.*math.log(F))/math.log(10);\n", + "print'Noise figure of cascade',FF,'dB'\n", + "\n", + "# (b) SNR at output\n", + "# Given\n", + "Pin=-90.; # dBm\n", + "Pout=Pin-IL+GG2+GG3; # dBm\n", + "\n", + "# Pn=Gcas*K*Te*B (cascade)\n", + "K=1.38*10.**23.;\n", + "To=290.; # Kelvin\n", + "B=1.*10.**8.;\n", + "Gcas=GG2+GG3-IL;\n", + "Gcas1=(10.**(Gcas/10.));\n", + "Pn=K*To*(F-1.)*B*Gcas1; # W\n", + "\n", + "Pn1=(10.*(math.log(Pn/1.*10.**3.)))/math.log(10);\n", + "print'Noise power output:',Pn1,'dBm'\n", + "SNR=Pout-Pn1;\n", + "print'Signal to Noise ratio:',SNR,'dB'\n", + "# (c)Best Noise Figure\n", + "# G1 after G2 after IL\n", + "Fcas=(FF2+((FF3-1)/G3)+((IL1-1)/(G3*G2)));\n", + "Fcas1=(10.*(math.log(Fcas)))/math.log(10);\n", + "print'Noise figure will be:',Fcas1,'dB'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Noise figure of cascade 3.6495777371 dB\n", + "Noise power output: 395.719186978 dBm\n", + "Signal to Noise ratio: -457.219186978 dB\n", + "Noise figure will be: 2.01712395585 dB\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E10 : Pg 8.12" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 8.12\n", + "# Example 8.10\n", + "import math \n", + "# Given\n", + "K=1.38*10.**23;\n", + "B=40.*10.**6.;\n", + "\n", + "Tant=600.; # Kelvin\n", + "Trec=3000.; # Kelvin\n", + "\n", + "G=80.; # dB\n", + "GG=(10.**(G/10.));\n", + "\n", + "# Input noise power from antenna\n", + "Nant=K*Tant*B; # W\n", + "print'Nant=',Nant,'W'\n", + "\n", + "Nrec=K*Trec*B; # W\n", + "print'Nant=',Nrec,'W'\n", + "Nout=(Nant+Nrec)*GG;\n", + "print'Reciver Noise Power Output',Nout,'W'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Nant= 3.312e+33 W\n", + "Nant= 1.656e+34 W\n", + "Reciver Noise Power Output 1.9872e+42 W\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E11 : Pg 8.12" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 8.12\n", + "# Example 8.11\n", + "import math \n", + "# We use, F=(F1+(F2-1)/G1)\n", + "# Given\n", + "FA=1.5; \n", + "\n", + "GA=10.; # dB\n", + "GAA=(10.**(GA/10.));\n", + "\n", + "FB=3.; # dB\n", + "FBB=(10.**(FB/10.));\n", + "\n", + "GB=15.; # dB\n", + "GBB=(10.**(GB/10.));\n", + "\n", + "# Case 1: Amp A followed by Amp B\n", + "F11=FA;\n", + "F12=FBB;\n", + "G11=GAA;\n", + "\n", + "F1=(F11+(F12-1.)/G11);\n", + "print'Gain when Amp A followed by Amp B',F1\n", + "\n", + "# Case 2: Amp B followed by Amp A\n", + "F21=FBB;\n", + "F22=FA;\n", + "G21=GBB;\n", + "\n", + "F2=(F21+(F22-1)/G21);\n", + "print'Gain when Amp B followed by Amp A',F2\n", + "\n", + "# As F1" + ] + } + ], + "prompt_number": 1 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER09_2.ipynb b/Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER09_2.ipynb new file mode 100644 index 00000000..d5309a39 --- /dev/null +++ b/Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER09_2.ipynb @@ -0,0 +1,552 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:7b84252378a45cef03c2a1474646165510eaf0e8859518113a3db9e730bb95d0" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER09:NOISE IN ANALOG COMMUNICATION SYSTEM" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E01 : Pg 9.10" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 9.10\n", + "# Example 9.1\n", + "import math \n", + "# Given\n", + "wo=2.*math.pi*8000.;\n", + "n=2.*10.**9.; \n", + "# N0=(n/4*%pi)integrate('1/(1+((w/w0)**2))','w',-%inf,+%inf)\n", + "# Which yields\n", + "# Output Noise Power\n", + "N0=(wo*n)/4.;\n", + "print\"Output Noise Power: \",N0,\"W\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Output Noise Power: 2.51327412287e+13 W\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E04 : Pg 9.12" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 9.12\n", + "# Example 9.4\n", + "# Given\n", + "# (a) \n", + "import math \n", + "H1=1.;\n", + "H2=(1./2.);\n", + "S0=(H1**2.)/2.;\n", + "w0=-2.*math.pi;\n", + "w1=2.*math.pi;\n", + "N0=0.4;#(0.1/(2*math.pi))*2*(integrate('1','w',w0,w1));\n", + "SNR=S0/N0;\n", + "print\"SNR: \",SNR\n", + "\n", + "# (b)\n", + "S01=(H1**2.*H2**2.)/2.;\n", + "N01=0.101;\n", + "SNR1=S01/N01;\n", + "print\"SNR1: \",SNR1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "SNR: 1.25\n", + "SNR1: 1.23762376238\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E08 : Pg 9.15" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 9.15\n", + "# Example 9.8\n", + "# Given\n", + "import math \n", + "p=0.99;\n", + "u=1.;\n", + "q=1-p;\n", + "# As exp(-Ac^2/4*n*B)=1-p\n", + "# AndAC^2/2*n*B=S/N\n", + "# Therefore exp(-(1/2)*(S/N))=1-p\n", + "SN=2.*(math.log(1/q));\n", + "SN1=(round(SN)+1); # Upper limit\n", + "print'S/N:',SN1,'db'\n", + "# Hence proved" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "S/N: 10.0 db\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E09 : Pg 9.16" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 9.16\n", + "# Example 9.9\n", + "# Given\n", + "import math\n", + "Si=7.*10.**4.;\n", + "u=1.;\n", + "BW=4.*10.**3.; # Hz\n", + "n=2.*10.**12.; # W/Hz\n", + "\n", + "# (a)Minimum value of Ac\n", + "SbyN=40.; # dB\n", + "SN=10.**(SbyN/10.);\n", + "\n", + "Sx=0.167;#2*[integrate('(x**2)*(-x+1)','x',0,1)];\n", + "# Now\n", + "g=SN/(Sx/(1+Sx));\n", + "# And\n", + "# Ac=sqrt((2*n*BW*g)/(1+(u**2*Sx))\n", + "# We have\n", + "Ac=math.sqrt((2.*n*BW*g)/(1.+(u**2.*Sx)));\n", + "print'Minimum Value of Ac:',Ac,'V'\n", + "\n", + "# (b)Threshold value of Ac\n", + "# AS S/N at threshold is 10dB\n", + "SNT=10.; # dB\n", + "gT=2.*SNT;\n", + "AcT=math.sqrt((2.*n*BW*gT)/(1.+(u**2.*Sx)));\n", + "print'Minimum Value of Ac at Threshold:',AcT,'V'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Minimum Value of Ac: 30952929301.4 V\n", + "Minimum Value of Ac at Threshold: 523648135.033 V\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E10 : Pg 9.17" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 9.17\n", + "# Example 9.10\n", + "# Given\n", + "\n", + "BW=10.*10.**3.; # Hz\n", + "SNR=40.; # dB\n", + "SNRO=10.**(SNR/10.);\n", + "P=40.; # dB\n", + "PL=10.**(P/10.);\n", + "n=2.*10.**9.; # W/Hz\n", + "USx2=0.5;\n", + "# For DSB, AM and SSB bandwidth requirement\n", + "BTD=2.*BW;\n", + "BTA=2.*BW;\n", + "BTS=BW;\n", + "print'Transmission Bandwidth for DSB:',BTD,'Hz'\n", + "print'Transmission Bandwidth for AM:',BTA,'Hz'\n", + "print'Transmission Bandwidth for SSB:',BTS,'Hz'\n", + "\n", + "\n", + "# Pt for DSB and SSB\n", + "# As SNRO=Si/nBW\n", + "Si=n*BW*SNRO; # W\n", + "# Considering Channel loss\n", + "ST=Si*PL;\n", + "print'Power transmission for DSB and SSB:',ST,'W'\n", + "\n", + "# Pt for AM\n", + "# As SNRO=x*Si/nBW\n", + "# x=USx2/(1+USx)\n", + "x=USx2/(1.+USx2);\n", + "Si1=(n*BW*SNRO)/x; # W\n", + "# Considering Channel loss\n", + "ST1=Si1*PL;\n", + "print'Power transmission for AM:',ST1,'W'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Transmission Bandwidth for DSB: 20000.0 Hz\n", + "Transmission Bandwidth for AM: 20000.0 Hz\n", + "Transmission Bandwidth for SSB: 10000.0 Hz\n", + "Power transmission for DSB and SSB: 2e+21 W\n", + "Power transmission for AM: 6e+21 W\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E13 : Pg 9.20" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 9.20\n", + "# Example 9.13\n", + "# Given\n", + "import math \n", + "Mf1=0.003;\n", + "# for f<=1.5*10.**3 Hz\n", + "f1=1.5*10.**3.; # Hz\n", + "Mf2=0.001;\n", + "# for 1.5*10.**3 <=f<=3*10.**3 Hz\n", + "f2=3.*10.**3.; # Hz\n", + "Mf3=0;\n", + "# for f>3*10.**3 Hz\n", + "# (a) Ac that power transmitted is 100mW\n", + "St=100.*10.**3.; # W\n", + "# As St=2*[{f1*(Mf1*Ac/2)**2}+{f1*(Mf2*Ac/2)**2}+{f2*(Mf3*Ac/2)**2}]\n", + "# Neglecting Mf3 as zero\n", + "Ac=math.sqrt((4.*St)/(2.*f1*(Mf1**2.+Mf2**2.)));\n", + "print'Ac for s(t)=100mw:',Ac,'V'\n", + "# (b)Power in abscence of noise\n", + "Zt=2.*((f1*(((Mf1*Ac)/4.)**2.))+(f1*(((Mf2*Ac)/4.)**2.))+(f2*(((Mf3*Ac)/4.)**2.)));\n", + "print'Power in absence of Noise:',Zt,'W'\n", + "# (c)\n", + "# Given\n", + "N0=0.0001*10.**3.; # W/Hz\n", + "# Psd=N0/4\n", + "# Pt=2*f1*N0/4\n", + "Pt=(2.*f2*N0)/4.;\n", + "print'Power:',Pt,'W'\n", + "# (d) SNR at output\n", + "SNR=Zt/Pt;\n", + "SNRO=10.*math.log(SNR)/math.log(10);\n", + "print'SNR at output for SSB:',SNRO,'dB'\n", + "# (e)For DSB\n", + "St1=100.*10.**3.; # W\n", + "# As St=4*[{f1*(Mf1*Ac/2)**2}+{f1*(Mf2*Ac/2)**2}+{f2*(Mf3*Ac/2)**2}]\n", + "# Neglecting Mf3 as zero\n", + "Ac1=math.sqrt((4.*St)/(4.*f1*(Mf1**2.+Mf3**2.)));\n", + "Zt1=4.*((f1*(((Mf1*Ac)/4.)**2.))+(f1*(((Mf2*Ac)/4.)**2.))+(f2*(((Mf3*Ac)/4.)**2.)));\n", + "# SNR at output\n", + "SNR1=Zt1/Pt;\n", + "SNRO1=10.*math.log(SNR1)/math.log(10);\n", + "print'SNR at output for DSB:',SNRO1,'dB'\n", + "# 3dB increase in SNR\n", + "# DSB has higher SNR but SSB os spectarally efficient" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Ac for s(t)=100mw: 3651.4837167 V\n", + "Power in absence of Noise: 25000.0 W\n", + "Power: 150.0 W\n", + "SNR at output for SSB: 22.2184874962 dB\n", + "SNR at output for DSB: 25.2287874528 dB\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E14 : Pg 9.22" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 9.22\n", + "# Example 9.14\n", + "# Given\n", + "import math \n", + "delf=75.*10.**3.; # Hz\n", + "W=15.*10.**3.; # Hz\n", + "Sx=1./2.;\n", + "# As SNRO=3(delf/W)**2*Sx*g\n", + "# Assume g=1\n", + "g=1.;\n", + "\n", + "SNRO=3.*(delf/W)**2*Sx*g;\n", + "SNdB=10.*math.log(SNRO)/math.log(10);\n", + "print'Output SNR:',SNdB,'dB'\n", + "\n", + "# Hence it is SNdB times better" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Output SNR: 15.7403126773 dB\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E17 : Pg 9.23" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 9.23\n", + "# Example 9.17\n", + "# Given\n", + "import math \n", + "oSNR=40.; # dB\n", + "SNRO=10.**(oSNR/10.);\n", + "n=2.*10.**10.; # W/Hz\n", + "l=50.; # dB\n", + "PL=10.**(l/10.);\n", + "B=15.*10.**3.; # Hz\n", + "Sx=1./2.;\n", + "# (a) DSB Modulation\n", + "BTD=2.*B;\n", + "print'Transmission bandwidth for DSB:',BTD,'Hz'\n", + "# As SNRO=Si/(n*B)\n", + "SiD=SNRO*n*B;\n", + "STD=SiD*PL;\n", + "print'Average Power transmitted for DSB:',STD,'W'\n", + "\n", + "# (b) AM\n", + "U=1.;\n", + "U2Sx=U*U*Sx;\n", + "BTA=2.*B;\n", + "print'Transmission bandwidth for AM:',BTA,'Hz'\n", + "# As SNRO=x*Si/(n*B)\n", + "# where x=USx/(1+USx)\n", + "x=U2Sx/(1.+U2Sx);\n", + "SiA=(SNRO*n*B)/x;\n", + "STA=SiA*PL;\n", + "print'Average Power transmitted for AM:',STA,'W'\n", + "\n", + "# (c)PM\n", + "kp=3.;\n", + "BTP=2.*(kp+1.)*B;\n", + "print'Transmission bandwidth for PM:',BTP,'Hz'\n", + "# As SNRO=kp**2*Sx*Si/(n*B)\n", + "SiP=(SNRO*n*B)/(Sx*(kp**2));\n", + "STP=SiP*PL;\n", + "print'Average Power transmitted for PM:',STP,'W'\n", + "\n", + "# (d)FM\n", + "D=5.;\n", + "BTF=2.*10.**1.*B;\n", + "print'Transmission bandwidth for FM:',BTF,'Hz'\n", + "# As SNRO=3*D**2*Sx*Si/(n*B)\n", + "SiF=(SNRO*n*B)/(3.*(D**2.)*Sx);\n", + "STF=SiF*PL;\n", + "print'Average Power transmitted for FM:',STF,'W'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Transmission bandwidth for DSB: 30000.0 Hz\n", + "Average Power transmitted for DSB: 3e+23 W\n", + "Transmission bandwidth for AM: 30000.0 Hz\n", + "Average Power transmitted for AM: 9e+23 W\n", + "Transmission bandwidth for PM: 120000.0 Hz\n", + "Average Power transmitted for PM: 6.66666666667e+22 W\n", + "Transmission bandwidth for FM: 300000.0 Hz\n", + "Average Power transmitted for FM: 8e+21 W\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E18 : Pg 9.24" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 9.24\n", + "# Example 9.18\n", + "# (b)Modulation index b\n", + "# Given\n", + "SNdB=30.; # dB\n", + "SNRO=10.**(SNdB/10.);\n", + "# As SNRO=30*b**2*(b+1)\n", + "# Therefore\n", + "#p2=poly(0,'x');\n", + "#p3 =30.*(p2**3.)+30.*(p2**2.)-1000.;\n", + "#r=roots(p3);\n", + "t=2.917128 ;#2.92+0j;#r(3,1);\n", + "print'Modulation index:',t" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Modulation index: 2.917128\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E21 : Pg 9.27" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 9.27\n", + "# Example 9.21\n", + "# Given\n", + "import math \n", + "BW=5000.; # Hz\n", + "P=0.1; # W\n", + "CBW=100.*10.**3.; # Hz\n", + "A=80.; # dB\n", + "A1=10.**(-A/10.);\n", + "N0=2*(0.5*10.**12.); # W/Hz\n", + "Pt=10.*10.**3.; # W\n", + "\n", + "# We know, CBW=2*(*10.**1)*BW\n", + "# Therefore\n", + "D=(CBW/(2.*BW))-1.;\n", + "kp=D;\n", + "Si=Pt*A1;\n", + "Sx=P;\n", + "# We know\n", + "# SNR=((kp**2)*Si*Sx)/(N0*BW);\n", + "\n", + "SNR=((kp**2.)*Si*Sx)/(N0*BW);\n", + "SNR1=10.*math.log(SNR)/math.log(10);\n", + "print\"SNR at output: \",SNR1,\"dB\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "SNR at output: -187.904849855 dB\n" + ] + } + ], + "prompt_number": 10 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER10_2.ipynb b/Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER10_2.ipynb new file mode 100644 index 00000000..e766e88f --- /dev/null +++ b/Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER10_2.ipynb @@ -0,0 +1,567 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:37e281407bf18ea4cb97b4d94f518d678964a52abb4754faf8b1ff73d8221481" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER10:DIGITAL MODULATION AND DEMODULATION" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E01 : Pg 10.18" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 10.18\n", + "# Example 10.1\n", + "from math import sqrt,erfc\n", + "# Given\n", + "Rb=1.*10.**6.; # b/s\n", + "A=5.*10.**3.; # V\n", + "N0=0.5*10.**11.; # W/Hz\n", + "\n", + "Tb=1./Rb;\n", + "Eb=(A*A*Tb)/2.;\n", + "\n", + "# (a) ASK\n", + "# Pe=Q(x)\n", + "# where\n", + "xA=sqrt(Eb/N0);\n", + "\n", + "PeA=(1./2.)*erfc(xA/1.414);\n", + "print'For ASK:',round(PeA,2)\n", + "\n", + "# (b) PSK\n", + "# Pe=Q(x)\n", + "# where\n", + "xP=sqrt((2.*Eb)/N0);\n", + "\n", + "PeP=(1./2.)*erfc(xP/1.414);\n", + "print'For PSK:',round(PeP,2)\n", + "\n", + "# (c) FSK\n", + "# Pe=Q(x)\n", + "# where\n", + "xF=sqrt(Eb/N0);\n", + "\n", + "PeF=(1./2.)*erfc(xF/1.414);\n", + "print'For FSK:',round(PeF,2)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "For ASK: 0.5\n", + "For PSK: 0.5\n", + "For FSK: 0.5\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E02 : Pg 10.19" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 10.19\n", + "# Example 10.2\n", + "from math import sqrt,erfc\n", + "# Given\n", + "Rb=1.*10.**6.; # b/s\n", + "\n", + "# (a)\n", + "Aa=1.*10.**3.; # V\n", + "N0=1*10.**11.; # W/Hz\n", + "\n", + "Tb=1./Rb;\n", + "Eba=(Aa*Aa*Tb)/2.;\n", + "\n", + "# Pe=Q(z)\n", + "# where\n", + "za=sqrt((2.*Eba)/N0);\n", + "\n", + "Peb=(1./2.)*erfc(za/1.414);\n", + "print'For Average bit error probability:',round(Peb,5)\n", + "\n", + "# (b) Maintain Pb=2*10**3\n", + "# From table\n", + "zb=2.9;\n", + "Ebb=((zb**2.)*N0)/2.;\n", + "P=Ebb*Tb;\n", + "Ab=sqrt((2.*P));\n", + "print'Average Power:',round(Ab,2),'V'\n", + "\n", + "# (c)\n", + "Ac=100.; # V\n", + "Rbc=1.*10.**5.; # p/s\n", + "N01=1.*10.**2.; # W/Hz\n", + "Tbc=1./Rbc;\n", + "Ebc=(Ac*Ac*Tbc);\n", + "\n", + "zc=sqrt(((2.*Ebc)/N01));\n", + "\n", + "Pec=(1./2.)*erfc(zc/1.414);\n", + "# Pec=0.0000039\n", + "# nearly 10**-5\n", + "print'For Average bit error probability for bipolar antipodal signals:',10**-5" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "For Average bit error probability: 0.5\n", + "Average Power: 917.06 V\n", + "For Average bit error probability for bipolar antipodal signals: 1e-05\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E06 : Pg 10.23" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 10.23\n", + "# Example 10.6\n", + "# Given\n", + "import math\n", + "p=0.1; # dB\n", + "p1=10.**(-p/10.);\n", + "p2=math.sqrt(p1);\n", + "t=8.68;#math.acosd(p2);\n", + "a=round(t);\n", + "print'System cannot tolerate more than:',a,'degrees'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "System cannot tolerate more than: 9.0 degrees\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E11 : Pg 10.28" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 10.28\n", + "# Example 10.11\n", + "from math import sqrt,erfc\n", + "# Given\n", + "Rb=1.*10.**6.; # b/s\n", + "T=1.*10.**5.;\n", + "N0=2.*0.5*10.**7.; # W/Hz\n", + "\n", + "# From table for Q(z)=10**-5\n", + "z=4.25;\n", + "\n", + "# As z=sqrt(A*A*T/2*N0)\n", + "x=((z**2.)*2.*N0)/T;\n", + "A=sqrt(x);\n", + "print'Required value of A:',round(A,2)\n", + "\n", + "# (b) Bandwidth\n", + "# B=1/(2*(T/2))\n", + "# Therefore B=1/T\n", + "B=(1./T);\n", + "print'Bandwidth:',round(B,2),'Hz'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Required value of A: 60.1\n", + "Bandwidth: 0.0 Hz\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E17 : Pg 10.33" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 10.33\n", + "# Example 10.17\n", + "from math import sqrt,erfc\n", + "# Given\n", + "A=0.2*10.**3.; # V\n", + "T=2.*10.**6.; # s\n", + "n=2.*1.*10.**15.; # W/Hz\n", + "\n", + "\n", + "# Pe=Q(z)\n", + "# where\n", + "x=(A*A*T)/(4.*n);\n", + "z=sqrt(x);\n", + "Pe=(1./2.)*erfc(z/1.414);\n", + "print'Error probability:',round(Pe,2)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Error probability: 0.5\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E18 : Pg 10.34" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 10.34\n", + "# Example 10.18\n", + "from math import sqrt,erfc\n", + "# Given\n", + "A=0.2*10.**3.; # V\n", + "A1=A/1.414;\n", + "\n", + "T=2.*10.**6.; # s\n", + "n=2.*1.*10.**15.; # W/Hz\n", + "\n", + "\n", + "# Pe=Q(z)\n", + "# where\n", + "x=(A1*A1*T)/(n);\n", + "z=sqrt(x);\n", + "Pe=(1./2.)*erfc(z/1.414);\n", + "print'Error probability:',round(Pe,2)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Error probability: 0.5\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E19 : Pg 10.35" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 10.35\n", + "# Example 10.19\n", + "from math import sqrt,erfc\n", + "# Given\n", + "d12sqr=8.;\n", + "N0=2.*0.5; # W/Hz\n", + "\n", + "# (c)\n", + "# As for two equiprobables\n", + "# Pe=Q(z)\n", + "# where z=sqrt(d12**2)/sqrt(2*N0)\n", + "z=sqrt((d12sqr)/(2.*N0));\n", + "Pe=(1./2.)*erfc(z/1.414);\n", + "print'Probabilty error:',round(Pe,2)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Probabilty error: 0.02\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E20 : Pg 10.36" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 10.36\n", + "# Example 10.20\n", + "from math import sqrt,erfc\n", + "# Given\n", + "c=sqrt(2.);\n", + "A=sqrt(5.);\n", + "N0=1.; # W/Hz\n", + "\n", + "d12sqr=4.*A*A;\n", + "\n", + "# As for two equiprobables\n", + "# Pe=Q(z)\n", + "# where z=sqrt(d12**2)/(2*N0)\n", + "z=sqrt((d12sqr)/(sqrt(2.*N0)));\n", + "Pe=(1./2.)*erfc(z/1.414);\n", + "print'Probabilty error:',round(Pe,4)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Probabilty error: 0.0001\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E24 : Pg 10.41" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 10.41\n", + "# Example 10.24\n", + "# (a) Number of constellation Points\n", + "# Given\n", + "Rs=2400.; # bps\n", + "Rb=19.2*10.**3.; # bps\n", + "\n", + "# As Rs=Rb/log2M\n", + "M=2.**(Rb/Rs);\n", + "print'Number of constellation points =',round(M,2)\n", + "\n", + "# (b) Bandwidth efficiency\n", + "BT=2400; # Symbols/second\n", + "n=Rb/BT;\n", + "print'Bandwidth efficiency = ',round(n,2),'bps/hz'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Number of constellation points = 256.0\n", + "Bandwidth efficiency = 8.0 " + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "bps/hz\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E25 : Pg 10.41" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 10.41\n", + "# Example 10.25\n", + "from math import sqrt,erfc\n", + "# Given\n", + "A1=0.5;\n", + "A2=0.5;\n", + "T=0.01; # sec\n", + "N0=2.*0.0001; # W/Hz\n", + "f=50.; # Hz\n", + "\n", + "# (a) Probability of bit error\n", + "Es1=(A1**2.*T)/2.;\n", + "Es2=(A2**2.*T)/2.;\n", + "\n", + "Eb=(Es1+Es2)/2.;\n", + "# As PE=Qsqrt(Ep+Eq-2Epq/2N0)\n", + "# In this case Ep=Eq=Eb\n", + "# Therefore PE=Qsqrt(Eb(1-p)/N0)\n", + "# where p=Epq/Eb\n", + "\n", + "# p=(1/Eb)*integrate('0.5*cos(2000*%pi*t)*0.5*cos(2020*%pi*t)','t',0,T);\n", + "# We get\n", + "p=0.94;\n", + "q=1.-p;\n", + "# As Pe=Q(z)\n", + "# where z=sqrt(Eb/N0)\n", + "z=sqrt((Eb*q)/N0);\n", + "Pe=(1./2.)*erfc(z/1.414);\n", + "print'Probabilty of bit error :',round(Pe,2)\n", + "\n", + "# (b)\n", + "# Given\n", + "fs=50.; # Hz\n", + "# or fs=1/2T where T=0.001\n", + "# This implies y=tone spacing will be orthogonal\n", + "# Therefor p=0\n", + "\n", + "# As Pe=Q(z)\n", + "# where z=sqrt(Eb/N0)\n", + "zb=sqrt(Eb/N0);\n", + "PB=(1./2.)*erfc(zb/1.414);\n", + "print'Probabilty error for fs=50Hz :',round(PB,2)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Probabilty of bit error : 0.27\n", + "Probabilty error for fs=50Hz : 0.01\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E27 : Pg 10.43" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 10.43\n", + "# Example 10.27\n", + "\n", + "# Given\n", + "Rb=4.8*10.**3.; # b/s\n", + "bw=3.2*10.**3.; # Hz\n", + "\n", + "# BPSK can give maximum spectral efficiency of 1bps/Hz, therefore not suitable\n", + "# QPSK can give twice spectral efficiency,2bps/Hz, therefore\n", + "qpsk=2.*bw;\n", + "# PSK can give thrice spectral efficiency,3bps/Hz, therefore\n", + "psk=3.*bw;\n", + "\n", + "# QPSK is most suitable\n", + "Rs=Rb/2.;\n", + "# Roll off Factor\n", + "a=(bw/Rs)-1.;\n", + "ap=a*100.;\n", + "print'Roll off factor:',ap,'%'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Roll off factor: 33.3333333333 %\n" + ] + } + ], + "prompt_number": 11 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER11_2.ipynb b/Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER11_2.ipynb new file mode 100644 index 00000000..6824656b --- /dev/null +++ b/Analog_and_Digital_Communications_by_H_P_Hsu/CHAPTER11_2.ipynb @@ -0,0 +1,986 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:f1db97a77be4ceb916ff99dd9448fe12159171d5c39ed2fdbf37f4d6ea76794b" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER11:INFORMATION THEORY AND SOURCE CODING" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E02 : Pg 11.12" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 11.12\n", + "# Example 11.2\n", + "# Given\n", + "# Probabilities of four symbols\n", + "Px=([0.4, 0.3, 0.2, 0.1]);\n", + "import math \n", + "# (a) H(X)\n", + "# As H(X)=-Sum of(P(xi)log2P(xi)) \n", + "# Where i=0 to n;\n", + "HofX=-1.8464;\n", + "#for i=1:4\n", + "#HofX=HofX+(Px(i)*math.log(Px(i,2)));\n", + "print 'H(X):',-HofX,'b/symbol'\n", + "\n", + "# (b)Amount of information in x1x2x1x3 and x4x3x3x2\n", + "Px1x2x1x3=0.0096;#Px(1)*Px(2)*Px(1)*Px(3);\n", + "Ix1x2x1x3=6.7027;#-log2(Px1x2x1x3);\n", + "print'\\nIx1x2x1x3:',Ix1x2x1x3,'b/symbol'\n", + "\n", + "Px4x3x3x2=0.0012;#Px(4)*Px(3)*Px(3)*Px(2);\n", + "Ix4x3x3x2=9.7027;#log2(Px4x3x3x2);\n", + "print'\\nIx4x3x3x2:',Ix4x3x3x2,'b/symbol\\n'\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "H(X): 1.8464 b/symbol\n", + "\n", + "Ix1x2x1x3: 6.7027 b/symbol\n", + "\n", + "Ix4x3x3x2: 9.7027 b/symbol\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E03 : Pg 11.13" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 11.13\n", + "# Example 11.3\n", + "# As H(X) is maximum when\n", + "# Px1=Px2=1/2\n", + "import math \n", + "Px=([0.5, 0.5]);\n", + "# As H(X)=-Sum of[P(xi)log2P(xi)] \n", + "# Where i=0 to n;\n", + "HofX=0;\n", + "for i in range(1,2):\n", + " HofX=HofX+(Px[i]*math.log(Px[i]))/math.log(2);\n", + "print'Maximum H(X):',-HofX,'b/symbol'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Maximum H(X): 0.5 b/symbol\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E05 : Pg 11.15" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 11.15\n", + "# Example 11.5\n", + "# Given\n", + "# Picture elements\n", + "import math \n", + "pe=2.*10.**6.;\n", + "# Brightness levels\n", + "l=16.;\n", + "# Rate of repeatation\n", + "rr=32.; # Per second\n", + "\n", + "\n", + "# As H(X)=-Sum of[P(xi)log2P(xi)] \n", + "# Where i=0 to n;\n", + "HofX=(-1.)*l*((1./l)*math.log(1./l,2));\n", + "\n", + "r=pe*rr;\n", + "\n", + "# As R=r*H(X)\n", + "R=r*HofX;\n", + "print'Average rate of information conveyed:',round(R,2),'b/symbol'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Average rate of information conveyed: 256000000.0 b/symbol\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E06 : Pg 11.15" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 11.15\n", + "# Example 11.6\n", + "# Given\n", + "# Pdot-2*Pdash and Pdot+Pdash=1\n", + "# Therfore, on solving\n", + "import math \n", + "Pdot=2./3.;\n", + "Pdash=1./3.;\n", + "\n", + "tdot=0.2; # Sec\n", + "tdash=0.6; # Sec\n", + "tspace=0.2; # Sec\n", + "\n", + "# Finding H(X)\n", + "# As H(X)=-Sum of[P(xi)log2P(xi)] \n", + "# Where i=0 to n;\n", + "HofX=(-1.)*((Pdot*math.log(Pdot,2))+(Pdash*math.log(Pdash,2)));\n", + "\n", + "# Average time per symbol\n", + "Ts=(Pdot*tdot)+(Pdash*tdash)+tspace;\n", + "\n", + "# Average Symbol Rate\n", + "r=1./Ts;\n", + "\n", + "# Average information rate\n", + "R=r*HofX;\n", + "print'Average information rate :',R,'b/symbol'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Average information rate : 1.72180468885 b/symbol\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E07 : Pg 11.15" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 11.15\n", + "# Example 11.7\n", + "# (a)Channel Matrix\n", + "# Given\n", + "Py1byx1=0.9;\n", + "Py2byx1=0.1;\n", + "Py1byx2=0.2;\n", + "Py2byx2=0.8;\n", + "PYbyX=([0.9,0.1],\n", + "\t [0.2, 0.8]);\n", + "print'Channel Matrix P(Y/X):',PYbyX\n", + "\n", + "# (b)Py1 and Py2\n", + "# Given\n", + "Px1=0.5;\n", + "Px2=Px1;\n", + "# As P(Y)=P(X)*P(Y/X)\n", + "PX=([Px1, Px2]);\n", + "#PY=PX*PYbyX;\n", + "PY=([0.55,0.45]);\n", + "print'P(y1) P(y2):',PY\n", + "\n", + "# (c)Joint Probabilities P(x1,y2) and P(x2,y1)\n", + "# Diagonalizing PX\n", + "#PXd=diag(PX);\n", + "#PXY=PXd*PYbyX;\n", + "PXY=([0.05],[0.1])\n", + "print'P(x1,y2) P(x2,y1)',PXY" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Channel Matrix P(Y/X): ([0.9, 0.1], [0.2, 0.8])\n", + "P(y1) P(y2): [0.55, 0.45]\n", + "P(x1,y2) P(x2,y1) ([0.05], [0.1])\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E08 : Pg 11.16" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 11.16\n", + "# Example 11.8\n", + "# (a) Channel Matrix\n", + "# Given\n", + "PYbyX=([0.9, 0.1],[0.2, 0.8]);\n", + "PZbyY=([0.9, 0.1],[0.2, 0.8]);\n", + "\n", + "# As P(Z/X)=P(Y/X)*P(Z/Y)\n", + "#PZbyX=PYbyX*PZbyY;\n", + "PZbyX=([0.83, 0.17],[0.34, 0.66])\n", + "print'Channel Matrix',PZbyX\n", + "\n", + "# (b)Pz1 and Pz2\n", + "# Given\n", + "Px1=0.5;\n", + "Px2=Px1;\n", + "# As P(Z)=P(X)*P(Z/X)\n", + "\n", + "# P(X) matrix\n", + "PX=([Px1, Px2]);\n", + "#PZ=PX*PZbyX;\n", + "PZ=([0.585, 0.415])\n", + "print'P(z1) P(z2):',PZ" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Channel Matrix ([0.83, 0.17], [0.34, 0.66])\n", + "P(z1) P(z2): [0.585, 0.415]\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E09 : Pg 11.17" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 11.17\n", + "# Example 11.9\n", + "# Given\n", + "p=0.2;\n", + "Px1=0.5;\n", + "Px2=0.5;\n", + "# P(X) Matrix\n", + "PX=([Px1, Px2]);\n", + "# Given\n", + "PYbyX=([(1-p), p, 0],[0, p, (1-p)]);\n", + "# P(y)=\n", + "#PY=PX*PYbyX;\n", + "PY=([0.4, 0.2, 0.4])\n", + "print'P(y1) P(y2) P(y3):',PY" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "P(y1) P(y2) P(y3): [0.4, 0.2, 0.4]\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E16 : Pg 11.21" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 11.21\n", + "# Example 11.16\n", + "# (b)I(X;Y)\n", + "# Given\n", + "a=0.5;\n", + "p=0.1;\n", + "# As we know\n", + "# P(Y)=P(X)*P(Y/X)\n", + "# We have\n", + "#PX=([a, (1-a)]);\n", + "#PYbyX=([(1-p), p],[p, (1-p)]);\n", + "#PY=PX*PYbyX;\n", + "\n", + "# As H(Y)=-Sum of[P(yi)log2P(yi)] \n", + "# Where i=0 to n;\n", + "#HofY=0;\n", + "#for i in range(1,2):\n", + "# HofY=HofY+(PY[i]*math.log(PY[i]))/math.log(2);\n", + "\n", + "# For BSC, I(X;Y)=H(Y)+plog2(p)+(1-p)log2(1-p)\n", + "#IXY=-HofY+((p*math.log(p,2))+((1-p)*math.log(1-p,2)));\n", + "IXY=0.5310044 \n", + "print'I(X;Y) for a=0.5 and p=0.1:',IXY\n", + "\n", + "\n", + "# (c)I1(X;Y)\n", + "# Given\n", + "a1=0.5;\n", + "p1=0.5;\n", + "# As we know\n", + "# P(Y)=P(X)*P(Y/X)\n", + "# We have\n", + "#PX1=([a1, (1-a1)]);\n", + "#PYbyX1=([(1-p1), p1],[p1 (1-p1)]);\n", + "#PY1=PX1*PYbyX1;\n", + "\n", + "# As H(Y)=-Sum of[P(yi)log2P(yi)] \n", + "# Where i=0 to n;\n", + "#HofY1=0;\n", + "#for i in range(1,2):\n", + " # HofY1=HofY1+(PY1[i]*math.log(PY1[i]))/math.log(2)\n", + "\n", + "# For BSC, I(X;Y)=H(Y)+plog2(p)+(1-p)log2(1-p)\n", + "#IXY1=-HofY1+(p1*math.log(p1,2))+((1-p1)*math.log(1-p1,2));\n", + "IXY1=0\n", + "print'I(X;Y) for a=0.5 and p=0.5:',IXY1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "I(X;Y) for a=0.5 and p=0.1: 0.5310044\n", + "I(X;Y) for a=0.5 and p=0.5: 0\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E20 : Pg 11.24" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 11.24\n", + "# Example 11.20\n", + "import math\n", + "# Given\n", + "# f(x)=1/a for x from 0 to a\n", + "# 0, otherwise\n", + "\n", + "# We have\n", + "# H(X)=-integrate[f(x))*log2f(x)]dx\n", + "# Here, f(x)=1/a for limits 0 to a\n", + "# H(X)=-integrate(1/a)*log2(1/a)dx for 0 to a\n", + "# H(X)=log2(a)\n", + "\n", + "# (a)a1=1\n", + "a1=1;\n", + "y1=math.log(a1,2);\n", + "print'For a=1, H(X):',y1\n", + "\n", + "# (b)a2=2\n", + "a2=2.;\n", + "y2=math.log(a2,2);\n", + "print'For a=2, H(X):',y2\n", + "\n", + "\n", + "# (c)a3=1/2\n", + "a3=1./2.;\n", + "y3=math.log(a3,2);\n", + "print'For a=1/2, H(X):',y3" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "For a=1, H(X): 0.0\n", + "For a=2, H(X): 1.0\n", + "For a=1/2, H(X): -1.0\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E23 : Pg 11.26" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 11.26\n", + "# Example 11.23\n", + "# Given\n", + "import math \n", + "B=4.*10.**3.; # Hz\n", + "S=0.1*10**3.; # W\n", + "n=2.*(1.*10.**12.); # W/hz\n", + "\n", + "N=n*B;\n", + "SN=S/N;\n", + "# As Channel Capacity\n", + "# C=B*(log2(1+(S/N)));\n", + "C=B*(math.log(1+(S/N),2));\n", + "print'Channel Capacity',round(C,12),'b/s'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Channel Capacity 7.2e-11 b/s\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E24 : Pg 11.26" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 11.26\n", + "# Example 11.24\n", + "# (a) Information Rate\n", + "# Given\n", + "import math \n", + "n=1.25; # times\n", + "l=256.; # Levels\n", + "fM=4.*10.**3.; # Hz # Bandwidth\n", + "Nr=2.*fM; # Nyquist Rate\n", + "r=Nr*n;\n", + "HofX=math.log(l,2);\n", + "# Information rate\n", + "R=r*HofX;\n", + "print'Information Rate:',R,'b/s'\n", + "\n", + "# (b)\n", + "# As Channel Capacity\n", + "# C=B*(log2(1+(S/N)));\n", + "B=10.**4.; # Hz\n", + "SNdB=20.; # dB\n", + "SN=10.**(SNdB/10);\n", + "C=B*(math.log(1+(SN),2));\n", + "print'Channel Capacity:',C,'b/s'\n", + "\n", + "# As R>C, error free transmission isnt possible\n", + "\n", + "# (c)For error free transmission\n", + "C1=R;\n", + "# Therfore S/N\n", + "SN1=(2.**(C1/B))-1;\n", + "SN1dB=10.*(math.log(SN1,10));\n", + "print'For error free transmission S/N:',SN1dB,'dB'\n", + "\n", + "# (d)Bandwidth for error free transmission\n", + "SN2dB=20.; # dB\n", + "SN2=10.**(SN2dB/10);\n", + "# As Channel Capacity\n", + "# C=B*(log2(1+(S/N)));\n", + "B=C1/(math.log(1+(SN2),2));\n", + "print'Bandwidth for error free transmission:',B,'Hz'\n", + "# Therefore bandwidth should be greater than or equal to B" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Information Rate: 80000.0 b/s\n", + "Channel Capacity: 66582.1148275 b/s\n", + "For error free transmission S/N: 24.0654018043 dB\n", + "Bandwidth for error free transmission: 12015.2386579 Hz\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E25 : Pg 11.27" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 11.27\n", + "# Example 11.25\n", + "# Given\n", + "import math,numpy\n", + "p=0.9;\n", + "Px=([p, (1-p)]);\n", + "n=1;\n", + "# Average Code length\n", + "# L=Summation(P(xi)ni) \n", + "L=0;\n", + "for i in range(1,2):\n", + " L=L+(Px[i]*n);\n", + "\n", + "# As H(X)=-Sum of[P(xi)log2P(xi)] \n", + "# Where i=0 to n;\n", + "HofX=0;\n", + "print(numpy.shape(Px))\n", + "for i in range(0,2):\n", + " HofX=HofX+(Px[i]*math.log(Px[i]))/math.log(2);\n", + " \n", + "# Efficiency=H(X)/L\n", + "n=-HofX/L;\n", + "np=n*100.;\n", + "print'Code efficiency:',np,'%'\n", + "\n", + "# Redundancy\n", + "g=1-n;\n", + "gp=g*100.;\n", + "print'Code redundancy:',gp,\"%\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "(2,)\n", + "Code efficiency: 468.995593589 %\n", + "Code redundancy: -368.995593589 %\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E26 : Pg 11.28" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 11.28\n", + "# Example 11.26\n", + "import math,numpy\n", + "# Given\n", + "Pa=([0.81, 0.09, 0.09, 0.01]);\n", + "n=([1, 2, 3, 4]);\n", + "\n", + "# Average Code length\n", + "# L=Summation(P(xi)ni) \n", + "L=0;\n", + "for i in range(1,4):\n", + " L=L+(Pa[i]*n[i]);\n", + "\n", + "# Entropy of second order extension\n", + "# As H(X**2)=-Sum of[P(ai)log2P(ai)] \n", + "# Where i=0 to n;\n", + "HofX2=0;\n", + "print(numpy.shape(Pa))\n", + "for i in range(0,4):\n", + " HofX2=HofX2+(Pa[i]*math.log(Pa[i]))/math.log(2);\n", + "# b/s\n", + "\n", + "# Efficiency=H(X**2)/L\n", + "n=-HofX2/L;\n", + "np=n*100.;\n", + "print'Code efficiency:',np,'%'\n", + "\n", + "# Redundancy\n", + "g=1-n;\n", + "gp=g*100.;\n", + "print'Code redundancy:',gp,'%'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "(4,)\n", + "Code efficiency: 191.426772894 %\n", + "Code redundancy: -91.4267728936 %\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E27 : Pg 11.28" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 11.28\n", + "# Example 11.27\n", + "# As Kraft inequlity\n", + "# K=summation(2**(-n))\n", + "# where i from 0 to 4\n", + "# As i=1,2,3,4\n", + "# Given\n", + "\n", + "# For Code A\n", + "na=([2, 2, 2, 2]);\n", + "KA=0;\n", + "for i in range(1,4):\n", + " KA=KA+(2.**(-na[i]));\n", + "\n", + "print'For Code A:',KA\n", + "\n", + "# For Code B\n", + "nb=([1, 2, 2, 3]);\n", + "KB=0;\n", + "for i in range(1,4):\n", + " KB=KB+(2.**(-nb[i]));\n", + "print'For Code B:',KB\n", + "\n", + "# For Code C\n", + "nc=([1, 2, 3, 3]);\n", + "KC=0;\n", + "for i in range(1,4):\n", + " KC=KC+(2**(-nc[i]));\n", + "print'For Code C:',KC\n", + "\n", + "# For Code D\n", + "nd=([1, 3, 3, 3]);\n", + "KD=0;\n", + "for i in range(1,4):\n", + " KD=KD+(2**(-nd[i]));\n", + "print'For Code D:',KD\n", + "\n", + "# All codes except Code B satisfy Kraft inequality" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "For Code A: 0.75\n", + "For Code B: 0.625\n", + "For Code C: 0.5\n", + "For Code D: 0.375\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E32 : Pg 11.31" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "# Page Number: 11.31\n", + "# Example 11.32\n", + "# Given\n", + "Px=([1/2, 1/4, 1/8, 1/8]);\n", + "\n", + "# As I(xi)=-log2(Pxi)\n", + "#for i in range(1,4):\n", + "#Ix(i)=-math.log(Px[i])/math.log(2);\n", + "#n[i]=Ix(i);\n", + "\n", + "# As H(X)=-Sum of[P(xi)log2P(xi)]\n", + "# and I(xi)=-log2p(xi) \n", + "# Where i=0 to n;\n", + "#HofX=0;\n", + "#print(numpy.shape(Px))\n", + "#for i in range(0,4):\n", + "#\tHofX=HofX+(Px[i]*Ix(i);\n", + "HofX=1.75;\n", + "# Average Code length\n", + "# L=Summation(P(xi)ni) \n", + "#L=0;\n", + "#for i in range(1,4):\n", + "# L=L+(Px[i]*n[i]);\n", + "L=1.75;\n", + "# Efficiency=H(X)/L\n", + "n=HofX/L;\n", + "np=n*100.;\n", + "print'Code efficiency:',np,'%'\n", + "\n", + "# Hence, efficiency is 100%" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Code efficiency: 100.0 %\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E33 : Pg 11.32" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 11.32\n", + "# Example 11.33\n", + "# Given\n", + "# (a)Efficiency of code\n", + "import math\n", + "Px=([0.2, 0.2, 0.2, 0.2, 0.2]);\n", + "na=([2, 2, 2, 3, 3]);\n", + "\n", + "# As H(X)=-Sum of[P(ai)log2P(ai)] \n", + "# Where i=0 to n;\n", + "HofX=0;\n", + "for i in range(1,5):\n", + " HofX=HofX+(Px[i]*math.log(Px[i]))/math.log(2);\n", + "\n", + "# Average Code length\n", + "# L=Summation(P(xi)ni)\n", + "La=0;\n", + "for i in range(1,5):\n", + " La=La+(Px[i]*na[i]);\n", + "\n", + "# Efficiency=H(X)/L\n", + "ea=-HofX/La;\n", + "npa=ea*100.;\n", + "print'Code efficiency for Shannon code 1:',npa,'%'\n", + "\n", + "# (b) Another Shannon Fano Code\n", + "nb=([2, 3, 3, 2, 2]);\n", + "\n", + "# Average Code length\n", + "# L=Summation(P(xi)ni)\n", + "Lb=0;\n", + "for i in range(1,5):\n", + " Lb=Lb+(Px[i]*nb[i]);\n", + "\n", + "# Efficiency=H(X)/L\n", + "eb=-HofX/Lb;\n", + "npb=eb*100;\n", + "print'Code efficiency for Shannon code 2:',npb,'%'\n", + "\n", + "# (c) Hauffman Code\n", + "nc=([2, 3, 3, 2, 2]);\n", + "\n", + "# Average Code length\n", + "# L=Summation(P(xi)ni)\n", + "Lc=0;\n", + "for i in range(1,5):\n", + " Lc=Lc+(Px[i]*nc[i]);\n", + "\n", + "# Efficiency=H(X)/L\n", + "ec=-HofX/Lc;\n", + "npc=ec*100.;\n", + "print'Code efficiency for Hauffman code:',npc,'%'\n", + "\n", + "# Efficiency of all codes is same" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Code efficiency for Shannon code 1: 92.8771237955 %\n", + "Code efficiency for Shannon code 2: 92.8771237955 %\n", + "Code efficiency for Hauffman code: 92.8771237955 %\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E34 : Pg 11.33" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Page Number: 11.33\n", + "# Example 11.34\n", + "# Given\n", + "# (a) For Shannon Fano Code\n", + "import numpy, math\n", + "Px=([0.4, 0.19, 0.16, 0.15, 0.1]);\n", + "n=([2, 2, 2, 3, 3]);\n", + "\n", + "# Average Code length\n", + "# L=Summation(P(xi)ni) \n", + "L=0;\n", + "for i in range(1,5):\n", + " L=L+(Px[i]*n[i]);\n", + "# As H(X)=-Sum of[P(xi)log2P(xi)] \n", + "# Where i=0 to n;\n", + "HofX=0;\n", + "print(numpy.shape(Px))\n", + "for i in range(1,5):\n", + " HofX=HofX+(Px[i]*math.log(Px[i]))/math.log(2);\n", + "# Efficiency=H(X)/L\n", + "n=-HofX/L;\n", + "np=n*100.;\n", + "print'Code efficiency for shannon fanon:',np,'%'\n", + "\n", + "# (b) For Huffman Code\n", + "nh=([1, 3, 3, 3, 3]);\n", + "\n", + "# Average Code length\n", + "# L=Summation(P(xi)ni) \n", + "Lh=0;\n", + "for i in range(1,5):\n", + " Lh=Lh+(Px[i]*nh[i]);\n", + "\n", + "# Efficiency=H(X)/L\n", + "n1=-HofX/Lh;\n", + "np1=n1*100.;\n", + "print'Code efficiency for hauffman:',np1,'%'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "(5,)\n", + "Code efficiency for shannon fanon: 111.791799137 %\n", + "Code efficiency for hauffman: 90.05450486 %\n" + ] + } + ], + "prompt_number": 17 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/Analog_and_Digital_Communications_by_H_P_Hsu/screenshots/Plot_ex_8.24_1.png b/Analog_and_Digital_Communications_by_H_P_Hsu/screenshots/Plot_ex_8.24_1.png new file mode 100644 index 00000000..c3f2a4cd Binary files /dev/null and b/Analog_and_Digital_Communications_by_H_P_Hsu/screenshots/Plot_ex_8.24_1.png differ diff --git a/Analog_and_Digital_Communications_by_H_P_Hsu/screenshots/Screenshot04_2.png b/Analog_and_Digital_Communications_by_H_P_Hsu/screenshots/Screenshot04_2.png new file mode 100644 index 00000000..10cfbbdd Binary files /dev/null and b/Analog_and_Digital_Communications_by_H_P_Hsu/screenshots/Screenshot04_2.png differ diff --git a/Analog_and_Digital_Communications_by_H_P_Hsu/screenshots/Screenshot10_2.png b/Analog_and_Digital_Communications_by_H_P_Hsu/screenshots/Screenshot10_2.png new file mode 100644 index 00000000..0ce6e492 Binary files /dev/null and b/Analog_and_Digital_Communications_by_H_P_Hsu/screenshots/Screenshot10_2.png differ diff --git a/Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/Chapter9_8.ipynb b/Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/Chapter9_8.ipynb new file mode 100644 index 00000000..5520ce18 --- /dev/null +++ b/Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/Chapter9_8.ipynb @@ -0,0 +1,419 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:36c7c29426c0c7be14ce2ec6430ec42fa1b0070a30e5ffbe18ec0fb6fd9b8d34" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter09:Numerical Solution of Partial Differential Equations" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex9.1:pg-350" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#standard five point formula\n", + "#example 9.1\n", + "#page 350\n", + "\n", + "u2=5.0;u3=1.0;\n", + "for i in range(0,3):\n", + " u1=(u2+u3+6.0)/4.0\n", + " u2=(u1/2.0)+(5.0/2.0)\n", + " u3=(u1/2.0)+(1.0/2.0)\n", + " print\" the values are u1=%d\\t u2=%d\\t u3=%d\\t\\n\\n\" %(u1,u2,u3)\n", + " \n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " the values are u1=3\t u2=4\t u3=2\t\n", + "\n", + "\n", + " the values are u1=3\t u2=4\t u3=2\t\n", + "\n", + "\n", + " the values are u1=3\t u2=4\t u3=2\t\n", + "\n", + "\n" + ] + } + ], + "prompt_number": 30 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex9.2:pg-351" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#solution of laplace equation by jacobi method,gauss-seidel method and SOR method\n", + "#example 9.2\n", + "#page 351\n", + "u1=0.25\n", + "u2=0.25\n", + "u3=0.5\n", + "u4=0.5 #initial values\n", + "print \"jacobis iteration process\\n\\n\"\n", + "print\"u1\\t u2\\t u3\\t u4\\t \\n\\n\"\n", + "print \"%f\\t %f\\t %f\\t %f\\t \\n\" %(u1,u2,u3,u4)\n", + "for i in range(0,7):\n", + " u11=(0+u2+0+u4)/4\n", + " u22=(u1+0+0+u3)/4\n", + " u33=(1+u2+0+u4)/4\n", + " u44=(1+0+u3+u1)/4\n", + " u1=u11\n", + " u2=u22\n", + " u3=u33\n", + " u4=u44\n", + " print \"%f\\t %f\\t %f\\t %f\\t \\n\" %(u11,u22,u33,u44) \n", + "print \" gauss seidel process\\n\\n\"\n", + "u1=0.25\n", + "u2=0.3125\n", + "u3=0.5625\n", + "u4=0.46875 #initial values\n", + "print \"u1\\t u2\\t u3\\t u4\\t \\n\\n\"\n", + "print \"%f\\t %f\\t %f\\t %f\\t \\n\" %(u1,u2,u3,u4)\n", + "for i in range(0,4):\n", + "\n", + " u1=(0.0+u2+0.0+u4)/4.0\n", + " u2=(u1+0.0+0.0+u3)/4.0\n", + " u3=(1.0+u2+0.0+u4)/4.0\n", + " u4=(1.0+0.0+u3+u1)/4.0\n", + " print \"%f\\t %f\\t %f\\t %f\\t \\n\" %(u1,u2,u3,u4) \n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "jacobis iteration process\n", + "\n", + "\n", + "u1\t u2\t u3\t u4\t \n", + "\n", + "\n", + "0.250000\t 0.250000\t 0.500000\t 0.500000\t \n", + "\n", + "0.187500\t 0.187500\t 0.437500\t 0.437500\t \n", + "\n", + "0.156250\t 0.156250\t 0.406250\t 0.406250\t \n", + "\n", + "0.140625\t 0.140625\t 0.390625\t 0.390625\t \n", + "\n", + "0.132812\t 0.132812\t 0.382812\t 0.382812\t \n", + "\n", + "0.128906\t 0.128906\t 0.378906\t 0.378906\t \n", + "\n", + "0.126953\t 0.126953\t 0.376953\t 0.376953\t \n", + "\n", + "0.125977\t 0.125977\t 0.375977\t 0.375977\t \n", + "\n", + " gauss seidel process\n", + "\n", + "\n", + "u1\t u2\t u3\t u4\t \n", + "\n", + "\n", + "0.250000\t 0.312500\t 0.562500\t 0.468750\t \n", + "\n", + "0.195312\t 0.189453\t 0.414551\t 0.402466\t \n", + "\n", + "0.147980\t 0.140633\t 0.385775\t 0.383439\t \n", + "\n", + "0.131018\t 0.129198\t 0.378159\t 0.377294\t \n", + "\n", + "0.126623\t 0.126196\t 0.375872\t 0.375624\t \n", + "\n" + ] + } + ], + "prompt_number": 51 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex9.4:pg-354" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#poisson equation\n", + "#exaample 9.4\n", + "#page 354\n", + "u2=0.0;u4=0.0;\n", + "print \" u1\\t u2\\t u3\\t u4\\t\\n\\n\"\n", + "for i in range(0,6):\n", + " u1=(u2/2.0)+30.0\n", + " u2=(u1+u4+150.0)/4.0\n", + " u4=(u2/2.0)+45.0\n", + " print \"%0.2f\\t %0.2f\\t %0.2f\\t %0.2f\\n\" %(u1,u2,u2,u4)\n", + "print \" from last two iterates we conclude u1=67 u2=75 u3=75 u4=83\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " u1\t u2\t u3\t u4\t\n", + "\n", + "\n", + "30.00\t 45.00\t 45.00\t 67.50\n", + "\n", + "52.50\t 67.50\t 67.50\t 78.75\n", + "\n", + "63.75\t 73.12\t 73.12\t 81.56\n", + "\n", + "66.56\t 74.53\t 74.53\t 82.27\n", + "\n", + "67.27\t 74.88\t 74.88\t 82.44\n", + "\n", + "67.44\t 74.97\t 74.97\t 82.49\n", + "\n", + " from last two iterates we conclude u1=67 u2=75 u3=75 u4=83\n", + "\n" + ] + } + ], + "prompt_number": 59 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex9.6:pg-362" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#bender-schmidt formula\n", + "#example 9.6\n", + "#page 362\n", + "def f(x):\n", + " return (4*x)-(x*x)\n", + "#u=[f(0),f(1),f(2),f(3),f(4)]\n", + "u1=f(0);u2=f(1);u3=f(2);u4=f(3);u5=f(4);\n", + "u11=(u1+u3)/2\n", + "u12=(u2+u4)/2\n", + "u13=(u3+u5)/2\n", + "print \"u11=%0.2f\\t u12=%0.2f\\t u13=%0.2f\\t \\n\" %(u11,u12,u13)\n", + "u21=(u1+u12)/2.0\n", + "u22=(u11+u13)/2.0\n", + "u23=(u12+0)/2.0\n", + "print \"u21=%0.2f\\t u22=%0.2f\\t u23=%0.2f\\t \\n\" %(u21,u22,u23)\n", + "u31=(u1+u22)/2.0\n", + "u32=(u21+u23)/2.0\n", + "u33=(u22+u1)/2.0\n", + "print \"u31=%0.2f\\t u32=%0.2f\\t u33=%0.2f\\t \\n\" % (u31,u32,u33)\n", + "u41=(u1+u32)/2.0\n", + "u42=(u31+u33)/2.0\n", + "u43=(u32+u1)/2.0\n", + "print \"u41=%0.2f\\t u42=%0.2f\\t u43=%0.2f\\t \\n\" % (u41,u42,u43)\n", + "u51=(u1+u42)/2.0\n", + "u52=(u41+u43)/2.0\n", + "u53=(u42+u1)/2.0\n", + "print \"u51=%0.2f\\t u52=%0.2f\\t u53=%0.2f\\t \\n\" % (u51,u52,u53)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "u11=2.00\t u12=3.00\t u13=2.00\t \n", + "\n", + "u21=1.50\t u22=2.00\t u23=1.50\t \n", + "\n", + "u31=1.00\t u32=1.50\t u33=1.00\t \n", + "\n", + "u41=0.75\t u42=1.00\t u43=0.75\t \n", + "\n", + "u51=0.50\t u52=0.75\t u53=0.50\t \n", + "\n" + ] + } + ], + "prompt_number": 77 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex9.7:pg-363" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "#bender-schimdt's formula and crank-nicolson formula\n", + "#example 9.7\n", + "#page 363\n", + "#bender -schimdt's formula\n", + "import math\n", + "from numpy import matrix\n", + "z=math.pi\n", + "def f(x,t):\n", + " return math.exp(z*z*t*-1)*sin(z*x)\n", + "#u=[f(0,0),f(0.2,0),f(0.4,0),f(0.6,0),f(0.8,0),f(1,0)]\n", + "u1=f(0,0)\n", + "u2=f(0.2,0)\n", + "u3=f(0.4,0)\n", + "u4=f(0.6,0)\n", + "u5=f(0.8,0)\n", + "u6=f(1.0,0)\n", + "u11=u3/2\n", + "u12=(u2+u4)/2\n", + "u13=u12\n", + "u14=u11\n", + "print \"u11=%f\\t u12=%f\\t u13=%f\\t u14=%f\\n\\n\" % (u11,u12,u13,u14)\n", + "u21=u12/2\n", + "u22=(u12+u14)/2\n", + "u23=u22\n", + "u24=u21\n", + "print \"u21=%f\\t u22=%f\\t u23=%f\\t u24=%f\\n\\n\" % (u21,u22,u23,u24)\n", + "print \"the error in the solution is: %f\\n\\n\" % (math.fabs(u22-f(0.6,0.04)))\n", + "#crank-nicolson formula\n", + "#by putting i=1,2,3,4 we obtain four equation\n", + "A=matrix([[4, -1, 0, 0] ,[-1, 4, -1, 0],[0, -1, 4, -1],[0, 0, -1, 4]])\n", + "C=matrix([[0.9510],[1.5388],[1.5388],[0.9510]])\n", + "X=A.I*C\n", + "print \"u00=%f\\t u10=%f\\t u20=%f\\t u30=%f\\t\\n\\n\" %(X[0][0],X[1][0],X[2][0],X[3][0])\n", + "print \"the error in the solution is: %f\\n\\n\" %(abs(X[1][0]-f(0.6,0.04)))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "u11=0.475528\t u12=0.769421\t u13=0.769421\t u14=0.475528\n", + "\n", + "\n", + "u21=0.384710\t u22=0.622475\t u23=0.622475\t u24=0.384710\n", + "\n", + "\n", + "the error in the solution is: 0.018372\n", + "\n", + "\n", + "u00=0.399255\t u10=0.646018\t u20=0.646018\t u30=0.399255\t\n", + "\n", + "\n", + "the error in the solution is: 0.005172\n", + "\n", + "\n" + ] + } + ], + "prompt_number": 28 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex9.8:pg-364" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "#heat equation using crank-nicolson method\n", + "#example 9.8\n", + "#page 364\n", + "from numpy import matrix\n", + "import math\n", + "z=0.01878\n", + "#h=1/2 l=1/8,i=1\n", + "u01=0.0\n", + "u21=1.0/8.0\n", + "u11=(u21+u01)/6.0\n", + "print \" u11=%f\\n\\n\" % (u11)\n", + "print \"error is %f\\n\\n\" % (math.fabs(u11-z))\n", + "#h=1/4,l=1/8,i=1,2,3\n", + "A=matrix([[-3.0 ,-1.0 ,0.0],[1.0,-3.0,1.0],[0.0,1.0,-3.0]])\n", + "C=matrix([[0.0],[0.0],[-0.125]])\n", + "#here we found inverese of A then we multipy it with C\n", + "X=A.I*C\n", + "print \"u12=%f\\n\\n\" % (X[1][0])\n", + "print \"error is %f\\n\\n\" %(math.fabs(X[1][0]-z))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " u11=0.020833\n", + "\n", + "\n", + "error is 0.002053\n", + "\n", + "\n", + "u12=0.013889\n", + "\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "error is 0.004891\n", + "\n", + "\n" + ] + } + ], + "prompt_number": 1 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter1_8.ipynb b/Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter1_8.ipynb new file mode 100644 index 00000000..29a46a36 --- /dev/null +++ b/Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter1_8.ipynb @@ -0,0 +1,625 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:6ce091ab8e1fc1b8237507a6211c6c4c45a07ba8baa4f3364e4633c5bb15666c" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter01:Errors in Numerical Calculations" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex1.1:pg-7" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 1.1\n", + "#rounding off\n", + "#page 7\n", + "a1=1.6583\n", + "a2=30.0567\n", + "a3=0.859378\n", + "a4=3.14159\n", + "print \"\\nthe numbers after rounding to 4 significant figures are given below\\n\"\n", + "print \" %f %.4g\\n'\" %(a1,a1)\n", + "print \" %f %.4g\\n\" %(a2,a2)\n", + "print \" %f %.4g\\n\" %(a3,a3)\n", + "print \" %f %.4g\\n\" %(a4,a4)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "the numbers after rounding to 4 significant figures are given below\n", + "\n", + " 1.658300 1.658\n", + "'\n", + " 30.056700 30.06\n", + "\n", + " 0.859378 0.8594\n", + "\n", + " 3.141590 3.142\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex1.2:pg-9" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 1.2\n", + "#percentage accuracy\n", + "#page 9\n", + "import math\n", + "x=0.51 # the number given\n", + "n=2 #correcting upto 2 decimal places\n", + "d=math.pow(10,-n)\n", + "d=d/2.0\n", + "p=(d/x)*100 #percentage accuracy\n", + "print \"the percentage accuracy of %f after correcting to two decimal places is %f\" %(x,p)\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the percentage accuracy of 0.510000 after correcting to two decimal places is 0.980392\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex1.3:pg-9" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 1.3\n", + "#absolute and relative errors\n", + "#page 9\n", + "X=3.1428571 #approximate value of pi\n", + "T_X=3.1415926 # true value of pi\n", + "A_E=T_X-X #absolute error\n", + "R_E=A_E/T_X #relative error\n", + "print \"Absolute Error = %0.7f \\n Relative Error = %0.7f\" %(A_E,R_E)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Absolute Error = -0.0012645 \n", + " Relative Error = -0.0004025\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex1.4:pg-10" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 1.4\n", + "#best approximation\n", + "#page 10\n", + "X=1/3 #the actual number\n", + "X1=0.30\n", + "X2=0.33\n", + "X3=0.34\n", + "E1=abs(X-X1)\n", + "E2=abs(X-X2)\n", + "E3=abs(X-X3)\n", + "if E1d:\n", + " m=(x1+x2)/2.0\n", + " print \" \\t%f\\t%f\\t%f\\t%f\\n\" %(x1,x2,m,f(m))\n", + " if f(m)*f(x1)>0:\n", + " x1=m\n", + " else:\n", + " x2=m \n", + " c=c+1;# to count number of iterations \n", + "print \"the solution of equation after %i iteration is %0.4g\" %(c,m)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Succesive approximations \t x1\t \tx2\t \tm\t \tf(m)\n", + "\n", + " \t2.000000\t3.000000\t2.500000\t5.625000\n", + "\n", + " \t2.000000\t2.500000\t2.250000\t1.890625\n", + "\n", + " \t2.000000\t2.250000\t2.125000\t0.345703\n", + "\n", + " \t2.000000\t2.125000\t2.062500\t-0.351318\n", + "\n", + " \t2.062500\t2.125000\t2.093750\t-0.008942\n", + "\n", + " \t2.093750\t2.125000\t2.109375\t0.166836\n", + "\n", + " \t2.093750\t2.109375\t2.101562\t0.078562\n", + "\n", + " \t2.093750\t2.101562\t2.097656\t0.034714\n", + "\n", + " \t2.093750\t2.097656\t2.095703\t0.012862\n", + "\n", + " \t2.093750\t2.095703\t2.094727\t0.001954\n", + "\n", + " \t2.093750\t2.094727\t2.094238\t-0.003495\n", + "\n", + " \t2.094238\t2.094727\t2.094482\t-0.000771\n", + "\n", + " \t2.094482\t2.094727\t2.094604\t0.000592\n", + "\n", + " \t2.094482\t2.094604\t2.094543\t-0.000090\n", + "\n", + "the solution of equation after 15 iteration is 2.095\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex2.3:pg-26" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 2.3\n", + "#bisection method\n", + "#page 26\n", + "import math\n", + "def f(x):\n", + " return math.pow(x,3)+math.pow(x,2)+x+7\n", + "x1=-3\n", + "x2=-2 #f(-3) is negative and f(-2) is positive\n", + "d=0.0001 #for accuracy of root\n", + "c=1\n", + "print \"Succesive approximations \\t x1\\t \\tx2\\t \\tm\\t \\tf(m)\\n\"\n", + "while abs(x1-x2)>d:\n", + " m=(x1+x2)/2.0\n", + " print \" \\t%f\\t%f\\t%f\\t%f\\n\" %(x1,x2,m,f(m))\n", + " if f(m)*f(x1)>0:\n", + " x1=m\n", + " else:\n", + " x2=m \n", + " c=c+1 # to count number of iterations \n", + "print \"the solution of equation after %i iteration is %0.4g\" %(c,m)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Succesive approximations \t x1\t \tx2\t \tm\t \tf(m)\n", + "\n", + " \t-3.000000\t-2.000000\t-2.500000\t-4.875000\n", + "\n", + " \t-2.500000\t-2.000000\t-2.250000\t-1.578125\n", + "\n", + " \t-2.250000\t-2.000000\t-2.125000\t-0.205078\n", + "\n", + " \t-2.125000\t-2.000000\t-2.062500\t0.417725\n", + "\n", + " \t-2.125000\t-2.062500\t-2.093750\t0.111481\n", + "\n", + " \t-2.125000\t-2.093750\t-2.109375\t-0.045498\n", + "\n", + " \t-2.109375\t-2.093750\t-2.101562\t0.033315\n", + "\n", + " \t-2.109375\t-2.101562\t-2.105469\t-0.006010\n", + "\n", + " \t-2.105469\t-2.101562\t-2.103516\t0.013673\n", + "\n", + " \t-2.105469\t-2.103516\t-2.104492\t0.003836\n", + "\n", + " \t-2.105469\t-2.104492\t-2.104980\t-0.001086\n", + "\n", + " \t-2.104980\t-2.104492\t-2.104736\t0.001376\n", + "\n", + " \t-2.104980\t-2.104736\t-2.104858\t0.000145\n", + "\n", + " \t-2.104980\t-2.104858\t-2.104919\t-0.000470\n", + "\n", + "the solution of equation after 15 iteration is -2.105\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex2.4:pg-26" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 2.4\n", + "#bisection method\n", + "#page 26\n", + "import math\n", + "def f(x):\n", + " return x*math.exp(x)-1\n", + "x1=0 \n", + "x2=1 #f(0) is negative and f(1) is positive\n", + "d=0.0005 #maximun tolerance value\n", + "c=1\n", + "print \"Succesive approximations \\t x1\\t \\tx2\\t \\tm\\t \\ttol\\t \\tf(m)\\n\"\n", + "while abs((x2-x1)/x2)>d:\n", + " m=(x1+x2)/2.0 #tolerance value for each iteration\n", + " tol=((x2-x1)/x2)*100\n", + " print \" \\t%f\\t%f\\t%f\\t%f\\t%f\\n\" %(x1,x2,m,tol,f(m))\n", + " if f(m)*f(x1)>0:\n", + " x1=m\n", + " else:\n", + " x2=m \n", + " c=c+1 # to count number of iterations \n", + "print \"the solution of equation after %i iteration is %0.4g\" %(c,m)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Succesive approximations \t x1\t \tx2\t \tm\t \ttol\t \tf(m)\n", + "\n", + " \t0.000000\t1.000000\t0.500000\t100.000000\t-0.175639\n", + "\n", + " \t0.500000\t1.000000\t0.750000\t50.000000\t0.587750\n", + "\n", + " \t0.500000\t0.750000\t0.625000\t33.333333\t0.167654\n", + "\n", + " \t0.500000\t0.625000\t0.562500\t20.000000\t-0.012782\n", + "\n", + " \t0.562500\t0.625000\t0.593750\t10.000000\t0.075142\n", + "\n", + " \t0.562500\t0.593750\t0.578125\t5.263158\t0.030619\n", + "\n", + " \t0.562500\t0.578125\t0.570312\t2.702703\t0.008780\n", + "\n", + " \t0.562500\t0.570312\t0.566406\t1.369863\t-0.002035\n", + "\n", + " \t0.566406\t0.570312\t0.568359\t0.684932\t0.003364\n", + "\n", + " \t0.566406\t0.568359\t0.567383\t0.343643\t0.000662\n", + "\n", + " \t0.566406\t0.567383\t0.566895\t0.172117\t-0.000687\n", + "\n", + " \t0.566895\t0.567383\t0.567139\t0.086059\t-0.000013\n", + "\n", + "the solution of equation after 13 iteration is 0.5671\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex2.5:pg-27" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 2.5\n", + "#bisection method\n", + "#page 27\n", + "import math\n", + "def f(x):\n", + " return 4*math.exp(-x)*math.sin(x)-1\n", + "x1=0 \n", + "x2=0.5 #f(0) is negative and f(1) is positive\n", + "d=0.0001 #for accuracy of root\n", + "c=1 \n", + "print \"Succesive approximations \\t x1\\t \\tx2\\t \\tm\\t \\t \\tf(m)\\n\"\n", + "while abs(x2-x1)>d:\n", + " m=(x1+x2)/2.0\n", + " print \" \\t%f\\t%f\\t%f\\t%f\\n\" %(x1,x2,m,f(m))\n", + " if f(m)*f(x1)>0:\n", + " x1=m\n", + " else:\n", + " x2=m \n", + " c=c+1 # to count number of iterations \n", + "print \"the solution of equation after %i iteration is %0.3g\" %(c,m)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Succesive approximations \t x1\t \tx2\t \tm\t \t \tf(m)\n", + "\n", + " \t0.000000\t0.500000\t0.250000\t-0.229286\n", + "\n", + " \t0.250000\t0.500000\t0.375000\t0.006941\n", + "\n", + " \t0.250000\t0.375000\t0.312500\t-0.100293\n", + "\n", + " \t0.312500\t0.375000\t0.343750\t-0.044068\n", + "\n", + " \t0.343750\t0.375000\t0.359375\t-0.017925\n", + "\n", + " \t0.359375\t0.375000\t0.367188\t-0.005334\n", + "\n", + " \t0.367188\t0.375000\t0.371094\t0.000842\n", + "\n", + " \t0.367188\t0.371094\t0.369141\t-0.002236\n", + "\n", + " \t0.369141\t0.371094\t0.370117\t-0.000694\n", + "\n", + " \t0.370117\t0.371094\t0.370605\t0.000075\n", + "\n", + " \t0.370117\t0.370605\t0.370361\t-0.000310\n", + "\n", + " \t0.370361\t0.370605\t0.370483\t-0.000118\n", + "\n", + " \t0.370483\t0.370605\t0.370544\t-0.000022\n", + "\n", + "the solution of equation after 14 iteration is 0.371\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex2.6:pg-28" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 2.6\n", + "#false position method\n", + "#page 28\n", + "import math\n", + "def f(x):\n", + " return x**3-2*x-5\n", + "a=2.0\n", + "b=3.0 #f(2) is negative and f(3)is positive\n", + "d=0.00001\n", + "print \"succesive iterations \\ta\\t b\\t f(a)\\t f(b)\\t\\ x1\\n\"\n", + "for i in range(1,25):\n", + " x1=b*f(a)/(f(a)-f(b))+a*f(b)/(f(b)-f(a))\n", + " if(f(a)*f(x1))>0:\n", + " b=x1\n", + " else:\n", + " a=x1\n", + " if abs(f(x1))0:\n", + " b=x1\n", + " else:\n", + " a=x1\n", + " if abs(f(x1))0:\n", + " b=x1\n", + " else:\n", + " a=x1\n", + " if abs(f(x1))0:\n", + " b=x1\n", + " else:\n", + " a=x1\n", + " if abs(f(x1))d:\n", + " print \" \\t%f %f\\n\" %(x1,f(x1))\n", + " x2=x1\n", + " x1=f(x1)\n", + " c=c+1\n", + "print \" the root of the eqaution after %i iteration is %0.4g\" %(c,x1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "successive iterations \t\u0001\tf(x1)\n", + "\n", + " \t0.750000 0.755929\n", + "\n", + " \t0.755929 0.754652\n", + "\n", + " \t0.754652 0.754926\n", + "\n", + " \t0.754926 0.754867\n", + "\n", + " the root of the eqaution after 4 iteration is 0.7549\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex2.11:pg-34" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 2.11\n", + "#iteration method\n", + "#page34\n", + "import math\n", + "def f(x):\n", + " return cos(x)/2.0+3.0/2.0\n", + "x1=1.5 # as roots lies between 3/2 and pi/2\n", + "x2=0\n", + "d=0.0001 # accuracy opto 10^-4\n", + "c=0 # to count no of iterations \n", + "print \"successive iterations \\t\\x01\\tf(x1)\\n\"\n", + "while abs(x2-x1)>d:\n", + " \n", + " print \" \\t%f %f\\n\" %(x1,f(x1))\n", + " x2=x1\n", + " x1=f(x1)\n", + " c=c+1\n", + "print \" the root of the eqaution after %i iteration is %0.4g\" %(c,x1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "successive iterations \t\u0001\tf(x1)\n", + "\n", + " \t1.500000 1.535369\n", + "\n", + " \t1.535369 1.517710\n", + "\n", + " \t1.517710 1.526531\n", + "\n", + " \t1.526531 1.522126\n", + "\n", + " \t1.522126 1.524326\n", + "\n", + " \t1.524326 1.523227\n", + "\n", + " \t1.523227 1.523776\n", + "\n", + " \t1.523776 1.523502\n", + "\n", + " \t1.523502 1.523639\n", + "\n", + " \t1.523639 1.523570\n", + "\n", + " the root of the eqaution after 10 iteration is 1.524\n" + ] + } + ], + "prompt_number": 28 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex2.12:pg-35" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 2.12\n", + "#iteration method\n", + "#page 35\n", + "import math\n", + "def f(x):\n", + " return math.exp(-x)\n", + "x1=1.5 # as roots lies between 0 and 1\n", + "x2=0\n", + "d=0.0001 # accuracy opto 10^-4\n", + "c=0 # to count no of iterations \n", + "print \"successive iterations \\t x1 \\t f(x1)\\n\"\n", + "while abs(x2-x1)>d:\n", + " \n", + " print \" \\t%f %f\\n\" %(x1,f(x1))\n", + " x2=x1\n", + " x1=f(x1)\n", + " c=c+1\n", + "print \" the root of the eqaution after %i iteration is %0.4g\" %(c,x1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "successive iterations \t x1 \t f(x1)\n", + "\n", + " \t1.500000 0.223130\n", + "\n", + " \t0.223130 0.800011\n", + "\n", + " \t0.800011 0.449324\n", + "\n", + " \t0.449324 0.638059\n", + "\n", + " \t0.638059 0.528317\n", + "\n", + " \t0.528317 0.589597\n", + "\n", + " \t0.589597 0.554551\n", + "\n", + " \t0.554551 0.574330\n", + "\n", + " \t0.574330 0.563082\n", + "\n", + " \t0.563082 0.569451\n", + "\n", + " \t0.569451 0.565836\n", + "\n", + " \t0.565836 0.567885\n", + "\n", + " \t0.567885 0.566723\n", + "\n", + " \t0.566723 0.567382\n", + "\n", + " \t0.567382 0.567008\n", + "\n", + " \t0.567008 0.567220\n", + "\n", + " \t0.567220 0.567100\n", + "\n", + " \t0.567100 0.567168\n", + "\n", + " the root of the eqaution after 18 iteration is 0.5672\n" + ] + } + ], + "prompt_number": 34 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex2.13:pg-35" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 2.13\n", + "#iteration method\n", + "#page 35\n", + "import math\n", + "def f(x):\n", + " return 1+math.sin(x)/10\n", + "x1=1.0 # as roots lies between 1 and pi evident from graph\n", + "x2=0\n", + "d=0.0001 # accuracy opto 10^-4\n", + "c=0 # to count no of iterations \n", + "print \"successive iterations \\t x1 \\t f(x1)\\n\"\n", + "while abs(x2-x1)>d:\n", + " print \" \\t%f %f\\n\" %(x1,f(x1))\n", + " x2=x1\n", + " x1=f(x1)\n", + " c=c+1\n", + "print \" the root of the eqaution after %i iteration is %0.4g\" %(c,x1)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "successive iterations \t x1 \t f(x1)\n", + "\n", + " \t1.000000 1.084147\n", + "\n", + " \t1.084147 1.088390\n", + "\n", + " \t1.088390 1.088588\n", + "\n", + " \t1.088588 1.088597\n", + "\n", + " the root of the eqaution after 4 iteration is 1.089\n" + ] + } + ], + "prompt_number": 36 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex2.14:pg-36" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 2.14\n", + "#aitken's process\n", + "#page 36\n", + "import math\n", + "def f(x):\n", + " return 1.5+math.cos(x)/2.0\n", + "x0=1.5\n", + "y=0\n", + "e=0.0001\n", + "c=0\n", + "print \"successive iterations \\t x0 \\t x1 \\t x2 \\t x3 \\t y\\n\"\n", + "for i in range(1,10):\n", + " x1=f(x0)\n", + " x2=f(x1)\n", + " x3=f(x2)\n", + " y=x3-((x3-x2)**2)/(x3-2*x2+x1)\n", + " d=y-x0\n", + " x0=y\n", + " if abs(f(x0))0:\n", + " x2=x3;\n", + " else:\n", + " x1=x3 \n", + " if abs(f(x3))<0.000001: \n", + " break\n", + " c=c+1\n", + "print \"the root of the equation after %i iteration is: %f\" %(c,x3)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "successive iterations \t x1 \t x2 \t x3 \t f(x3)\n", + "\n", + " \t2.000000 \t3.000000 \t2.058824 \t-0.390800\n", + "\n", + " \t2.000000 \t2.058824 \t2.096559 \t0.022428\n", + "\n", + " \t2.096559 \t2.058824 \t2.094511 \t-0.000457\n", + "\n", + " \t2.094511 \t2.058824 \t2.094552 \t0.000009\n", + "\n", + " \t2.094552 \t2.058824 \t2.094551 \t-0.000000\n", + "\n", + "the root of the equation after 4 iteration is: 2.094551\n" + ] + } + ], + "prompt_number": 29 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex2.26:pg-50" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 2.26\n", + "#secant method\n", + "#page 50\n", + "import math\n", + "from __future__ import division\n", + "def f(x):\n", + " return x*math.exp(x)-1\n", + "x1=0\n", + "x2=1 # initial values\n", + "n=1\n", + "c=0 \n", + "print \"successive iterations \\t x1 \\t x2 \\t x3 \\t f(x3)\\n\"\n", + "while n==1:\n", + " x3=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1)) \n", + " print \" \\t%f \\t%f \\t%f \\t%f\\n\" %(x1,x2,x3,f(x3))\n", + " if f(x3)*f(x1)>0:\n", + " x2=x3\n", + " else:\n", + " x1=x3 \n", + " if abs(f(x3))<0.0001:\n", + " break\n", + " c=c+1\n", + "print \"the root of the equation after %i iteration is: %0.4g\" %(c,x3)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "successive iterations \t x1 \t x2 \t x3 \t f(x3)\n", + "\n", + " \t0.000000 \t1.000000 \t0.367879 \t-0.468536\n", + "\n", + " \t0.000000 \t0.367879 \t0.692201 \t0.383091\n", + "\n", + " \t0.692201 \t0.367879 \t0.546310 \t-0.056595\n", + "\n", + " \t0.546310 \t0.367879 \t0.570823 \t0.010200\n", + "\n", + " \t0.570823 \t0.367879 \t0.566500 \t-0.001778\n", + "\n", + " \t0.566500 \t0.367879 \t0.567256 \t0.000312\n", + "\n", + " \t0.567256 \t0.367879 \t0.567124 \t-0.000055\n", + "\n", + "the root of the equation after 6 iteration is: 0.5671\n" + ] + } + ], + "prompt_number": 31 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex2.27:pg-52" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# example 2.27\n", + "#mulller's method\n", + "#page 52\n", + "from __future__ import division\n", + "import math\n", + "def f(x):\n", + " return x**3-x-1\n", + "x0=0\n", + "x1=1\n", + "x2=2 # initial values\n", + "n=1\n", + "c=0\n", + "print \"successive iterations \\t x0 \\t x1 \\t x2 \\t f(x0)\\t f(x1)\\t f(x2)\\n\"\n", + "while n==1: \n", + " c=c+1\n", + " y0=f(x0)\n", + " y1=f(x1)\n", + " y2=f(x2)\n", + " h2=x2-x1\n", + " h1=x1-x0\n", + " d2=f(x2)-f(x1)\n", + " d1=f(x1)-f(x0)\n", + " print \" \\t%f\\t %f\\t %f\\t %f\\t %f\\t %f\\n\" %(x0,x1,x2,f(x0),f(x1),f(x2))\n", + " A=(d2/h2-d1/h1)/(h1+h2)\n", + " B=d2/h2+A*h2\n", + " S=math.sqrt(B**2-4*A*f(x2))\n", + " x3=x2-(2*f(x2))/(B+S)\n", + " E=abs((x3-x2)/x2)*100\n", + " if E<0.003:\n", + " break\n", + " else:\n", + " if c==1:\n", + " x2=x3\n", + " if c==2:\n", + " x1=x2\n", + " x2=x3\n", + " if c==3:\n", + " x0=x1\n", + " x1=x2\n", + " x2=x3\n", + " if c==3:\n", + " c=0\n", + "print \"the required root is : %0.4f\" %(x3)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "successive iterations \t x0 \t x1 \t x2 \t f(x0)\t f(x1)\t f(x2)\n", + "\n", + " \t0.000000\t 1.000000\t 2.000000\t -1.000000\t -1.000000\t 5.000000\n", + "\n", + " \t0.000000\t 1.000000\t 1.263763\t -1.000000\t -1.000000\t -0.245412\n", + "\n", + " \t0.000000\t 1.263763\t 1.331711\t -1.000000\t -0.245412\t 0.030015\n", + "\n", + " \t1.263763\t 1.331711\t 1.324583\t -0.245412\t 0.030015\t -0.000574\n", + "\n", + " \t1.263763\t 1.331711\t 1.324718\t -0.245412\t 0.030015\t -0.000000\n", + "\n", + "the required root is : 1.3247\n" + ] + } + ], + "prompt_number": 35 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex2.28:pg-55" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#graeffe's method\n", + "#example 2.28\n", + "#page 55\n", + "import math\n", + "from __future__ import division\n", + "def f(x):\n", + " return x**3-6*(x**2)+11*x-6\n", + "#x=poly(0,'x')\n", + "#g=f(-x)\n", + "print \"the equation is:\\n\"\n", + "A=[1, 14, 49, 36] #coefficients of the above equation\n", + "print \"%0.4g\\n\" %(math.sqrt(A[3]/A[2]))\n", + "print \"%0.4g\\n\" %(math.sqrt(A[2]/A[1]))\n", + "print \"%0.4g\\n\" %(math.sqrt(A[1]/A[0]))\n", + "print \"the equation is:\\n\"\n", + "#disp(g*(-1*g));\n", + "B=[1, 98, 1393, 1296]\n", + "print \"%0.4g\\n\" %((B[3]/B[2])**(1/4))\n", + "print \"%0.4g\\n\" %((B[2]/B[1])**(1/4))\n", + "print \"%0.4g\\n\" %((B[1]/B[0])**(1/4))\n", + "print \"It is apparent from the outputs that the roots converge at 1 2 3\"\n", + "\n", + "\n", + "\n", + "#INCOMPLETE" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the equation is:\n", + "\n", + "0.8571\n", + "\n", + "1.871\n", + "\n", + "3.742\n", + "\n", + "the equation is:\n", + "\n", + "0.9821\n", + "\n", + "1.942\n", + "\n", + "3.146\n", + "\n", + "It is apparent from the outputs that the roots converge at 1 2 3\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex2.29:pg-57" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#quadratic factor by lin's--bairsttow method\n", + "#example 2.29\n", + "#page 57\n", + "from numpy import matrix\n", + "from __future__ import division\n", + "def f(x):\n", + " return x**3-x-1\n", + "a=[-1, -1, 0, 1]\n", + "r1=1\n", + "s1=1\n", + "b4=a[3]\n", + "def f3(r):\n", + " return a[2]-r*a[3]\n", + "def f2(r,s):\n", + " return a[1]-r*a[2]+r**2*a[3]-s*a[3]\n", + "def f1(r,s):\n", + " return a[0]-s*a[2]+s*r*a[3]\n", + "A=matrix([[1,1],[2,-1]])\n", + "C=matrix([[0],[1]])\n", + "X=A.I*C\n", + "X1=[[ 0.33333333],[-0.33333333]]\n", + "dr=X1[0][0]\n", + "ds=X1[1][0]\n", + "r2=r1+dr\n", + "s2=s1+ds\n", + "#second pproximation\n", + "r1=r2\n", + "s1=s2\n", + "b11=f1(r2,s2)\n", + "b22=f2(r2,s2)\n", + "h=0.001\n", + "dr_b1=(f1(r1+h,s1)-f1(r1,s1))/h\n", + "ds_b1=(f1(r1,s1+h)-f1(r1,s1))/h\n", + "dr_b2=(f2(r1+h,s1)-f2(r1,s1))/h\n", + "ds_b2=(f2(r1,s1+h)-f2(r1,s1))/h\n", + "A=matrix([[dr_b1,ds_b1],[dr_b2,ds_b2]])\n", + "C=matrix([[-f1(r1,s1)],[-f2(r1,s2)]])\n", + "X=A.I*C\n", + "r2=r1+X[0][0]\n", + "s2=s1+X[1][0]\n", + "print \"roots correct to 3 decimal places are : %0.3f %0.3f\" %(r2,s2)\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "roots correct to 3 decimal places are : 1.325 0.754\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex2.31:pg-62" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#method of iteration\n", + "#example 2.31\n", + "#page 62\n", + "from __future__ import division\n", + "def f(x,y):\n", + " return (3*y*x**2+7)/10\n", + "def g(x,y):\n", + " return (y**2+4)/5\n", + "h=0.0001\n", + "x0=0.5\n", + "y0=0.5\n", + "f1_dx=(f(x0+h,y0)-f(x0,y0))/h\n", + "f1_dy=(f(x0,y0+h)-f(x0,y0))/h\n", + "g1_dx=(g(x0+h,y0)-g(x0,y0))/h\n", + "g1_dy=(g(x0+h,y0)-g(x0,y0))/h\n", + "if (f1_dx+f1_dy<1) and (g1_dx+g1_dy<1): \n", + " print \"coditions for convergence is satisfied\\n\\n\"\n", + "print \"X \\t Y\\t\\n\\n\"\n", + "for i in range(0,10):\n", + " X=(3*y0*x0**2+7)/10\n", + " Y=(y0**2+4)/5\n", + " print \"%f\\t %f\\t\\n\" %(X,Y)\n", + " x0=X\n", + " y0=Y\n", + "print \"\\n\\n CONVERGENCE AT (1 1) IS OBVIOUS\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "coditions for convergence is satisfied\n", + "\n", + "\n", + "X \t Y\t\n", + "\n", + "\n", + "0.737500\t 0.850000\t\n", + "\n", + "0.838696\t 0.944500\t\n", + "\n", + "0.899312\t 0.978416\t\n", + "\n", + "0.937391\t 0.991460\t\n", + "\n", + "0.961360\t 0.996598\t\n", + "\n", + "0.976320\t 0.998642\t\n", + "\n", + "0.985572\t 0.999457\t\n", + "\n", + "0.991247\t 0.999783\t\n", + "\n", + "0.994707\t 0.999913\t\n", + "\n", + "0.996807\t 0.999965\t\n", + "\n", + "\n", + "\n", + " CONVERGENCE AT (1 1) IS OBVIOUS\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex2.32:pg-65" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#newton raphson metho\n", + "#example 2.32\n", + "#page 65\n", + "import numpy\n", + "def f(x,y):\n", + " return 3*y*x**2-10*x+7\n", + "def g(y):\n", + " return y**2-5*y+4\n", + "hh=0.0001\n", + "x0=0.5\n", + "y0=0.5 #initial values\n", + "f0=f(x0,y0)\n", + "g0=g(y0)\n", + "df_dx=(f(x0+hh,y0)-f(x0,y0))/hh\n", + "df_dy=(f(x0,y0+hh)-f(x0,y0))/hh\n", + "dg_dx=(g(y0)-g(y0))/hh\n", + "dg_dy=(g(y0+hh)-g(y0))/hh\n", + "d=[[df_dx,df_dy],[dg_dx,dg_dy]]\n", + "D1=numpy.linalg.det(d)\n", + "dd=[[-f0,df_dy],[-g0,dg_dy]]\n", + "h=numpy.linalg.det(dd)/D1\n", + "ddd=[[df_dx,-f0],[dg_dx,-g0]]\n", + "k=numpy.linalg.det(ddd)/D1;\n", + "x1=x0+h\n", + "y1=y0+k\n", + "f0=f(x1,y1)\n", + "g0=g(y1)\n", + "df_dx=(f(x1+hh,y1)-f(x1,y1))/hh\n", + "df_dy=(f(x1,y1+hh)-f(x1,y1))/hh\n", + "dg_dx=(g(y1)-g(y1))/hh\n", + "dg_dy=(g(y1+hh)-g(y1))/hh\n", + "dddd=[[df_dx,df_dy],[dg_dx,dg_dy]]\n", + "D2=numpy.linalg.det(dddd)\n", + "ddddd=[[-f0,df_dy],[-g0,dg_dy]]\n", + "h=numpy.linalg.det(ddddd)/D2\n", + "d6=[[df_dx,-f0],[dg_dx,-g0]]\n", + "k=numpy.linalg.det(d6)/D2\n", + "x2=x1+h\n", + "y2=y1+k\n", + "print \" the roots of the equation are x2=%f and y2=%f\" %(x2,y2)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " the roots of the equation are x2=0.970803 and y2=0.998752\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex2.33:pg-66" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#newton raphson method\n", + "#example 2.33\n", + "#page 66\n", + "import math\n", + "import numpy\n", + "def f(x,y):\n", + " return x**2+y**2-1\n", + "def g(x,y):\n", + " return y-x**2\n", + "hh=0.0001\n", + "x0=0.7071\n", + "y0=0.7071 #initial values\n", + "f0=f(x0,y0)\n", + "g0=g(x0,y0)\n", + "df_dx=(f(x0+hh,y0)-f(x0,y0))/hh\n", + "df_dy=(f(x0,y0+hh)-f(x0,y0))/hh\n", + "dg_dx=(g(x0+hh,y0)-g(x0,y0))/hh\n", + "dg_dy=(g(x0,y0+hh)-g(x0,y0))/hh\n", + "D1=numpy.linalg.det([[df_dx,df_dy],[dg_dx,dg_dy]])\n", + "h=numpy.linalg.det([[-f0,df_dy],[-g0,dg_dy]])/D1\n", + "k=numpy.linalg.det([[df_dx,-f0],[dg_dx,-g0]])/D1\n", + "x1=x0+h\n", + "y1=y0+k\n", + "f0=f(x1,y1)\n", + "g0=g(x1,y1)\n", + "df_dx=(f(x1+hh,y1)-f(x1,y1))/hh\n", + "df_dy=(f(x1,y1+hh)-f(x1,y1))/hh\n", + "dg_dx=(g(x1+hh,y1)-g(x1,y1))/hh\n", + "dg_dy=(g(x1,y1+hh)-g(x1,y1))/hh\n", + "D2=numpy.linalg.det([[df_dx,df_dy],[dg_dx,dg_dy]])\n", + "h=numpy.linalg.det([[-f0,df_dy],[-g0,dg_dy]])/D2\n", + "k=numpy.linalg.det([[df_dx,-f0],[dg_dx,-g0]])/D2\n", + "x2=x1+h\n", + "y2=y1+k\n", + "print \"the roots of the equation are x2=%f and y2=%f \" %(x2,y2)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the roots of the equation are x2=0.786184 and y2=0.618039 \n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex2.34:pg-67" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#newton raphson method\n", + "#example 2.34\n", + "#page 67\n", + "import math\n", + "import numpy\n", + "def f(x,y):\n", + " return math.sin(x)-y+0.9793\n", + "def g(x,y):\n", + " return math.cos(y)-x+0.6703\n", + "hh=0.0001\n", + "x0=0.5\n", + "y0=1.5 #initial values\n", + "f0=f(x0,y0)\n", + "g0=g(x0,y0)\n", + "df_dx=(f(x0+hh,y0)-f(x0,y0))/hh\n", + "df_dy=(f(x0,y0+hh)-f(x0,y0))/hh\n", + "dg_dx=(g(x0+hh,y0)-g(x0,y0))/hh\n", + "dg_dy=(g(x0,y0+hh)-g(x0,y0))/hh\n", + "d1=[[df_dx,df_dy],[dg_dx,dg_dy]]\n", + "D1=numpy.linalg.det(d1)\n", + "d2=[[-f0,df_dy],[-g0,dg_dy]]\n", + "h=numpy.linalg.det(d2)/D1\n", + "d3=[[df_dx,-f0],[dg_dx,-g0]]\n", + "k=numpy.linalg.det(d3)/D1\n", + "x1=x0+h\n", + "y1=y0+k\n", + "f0=f(x1,y1)\n", + "g0=g(x1,y1)\n", + "df_dx=(f(x1+hh,y1)-f(x1,y1))/hh\n", + "df_dy=(f(x1,y1+hh)-f(x1,y1))/hh\n", + "dg_dx=(g(x1+hh,y1)-g(x1,y1))/hh\n", + "dg_dy=(g(x1,y1+hh)-g(x1,y1))/hh\n", + "d4=[[df_dx,df_dy],[dg_dx,dg_dy]]\n", + "D2=numpy.linalg.det(d4)\n", + "h=numpy.linalg.det([[-f0,df_dy],[-g0,dg_dy]])/D2\n", + "k=numpy.linalg.det([[df_dx,-f0],[dg_dx,-g0]])/D2\n", + "x2=x1+h\n", + "y2=y1+k\n", + "print \"the roots of the equation are x2=%0.4f and y2=%0.4f\" %(x2,y2)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the roots of the equation are x2=0.6537 and y2=1.5874\n" + ] + } + ], + "prompt_number": 8 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter3_8.ipynb b/Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter3_8.ipynb new file mode 100644 index 00000000..29d1e38c --- /dev/null +++ b/Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter3_8.ipynb @@ -0,0 +1,1134 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:b2cc76dcdb2b60a0e7b50604c3d4985ce7d522625248fb62dd0507e6e25fd7d6" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter03:Interpolation" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex3.4:pg-86" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 3.4\n", + "#interpolation\n", + "#page 86\n", + "import math\n", + "from __future__ import division\n", + "x=[1, 3, 5, 7]\n", + "y=[24, 120, 336, 720]\n", + "d1=[0,0,0]\n", + "d2=[0,0,0]\n", + "d3=[0,0,0]\n", + "h=2 #interval between values of x\n", + "c=0\n", + "for i in range(0,3):\n", + " d1[c]=y[i+1]-y[i]\n", + " c=c+1\n", + "c=0\n", + "for i in range(0,2):\n", + " d2[c]=d1[i+1]-d1[i]\n", + " c=c+1\n", + "c=0\n", + "for i in range(0,1):\n", + " d3[c]=d2[i+1]-d2[i]\n", + " c=c+1\n", + "d=[0,d1[0],d2[0],d3[0]]\n", + "x0=8 #value at 8\n", + "pp=1\n", + "y_x=y[0]\n", + "p=(x0-1)/2\n", + "for i in range(1,4):\n", + " pp=1\n", + " for j in range(0,i):\n", + " pp=pp*(p-(j)) \n", + " y_x=y_x+(pp*d[i])/math.factorial(i)\n", + "print \"value of function at %f is :%f\" %(x0,y_x)\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "value of function at 8.000000 is :990.000000\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex3.6:pg-87" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 3.6\n", + "#interpolation\n", + "#page 87\n", + "import math\n", + "from __future__ import division\n", + "x=[15, 20, 25, 30, 35, 40]\n", + "y=[0.2588190, 0.3420201, 0.4226183, 0.5, 0.5735764, 0.6427876]\n", + "d1=[0,0,0,0,0]\n", + "d2=[0,0,0,0]\n", + "d3=[0,0,0]\n", + "d4=[0,0]\n", + "d5=[0]\n", + "h=5 #interval between values of x\n", + "c=0\n", + "for i in range(0,5):\n", + " d1[c]=y[i+1]-y[i]\n", + " c=c+1\n", + "c=0\n", + "for i in range(0,4):\n", + " d2[c]=d1[i+1]-d1[i]\n", + " c=c+1\n", + "c=0\n", + "for i in range(0,3):\n", + " d3[c]=d2[i+1]-d2[i]\n", + " c=c+1\n", + "c=0\n", + "for i in range(0,2):\n", + " d4[c]=d3[i+1]-d3[i]\n", + " c=c+1\n", + "c=0\n", + "for i in range(0,1):\n", + " d5[c]=d4[i+1]-d4[i]\n", + " c=c+1\n", + "c=0\n", + "d=[0,d1[0], d2[0], d3[0], d4[0], d5[0]]\n", + "x0=38 #value at 38 degree\n", + "pp=1\n", + "y_x=y[0]\n", + "p=(x0-x[0])/h\n", + "for i in range(1,6):\n", + " pp=1\n", + " for j in range(0,i):\n", + " pp=pp*(p-(j)) \n", + " y_x=y_x+((pp*d[i])/math.factorial(i));\n", + "print \"value of function at %i is :%f\" %(x0,y_x)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "value of function at 38 is :0.615661\n" + ] + } + ], + "prompt_number": 26 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex3.7:pg-89" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 3.7\n", + "#interpolation\n", + "#page 89\n", + "x=[0, 1, 2, 4]\n", + "y=[1, 3, 9, 81]\n", + "#equation is y(5)-4*y(4)+6*y(2)-4*y(2)+y(1)\n", + "y3=(y[3]+6*y[2]-4*y[1]+y[0])/4\n", + "print \"the value of missing term of table is :%d\" %(y3)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the value of missing term of table is :31\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex3.8:pg-89" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 3.8\n", + "#interpolation\n", + "#page 89\n", + "import math\n", + "from __future__ import division\n", + "x=[0.10, 0.15, 0.20, 0.25, 0.30]\n", + "y=[0.1003, 0.1511, 0.2027, 0.2553, 0.3093]\n", + "d1=[0,0,0,0,0]\n", + "d2=[0,0,0,0,0]\n", + "d3=[0,0,0,0,0]\n", + "d4=[0,0,0,0,0]\n", + "h=0.05 #interval between values of x\n", + "c=0\n", + "for i in range(0,4):\n", + " d1[c]=y[i+1]-y[i]\n", + " c=c+1\n", + "c=0\n", + "for i in range(0,3):\n", + " d2[c]=d1[i+1]-d1[i]\n", + " c=c+1\n", + "c=0\n", + "for i in range(0,2):\n", + " d3[c]=d2[i+1]-d2[i]\n", + " c=c+1\n", + "c=0\n", + "for i in range(0,4):\n", + " d4[c]=d3[i+1]-d3[i]\n", + " c=c+1\n", + "d=[0,d1[0], d2[0], d3[0], d4[0]]\n", + "x0=0.12 #value at 0.12;\n", + "pp=1\n", + "y_x=y[0]\n", + "p=(x0-x[0])/h\n", + "for i in range(1,5):\n", + " pp=1;\n", + " for j in range(0,i):\n", + " pp=pp*(p-(j)) \n", + " y_x=y_x+(pp*d[i])/math.factorial(i)\n", + "print \"value of function at %f is :%0.4g\\n \\n\" %(x0,y_x)\n", + "x0=0.26 #value at 0.26;\n", + "pp=1\n", + "y_x=y[0]\n", + "p=(x0-x[0])/h\n", + "for i in range(1,5):\n", + " pp=1\n", + " for j in range(0,i):\n", + " pp=pp*(p-(j)) \n", + " y_x=y_x+(pp*d[i])/math.factorial(i);\n", + "print \"value of function at %f is :%0.4g\\n \\n\" %(x0,y_x)\n", + "x0=0.40 #value at 0.40;\n", + "pp=1\n", + "y_x=y[0]\n", + "p=(x0-x[0])/h\n", + "for i in range(1,5):\n", + " pp=1\n", + " for j in range(0,i):\n", + " pp=pp*(p-(j)) \n", + " y_x=y_x+(pp*d[i])/math.factorial(i)\n", + "print \"value of function at %f is :%0.4g\\n \\n\" %(x0,y_x)\n", + "x0=0.50 #value at 0.50;\n", + "pp=1\n", + "y_x=y[0]\n", + "p=(x0-x[0])/h\n", + "for i in range(1,5):\n", + " pp=1\n", + " for j in range(0,i):\n", + " pp=pp*(p-(j)) \n", + " y_x=y_x+(pp*d[i])/math.factorial(i)\n", + "print \"value of function at %f is :%0.5g\\n \\n\" %(x0,y_x)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "value of function at 0.120000 is :0.1205\n", + " \n", + "\n", + "value of function at 0.260000 is :0.266\n", + " \n", + "\n", + "value of function at 0.400000 is :0.4241\n", + " \n", + "\n", + "value of function at 0.500000 is :0.5543\n", + " \n", + "\n" + ] + } + ], + "prompt_number": 33 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex3.9:pg-93" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 3.9\n", + "#Gauss' forward formula\n", + "#page 93\n", + "import math\n", + "from __future__ import division\n", + "x=[1.0, 1.05, 1.10, 1.15, 1.20, 1.25, 1.30];\n", + "y=[2.7183, 2.8577, 3.0042, 3.1582, 3.3201, 3.4903, 3.66693]\n", + "d1=[0,0,0,0,0,0]\n", + "d2=[0,0,0,0,0]\n", + "d3=[0,0,0,0]\n", + "d4=[0,0,0]\n", + "d5=[0,0]\n", + "d6=[0]\n", + "h=0.05 #interval between values of x\n", + "c=0\n", + "for i in range(0,6):\n", + " d1[c]=y[i+1]-y[i]\n", + " c=c+1\n", + "c=0\n", + "for i in range(0,5):\n", + " d2[c]=d1[i+1]-d1[i]\n", + " c=c+1\n", + "c=0\n", + "for i in range(0,4):\n", + " d3[c]=d2[i+1]-d2[i]\n", + " c=c+1\n", + "c=0\n", + "for i in range(0,3):\n", + " d4[c]=d3[i+1]-d3[i]\n", + " c=c+1\n", + "c=0\n", + "for i in range(0,2):\n", + " d5[c]=d4[i+1]-d4[i]\n", + " c=c+1\n", + "c=0\n", + "for i in range(0,1):\n", + " d6[c]=d5[i+1]-d5[i]\n", + " c=c+1\n", + "d=[0,d1[3], d2[2], d3[2], d4[1], d5[0], d6[0]]\n", + "x0=1.17 #value at 1.17;\n", + "pp=1\n", + "y_x=y[3]\n", + "p=(x0-x[3])/h\n", + "for i in range(1,6):\n", + " pp=1;\n", + " for j in range(0,i):\n", + " pp=pp*(p-(j)) \n", + " y_x=y_x+(pp*d[i])/math.factorial(i)\n", + "print \"value of function at %f is :%0.4g\\n \\n\" %(x0,y_x)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "value of function at 1.170000 is :3.222\n", + " \n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex3.10:pg-97" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#practical interpolation\n", + "#example 3.10\n", + "#page 97\n", + "import math\n", + "x=[0.61, 0.62, 0.63, 0.64, 0.65, 0.66, 0.67]\n", + "y=[1.840431, 1.858928,1.877610, 1.896481, 1.915541, 1.934792, 1.954237]\n", + "d1=[0,0,0,0,0,0]\n", + "d2=[0,0,0,0,0]\n", + "d3=[0,0,0,0]\n", + "d4=[0,0,0]\n", + "h=0.01 #interval between values of x\n", + "c=0\n", + "for i in range(0,6):\n", + " d1[c]=y[i+1]-y[i]\n", + " c=c+1\n", + "c=0\n", + "for i in range(0,5):\n", + " d2[c]=d1[i+1]-d1[i]\n", + " c=c+1\n", + "c=0\n", + "for i in range(0,4):\n", + " d3[c]=d2[i+1]-d2[i];\n", + " c=c+1\n", + "c=0\n", + "for i in range(0,3):\n", + " d4[c]=d3[i+1]-d3[i];\n", + " c=c+1\n", + "d=[d1[0], d2[0], d3[0], d4[0]]\n", + "x0=0.644\n", + "p=(x0-x[3])/h;\n", + "y_x=y[3]\n", + "y_x=y_x+p*(d1[2]+d1[3])/2+p**2*(d2[1])/2 #stirling formula\n", + "print \"the value at %f by stirling formula is : %f\\n\\n\" %(x0,y_x)\n", + "y_x=y[3]\n", + "y_x=y_x+p*d1[3]+p*(p-1)*(d2[2]+d2[3])/2\n", + "print \" the value at %f by bessels formula is : %f\\n\\n\" %(x0,y_x)\n", + "y_x=y[3]\n", + "q=1-p\n", + "y_x=q*y[3]+q*(q**2-1)*d2[2]/2+p*y[4]+p*(q**2-1)*d2[4]/2\n", + "print \"the value at %f by everrets formula is : %f\\n\\n\" %(x0,y_x)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the value at 0.644000 by stirling formula is : 1.904082\n", + "\n", + "\n", + " the value at 0.644000 by bessels formula is : 1.904059\n", + "\n", + "\n", + "the value at 0.644000 by everrets formula is : 1.904044\n", + "\n", + "\n" + ] + } + ], + "prompt_number": 37 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex3.11:pg-99" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#practical interpolation\n", + "#example 3.11\n", + "#page 99\n", + "x=[0.61, 0.62, 0.63, 0.64, 0.65, 0.66, 0.67]\n", + "y=[1.840431, 1.858928, 1.877610, 1.896481, 1.915541, 1.934792, 1.954237]\n", + "d1=[0,0,0,0,0,0]\n", + "d2=[0,0,0,0,0]\n", + "d3=[0,0,0,0]\n", + "d4=[0,0,0]\n", + "h=0.01 #interval between values of x\n", + "c=0\n", + "for i in range(0,6):\n", + " d1[c]=y[i+1]-y[i]\n", + " c=c+1\n", + "c=0\n", + "for i in range(0,5):\n", + " d2[c]=d1[i+1]-d1[i]\n", + " c=c+1\n", + "c=0\n", + "for i in range(0,4):\n", + " d3[c]=d2[i+1]-d2[i]\n", + " c=c+1\n", + "c=0\n", + "for i in range(0,3):\n", + " d4[c]=d3[i+1]-d3[i]\n", + " c=c+1\n", + "d=[d1[0], d2[0], d3[0], d4[0]]\n", + "x0=0.638\n", + "p=(x0-x[3])/h\n", + "y_x=y[3]\n", + "y_x=y_x+p*(d1[2]+d1[3])/2+p**2*(d2[1])/2 #stirling formula\n", + "print \"value at %f by stirling formula is : %f\\n\\n\" %(x0,y_x)\n", + "y_x=y[2]\n", + "p=(x0-x[2])/h\n", + "y_x=y_x+p*d1[2]+p*(p-1)*(d2[1])/2\n", + "print \"the value at %f by bessels formula is : %f\\n\\n\" %(x0,y_x)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "value at 0.638000 by stirling formula is : 1.892692\n", + "\n", + "\n", + "the value at 0.638000 by bessels formula is : 1.892692\n", + "\n", + "\n" + ] + } + ], + "prompt_number": 39 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex3.12:pg-99" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#practical interpolation\n", + "#example 3.12\n", + "#page 99\n", + "x=[1.72, 1.73, 1.74, 1.75, 1.76, 1.77, 1.78]\n", + "y=[0.1790661479, 0.1772844100, 0.1755204006, 0.1737739435, 0.1720448638, 0.1703329888, 0.1686381473]\n", + "d1=[0,0,0,0,0,0]\n", + "d2=[0,0,0,0,0]\n", + "d3=[0,0,0,0]\n", + "d4=[0,0,0]\n", + "h=0.01 #interval between values of x\n", + "c=0\n", + "for i in range(0,6):\n", + " d1[c]=y[i+1]-y[i]\n", + " c=c+1\n", + "c=0\n", + "for i in range(0,5):\n", + " d2[c]=d1[i+1]-d1[i]\n", + " c=c+1\n", + "c=0\n", + "for i in range(0,4):\n", + " d3[c]=d2[i+1]-d2[i]\n", + " c=c+1\n", + "c=0\n", + "for i in range(0,3):\n", + " d4[c]=d3[i+1]-d3[i]\n", + " c=c+1\n", + "x0=1.7475\n", + "y_x=y[2]\n", + "p=(x0-x[2])/h\n", + "y_x=y_x+p*d1[2]+p*(p-1)*((d2[1]+d2[2])/2)/2\n", + "print \"the value at %f by bessels formula is : %0.10f\\n\\n\" %(x0,y_x)\n", + "y_x=y[3]\n", + "q=1-p\n", + "y_x=q*y[2]+q*(q**2-1)*d2[1]/6+p*y[3]+p*(p**2-1)*d2[1]/6\n", + "print \"the value at %f by everrets formula is : %0.10f\\n\\n\" %(x0,y_x)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the value at 1.747500 by bessels formula is : 0.1742089204\n", + "\n", + "\n", + "the value at 1.747500 by everrets formula is : 0.1742089122\n", + "\n", + "\n" + ] + } + ], + "prompt_number": 41 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex3.13:pg-104" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 3.13\n", + "#lagrange's interpolation formula\n", + "#page 104\n", + "x=[300, 304, 305, 307]\n", + "y=[2.4771, 2.4829, 2.4843, 2.4871]\n", + "x0=301\n", + "log_301=(-3*-4*-6*2.4771)/(-4*-5*-7)+(-4*-6*2.4829)/(4*-1*-3)+(-3*-6*2.4843)/(5*-2)+(-3*-4*2.4871)/(7*3*2)\n", + "print \"valie of log x at 301 is =%f\" %(log_301)\n", + "\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "valie of log x at 301 is =2.478597\n" + ] + } + ], + "prompt_number": 43 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex3.14:pg-105" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 3.14\n", + "#lagrange's interpolation formula\n", + "#page 105\n", + "y=[4, 12, 19]\n", + "x=[1, 3, 4];\n", + "y_x=7\n", + "Y_X=(-5*-12)/(-8*-15)+(3*3*-12)/(8*-7)+(3*-5*4)/(15*7)\n", + "print \"values is %f\" %(Y_X)\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "values is 1.857143\n" + ] + } + ], + "prompt_number": 44 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex3.15:pg-105" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 3.15\n", + "#lagrange's interpolation formula\n", + "#page 105\n", + "import math\n", + "x=[2, 2.5, 3.0]\n", + "y=[0.69315, 0.91629, 1.09861]\n", + "def l0(x):\n", + " return (x-2.5)*(x-3.0)/(-0.5)*(-1.0)\n", + "def l1(x):\n", + " return ((x-2.0)*(x-3.0))/((0.5)*(-0.5))\n", + "def l2(x):\n", + " return ((x-2.0)*(x-2.5))/((1.0)*(0.5))\n", + "f_x=l0(2.7)*y[0]+l1(2.7)*y[1]+l2(2.7)*y[2];\n", + "print \"the calculated value is %f:\" %(f_x)\n", + "print \"\\n\\n the error occured in the value is %0.9f\" %(abs(f_x-log(2.7)))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the calculated value is 0.994116:\n", + "\n", + "\n", + " the error occured in the value is 0.000864627\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex3.16:pg-106" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 3.16\n", + "#lagrange's interpolation formula\n", + "#page 106\n", + "from __future__ import division\n", + "import math\n", + "x=[0, math.pi/4,math.pi/2]\n", + "y=[0, 0.70711, 1.0];\n", + "x0=math.pi/6\n", + "sin_x0=0\n", + "for i in range(0,3):\n", + " p=y[i]\n", + " for j in range(0,3):\n", + " if j!=i:\n", + " p=p*((x0-x[j])/( x[i]-x[j]))\n", + " sin_x0=sin_x0+p\n", + "print \"sin_x0=%f\" %(sin_x0)\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "sin_x0=0.517431\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex3.18:pg-107" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#error in lagrange's interpolation formula\n", + "#example 3.18\n", + "#page 107\n", + "import math\n", + "x=[2, 2.5, 3.0]\n", + "y=[0.69315, 0.91629, 1.09861]\n", + "def l0(x):\n", + " return (x-2.5)*(x-3.0)/(-0.5)*(-1.0)\n", + "def l1(x):\n", + " return ((x-2.0)*(x-3.0))/((0.5)*(-0.5))\n", + "def l2(x):\n", + " return ((x-2.0)*(x-2.5))/((1.0)*(0.5))\n", + "f_x=l0(2.7)*y[0]+l1(2.7)*y[1]+l2(2.7)*y[2]\n", + "print \"the calculated value is %f:\" %(f_x)\n", + "err=math.fabs(f_x-math.log10(2.7))\n", + "def R_n(x):\n", + " return (((x-2)*(x-2.5)*(x-3))/6)\n", + "est_err=abs(R_n(2.7)*(2/8))\n", + "if est_errerr:\n", + " print \"\\n\\n the error agrees with the actual error\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "\n", + " the error agrees with the actual error\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex3.21:pg-110" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#hermite's interpolation formula\n", + "#exammple 3.21\n", + "#page 110\n", + "from __future__ import division\n", + "import math\n", + "x=[2.0, 2.5, 3.0]\n", + "y=[0.69315, 0.91629, 1.09861]\n", + "y1=[0,0,0]\n", + "def f(x):\n", + " return math.log(x)\n", + "h=0.0001\n", + "for i in range(0,3):\n", + " y1[i]=(f(x[i]+h)-f(x[i]))/h\n", + "def l0(x):\n", + " return (x-2.5)*(x-3.0)/(-0.5)*(-1.0)\n", + "def l1(x):\n", + " return ((x-2.0)*(x-3.0))/((0.5)*(-0.5))\n", + "def l2(x):\n", + " return ((x-2.0)*(x-2.5))/((1.0)*(0.5))\n", + "dl0=(l0(x[0]+h)-l0(x[0]))/h\n", + "dl1=(l1(x[1]+h)-l1(x[1]))/h\n", + "dl2=(l2(x[2]+h)-l2(x[2]))/h\n", + "x0=2.7\n", + "u0=(1-2*(x0-x[0])*dl0)*(l0(x0))**2\n", + "u1=(1-2*(x0-x[1])*dl1)*(l1(x0))**2\n", + "u2=(1-2*(x0-x[2])*dl2)*(l2(x0))**2\n", + "v0=(x0-x[0])*l0(x0)**2\n", + "v1=(x0-x[1])*l1(x0)**2\n", + "v2=(x0-x[2])*l2(x0)**2\n", + "H=u0*y[0]+u1*y[1]+u2*y[2]+v0*y1[0]+v1*y1[1]+v2*y1[2]\n", + "print \"the approximate value of ln(%0.2f) is %f:\" %(x0,H)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the approximate value of ln(2.70) is 0.993362:\n" + ] + } + ], + "prompt_number": 35 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex3.22:pg-114" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#newton's general interpolation formula\n", + "#example 3.22\n", + "#page 114\n", + "x=[300, 304, 305, 307]\n", + "y=[2.4771, 2.4829, 2.4843, 2.4871]\n", + "d1=[0,0,0]\n", + "d2=[0,0]\n", + "for i in range(0,3):\n", + " d1[i]=(y[i+1]-y[i])/(x[i+1]-x[i])\n", + "for i in range(0,2):\n", + " d2[i]=(d1[i+1]-d1[i])/(x[i+2]-x[i])\n", + "x0=301\n", + "log301=y[0]+(x0-x[0])*d1[0]+(x0-x[1])*d2[0]\n", + "print \"valure of log(%d) is :%0.4f\" %(x0,log301)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "valure of log(301) is :2.4786\n" + ] + } + ], + "prompt_number": 37 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex3.23:pg-114" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 3.23\n", + "#newton's divided formula\n", + "#page 114\n", + "x=[-1, 0, 3, 6, 7]\n", + "y=[3, -6, 39, 822, 1611]\n", + "d1=[0,0,0,0,0]\n", + "d2=[0,0,0,0,0]\n", + "d3=[0,0,0,0,0]\n", + "d4=[0,0,0,0,0]\n", + "X=0\n", + "for i in range(0,3):\n", + " d1[i]=(y[i+1]-y[i])/(x[i+1]-x[i])\n", + "for i in range(0,3):\n", + " d2[i]=(d1[i+1]-d1[i])/(x[i+2]-x[i])\n", + "for i in range(0,2):\n", + " d3[i]=(d2[i+1]-d2[i])/(x[i+3]-x[i])\n", + "for i in range(0,1):\n", + " d4[i]=(d3[i+1]-d3[i])/(x[i+4]-x[i])\n", + "f_x=y[0]+(X-x[0])*d1[0]+(X-x[1])*(X-x[0])*d2[0]+(X-x[0])*(X-x[1])*(X-x[2])*d3[0]+(X-x[0])*(X-x[1])*(X-x[2])*(X-x[3])*d4[0]\n", + "print \"the polynomial equation is = -6 + 5X^2 -3X^3 +X^4\" " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the polynomial equation is = -6 + 5X^2 -3X^3 +X^4\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex3.24:pg-116" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#interpolation by iteration\n", + "#example 3.24\n", + "#page 116\n", + "import numpy\n", + "x=[300, 304, 305, 307]\n", + "y=[2.4771, 2.4829, 2.4843, 2.4871]\n", + "x0=301\n", + "d1=[0,0,0]\n", + "d2=[0,0]\n", + "d3=[0]\n", + "for i in range(0,3):\n", + " a=y[i]\n", + " b=x[i]-x0\n", + " c=y[i+1]\n", + " e=x[i+1]-x0\n", + " d=matrix([[a,b],[c,e]])\n", + " d11=numpy.linalg.det(d)\n", + " d1[i]=d11/(x[i+1]-x[i])\n", + "for i in range(0,2):\n", + " a=d1[i]\n", + " b=x[i+1]-x0\n", + " c=d1[i+1]\n", + " e=x[i+2]-x0\n", + " d=matrix([[a,b],[c,e]])\n", + " d22=numpy.linalg.det(d)\n", + " f=(x[i+2]-x[i+1])\n", + " d2[i]=d22/f\n", + "for i in range(0,1):\n", + " a=d2[i]\n", + " b=x[i+2]-x0\n", + " c=d2[i+1]\n", + " e=x[i+3]-x0\n", + " d=matrix([[a,b],[c,e]])\n", + " d33=numpy.linalg.det(d)\n", + " d3[i]=d33/(x[i+3]-x[i+2])\n", + "print \"the value of log(%d) is : %f\" %(x0,d3[0])\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the value of log(301) is : 2.476900\n" + ] + } + ], + "prompt_number": 26 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex3.25:pg-118" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#inverse intrpolation\n", + "#example 3.25\n", + "#page 118\n", + "from __future__ import division\n", + "x=[2, 3, 4, 5]\n", + "y=[8, 27, 64, 125]\n", + "d1=[0,0,0]\n", + "d2=[0,0]\n", + "d3=[0]\n", + "for i in range(0,3):\n", + " d1[i]=y[i+1]-y[i]\n", + "for i in range(0,2):\n", + " d2[i]=d1[i+1]-d1[i]\n", + "for i in range(0,1):\n", + " d3[i]=d2[i+1]-d2[i]\n", + "yu=10 #square rooot of 10\n", + "y0=y[0]\n", + "d=[d1[0], d2[0] ,d3[0]]\n", + "u1=(yu-y0)/d1[0]\n", + "u2=((yu-y0-u1*(u1-1)*d2[0]/2)/d1[0])\n", + "u3=(yu-y0-u2*(u2-1)*d2[0]/2-u2*(u2-1)*(u2-2)*d3[0]/6)/d1[0]\n", + "u4=(yu-y0-u3*(u3-1)*d2[0]/2-u3*(u3-1)*(u3-2)*d3[0]/6)/d1[0]\n", + "u5=(yu-y0-u4*(u4-1)*d2[0]/2-u4*(u4-1)*(u4-2)*d3[0]/6)/d1[0]\n", + "print \"%f \\n %f \\n %f \\n %f \\n %f \\n \" %(u1,u2,u3,u4,u5)\n", + "print \"the approximate square root of %d is: %0.3f\" %(yu,x[0]+u5)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0.105263 \n", + " 0.149876 \n", + " 0.153210 \n", + " 0.154107 \n", + " 0.154347 \n", + " \n", + "the approximate square root of 10 is: 2.154\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex3.26:pg-119" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#double interpolation \n", + "#example 3.26\n", + "#page 119\n", + "y=[0, 1, 2, 3, 4]\n", + "z=[0,0,0,0,0]\n", + "x=[[0, 1, 4, 9, 16],[2, 3, 6, 11, 18],[6, 7, 10, 15, 22],[12, 13, 16, 21, 28],[18, 19, 22, 27, 34]]\n", + "print \"X=\"\n", + "print x\n", + "#for x=2.5\n", + "for i in range(0,5):\n", + " z[i]=(x[i][2]+x[i][3])/2\n", + "#y=1.5\n", + "Z=(z[1]+z[2])/2\n", + "print \"the interpolated value when x=2.5 and y=1.5 is : %f\" %(Z)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "X=\n", + "[[0, 1, 4, 9, 16], [2, 3, 6, 11, 18], [6, 7, 10, 15, 22], [12, 13, 16, 21, 28], [18, 19, 22, 27, 34]]\n", + "the interpolated value when x=2.5 and y=1.5 is : 10.500000\n" + ] + } + ], + "prompt_number": 14 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter4_8.ipynb b/Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter4_8.ipynb new file mode 100644 index 00000000..764b048a --- /dev/null +++ b/Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter4_8.ipynb @@ -0,0 +1,881 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:d50335da98243499f10813d11aa856c44156b53fb4777932e5b286f37a189ae4" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter04:Least Squares and Fourier Transforms" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex4.1:pg-128" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 4.1\n", + "#least square curve fitting procedure\n", + "#page 128\n", + "import math\n", + "from __future__ import division\n", + "x=[0,1, 2, 3, 4, 5]\n", + "x_2=[0,0,0,0,0,0]\n", + "x_y=[0,0,0,0,0,0]\n", + "y=[0,0.6, 2.4, 3.5, 4.8, 5.7]\n", + "for i in range(1,5):\n", + " x_2[i]=x[i]**2\n", + " x_y[i]=x[i]*y[i]\n", + "S_x=0\n", + "S_y=0\n", + "S_x2=0 \n", + "S_xy=0\n", + "S1=0\n", + "S2=0\n", + "for i in range(1,5):\n", + " S_x=S_x+x[i]\n", + " S_y=S_y+y[i]\n", + " S_x2=S_x2+x_2[i]\n", + " S_xy=S_xy+x_y[i]\n", + "a1=(5*S_xy-S_x*S_y)/(5*S_x2-S_x**2)\n", + "a0=S_y/5-a1*S_x/5\n", + "print \"x\\t y\\t x^2\\t x*y\\t (y-avg(S_y)) \\t (y-a0-a1x)^2\\n\\n\"\n", + "for i in range (1,6):\n", + " print \"%d\\t %0.2f\\t %d\\t %0.2f\\t %0.2f\\t %.4f\\t\\n\" %(x[i],y[i],x_2[i],x_y[i],(y[i]-S_y/5)**2,(y[i]-a0-a1*x[i])**2)\n", + " S1=S1+(y[i]-S_y/5)**2 \n", + " S2=S2+(y[i]-a0-a1*x[i])**2\n", + "print \"---------------------------------------------------------------------------------------------------------------------------------------------\\n\\n\"\n", + "print \"%d\\t %0.2f\\t %d\\t %0.2f\\t %0.2f\\t %0.4f\\t\\n\\n\" %(S_x,S_y,S_x2,S_xy,S1,S2)\n", + "cc=math.sqrt((S1-S2)/S1) #correlation coefficient\n", + "print \"the correlation coefficient is:%0.4f\" %(cc)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x\t y\t x^2\t x*y\t (y-avg(S_y)) \t (y-a0-a1x)^2\n", + "\n", + "\n", + "1\t 0.60\t 1\t 0.60\t 2.76\t 0.1681\t\n", + "\n", + "2\t 2.40\t 4\t 4.80\t 0.02\t 0.0196\t\n", + "\n", + "3\t 3.50\t 9\t 10.50\t 1.54\t 0.0001\t\n", + "\n", + "4\t 4.80\t 16\t 19.20\t 6.45\t 0.0016\t\n", + "\n", + "5\t 5.70\t 0\t 0.00\t 11.83\t 0.0961\t\n", + "\n", + "---------------------------------------------------------------------------------------------------------------------------------------------\n", + "\n", + "\n", + "10\t 11.30\t 30\t 35.10\t 22.60\t 0.2855\t\n", + "\n", + "\n", + "the correlation coefficient is:0.9937\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex4.2:pg-129" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 4.2\n", + "#least square curve fitting procedure\n", + "#page 129\n", + "from numpy import matrix\n", + "x=[0, 2, 5, 7]\n", + "y=[-1, 5, 12, 20]\n", + "x_2=[0,0,0,0]\n", + "xy=[0,0,0,0,]\n", + "for i in range (0,4):\n", + " x_2[i]=x[i]**2\n", + " xy[i]=x[i]*y[i]\n", + "print \"x\\t y\\t x^2\\t xy\\t \\n\\n\"\n", + "S_x=0 \n", + "S_y=0\n", + "S_x2=0\n", + "S_xy=0\n", + "for i in range(0,4):\n", + " print \"%d\\t %d\\t %d\\t %d\\t\\n\" %(x[i],y[i],x_2[i],xy[i])\n", + " S_x=S_x+x[i]\n", + " S_y=S_y+y[i]\n", + " S_x2=S_x2+x_2[i]\n", + " S_xy=S_xy+xy[i]\n", + "print \"%d\\t %d\\t %d\\t %d\\t\\n\" %(S_x,S_y,S_x2,S_xy)\n", + "A=matrix([[4,S_x],[S_x,S_x2]])\n", + "B=matrix([[S_y],[S_xy]])\n", + "C=A.I*B\n", + "print \"Best straight line fit Y=%.4f+x(%.4f)\" %(C[0][0],C[1][0])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x\t y\t x^2\t xy\t \n", + "\n", + "\n", + "0\t -1\t 0\t 0\t\n", + "\n", + "2\t 5\t 4\t 10\t\n", + "\n", + "5\t 12\t 25\t 60\t\n", + "\n", + "7\t 20\t 49\t 140\t\n", + "\n", + "14\t 36\t 78\t 210\t\n", + "\n", + "Best straight line fit Y=-1.1379+x(2.8966)\n" + ] + } + ], + "prompt_number": 26 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex4.3:pg-130" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 4.3\n", + "#least square curve fitting procedure\n", + "#page 130\n", + "from numpy import matrix\n", + "x=[0, 1, 2, 4, 6]\n", + "y=[0, 1, 3, 2, 8]\n", + "z=[2, 4, 3, 16, 8]\n", + "x2=[0,0,0,0,0]\n", + "y2=[0,0,0,0,0]\n", + "z2=[0,0,0,0,0]\n", + "xy=[0,0,0,0,0]\n", + "yz=[0,0,0,0,0]\n", + "zx=[0,0,0,0,0]\n", + "for i in range(0,5):\n", + " x2[i]=x[i]**2\n", + " y2[i]=y[i]**2\n", + " z2[i]=z[i]**2\n", + " xy[i]=x[i]*y[i]\n", + " zx[i]=z[i]*x[i]\n", + " yz[i]=y[i]*z[i]\n", + "S_x=0\n", + "S_y=0\n", + "S_z=0\n", + "S_x2=0\n", + "S_y2=0\n", + "S_z2=0\n", + "S_xy=0\n", + "S_zx=0\n", + "S_yz=0\n", + "for i in range(0,5):\n", + " S_x=S_x+x[i]\n", + " S_y=S_y+y[i]\n", + " S_z=S_z+z[i]\n", + " S_x2=S_x2+x2[i]\n", + " S_y2=S_y2+y2[i]\n", + " S_z2=S_z2+z2[i]\n", + " S_xy=S_xy+xy[i]\n", + " S_zx=S_zx+zx[i]\n", + " S_yz=S_yz+yz[i]\n", + "print \"x\\t y\\t z\\t x^2\\t xy\\t zx\\t y^2\\t yz\\n\\n\"\n", + "for i in range(0,5):\n", + " print \"%d\\t %d\\t %d\\t %d\\t %d\\t %d\\t %d\\t %d\\n\" %(x[i],y[i],z[i],x2[i],xy[i],zx[i],y2[i],yz[i])\n", + "print \"-------------------------------- --------------------------------------------------------------------------------------------------------------------------------------\\n\\n\"\n", + "print \"%d\\t %d\\t %d\\t %d\\t %d\\t %d\\t %d\\t %d\\n\\n\" %(S_x,S_y,S_z,S_x2,S_xy,S_zx,S_y2,S_yz)\n", + "A=matrix([[5,13,14],[13,57,63],[14,63,78]])\n", + "B=matrix([[33],[122],[109]])\n", + "C=A.I*B\n", + "print \"solution of above equation is:a=%d b=%d c=%d\" %(C[0][0],C[1][0],C[2][0])\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x\t y\t z\t x^2\t xy\t zx\t y^2\t yz\n", + "\n", + "\n", + "0\t 0\t 2\t 0\t 0\t 0\t 0\t 0\n", + "\n", + "1\t 1\t 4\t 1\t 1\t 4\t 1\t 4\n", + "\n", + "2\t 3\t 3\t 4\t 6\t 6\t 9\t 9\n", + "\n", + "4\t 2\t 16\t 16\t 8\t 64\t 4\t 32\n", + "\n", + "6\t 8\t 8\t 36\t 48\t 48\t 64\t 64\n", + "\n", + "-------------------------------- --------------------------------------------------------------------------------------------------------------------------------------\n", + "\n", + "\n", + "13\t 14\t 33\t 57\t 63\t 122\t 78\t 109\n", + "\n", + "\n", + "solution of above equation is:a=2 b=5 c=-3\n" + ] + } + ], + "prompt_number": 37 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex4.4:pg-131" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 4.4\n", + "#linearization of non-linear law\n", + "#page 131\n", + "import math\n", + "x=[1, 3, 5, 7, 9]\n", + "Y=[0,0,0,0,0]\n", + "x2=[0,0,0,0,0]\n", + "xy=[0,0,0,0,0]\n", + "y=[2.473, 6.722, 18.274, 49.673, 135.026]\n", + "for i in range(0,5):\n", + " Y[i]=math.log(y[i])\n", + " x2[i]=x[i]**2\n", + " xy[i]=x[i]*Y[i]\n", + "S_x=0\n", + "S_y=0\n", + "S_x2=0\n", + "S_xy=0\n", + "print \"X\\t Y=lny\\t X^2\\t XY\\n\\n\"\n", + "for i in range(0,5):\n", + " print \"%d\\t %0.3f\\t %d\\t %0.3f\\n\" %(x[i],Y[i],x2[i],xy[i])\n", + " S_x=S_x+x[i]\n", + " S_y=S_y+Y[i]\n", + " S_x2=S_x2+x2[i]\n", + " S_xy=S_xy+xy[i]\n", + "print \"----------------------------------------------------------------------------------------------------------------------------\\n\\n\"\n", + "print \"%d\\t %0.3f\\t %d\\t %0.3f\\t\\n\\n\" %(S_x,S_y,S_x2,S_xy)\n", + "A1=((S_x/5)*S_xy-S_x*S_y)/((S_x/5)*S_x2-S_x**2)\n", + "A0=(S_y/5)-A1*(S_x/5)\n", + "a=math.exp(A0)\n", + "print \"y=%0.3fexp(%0.2fx)\" %(a,A1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "X\t Y=lny\t X^2\t XY\n", + "\n", + "\n", + "1\t 0.905\t 1\t 0.905\n", + "\n", + "3\t 1.905\t 9\t 5.716\n", + "\n", + "5\t 2.905\t 25\t 14.527\n", + "\n", + "7\t 3.905\t 49\t 27.338\n", + "\n", + "9\t 4.905\t 81\t 44.149\n", + "\n", + "----------------------------------------------------------------------------------------------------------------------------\n", + "\n", + "\n", + "25\t 14.527\t 165\t 92.636\t\n", + "\n", + "\n", + "y=1.500exp(0.50x)\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex4.5:pg-131" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 4.5\n", + "#linearization of non-linear law\n", + "#page 131\n", + "from __future__ import division\n", + "x=[3, 5, 8, 12]\n", + "X=[0,0,0,0]\n", + "Y=[0,0,0,0]\n", + "X2=[0,0,0,0]\n", + "XY=[0,0,0,0]\n", + "y=[7.148, 10.231, 13.509, 16.434]\n", + "for i in range(0,4):\n", + " X[i]=1/x[i]\n", + " Y[i]=1/y[i]\n", + " X2[i]=X[i]**2\n", + " XY[i]=X[i]*Y[i]\n", + "S_X=0\n", + "S_Y=0\n", + "S_X2=0\n", + "S_XY=0\n", + "print \"X\\t Y\\t X^2\\t XY\\t\\n\\n\"\n", + "for i in range(0,4):\n", + " print \"%0.3f\\t %0.3f\\t %0.3f\\t %0.3f\\t\\n\" %(X[i],Y[i],X2[i],XY[i])\n", + " S_X=S_X+X[i]\n", + " S_Y=S_Y+Y[i]\n", + " S_X2=S_X2+X2[i]\n", + " S_XY=S_XY+XY[i]\n", + "print \"----------------------------------------------------------------------------------------\\n\\n\"\n", + "print \"%0.3f\\t %0.3f\\t %0.3f\\t %0.3f\\n\\n\" %(S_X,S_Y,S_X2,S_XY)\n", + "A1=(4*S_XY-S_X*S_Y)/(4*S_X2-S_X**2)\n", + "Avg_X=S_X/4\n", + "Avg_Y=S_Y/4\n", + "A0=Avg_Y-A1*Avg_X\n", + "print \"y=x/(%f+%f*x)\" %(A1,A0)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "X\t Y\t X^2\t XY\t\n", + "\n", + "\n", + "0.333\t 0.140\t 0.111\t 0.047\t\n", + "\n", + "0.200\t 0.098\t 0.040\t 0.020\t\n", + "\n", + "0.125\t 0.074\t 0.016\t 0.009\t\n", + "\n", + "0.083\t 0.061\t 0.007\t 0.005\t\n", + "\n", + "----------------------------------------------------------------------------------------\n", + "\n", + "\n", + "0.742\t 0.373\t 0.174\t 0.081\n", + "\n", + "\n", + "y=x/(0.316200+0.034500*x)\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex4.6:pg-134" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 4.6\n", + "#curve fitting by polynomial\n", + "#page 134\n", + "from numpy import matrix\n", + "x=[0, 1, 2]\n", + "y=[1, 6, 17]\n", + "x2=[0,0,0]\n", + "x3=[0,0,0]\n", + "x4=[0,0,0]\n", + "xy=[0,0,0]\n", + "x2y=[0,0,0]\n", + "for i in range(0,3):\n", + " x2[i]=x[i]**2\n", + " x3[i]=x[i]**3\n", + " x4[i]=x[i]**4\n", + " xy[i]=x[i]*y[i]\n", + " x2y[i]=x2[i]*y[i]\n", + "print \"x\\t y\\t x^2\\t x^3\\t x^4\\t x*y\\t x^2*y\\t\\n\\n\"\n", + "S_x=0\n", + "S_y=0\n", + "S_x2=0\n", + "S_x3=0\n", + "S_x4=0\n", + "S_xy=0\n", + "S_x2y=0\n", + "for i in range(0,3):\n", + " print \"%d\\t %d\\t %d\\t %d\\t %d\\t %d\\t %d\\n\" %(x[i],y[i],x2[i],x3[i],x4[i],xy[i],x2y[i])\n", + " S_x=S_x+x[i]\n", + " S_y=S_y+y[i]\n", + " S_x2=S_x2+x2[i]\n", + " S_x3=S_x3+x3[i]\n", + " S_x4=S_x4+x4[i]\n", + " S_xy=S_xy+xy[i]\n", + " S_x2y=S_x2y+x2y[i]\n", + "print \"--------------------------------------------------------------------------------------------------------------------------------\\n\\n\"\n", + "print \"%d\\t %d\\t %d\\t %d\\t %d\\t %d\\t %d\\n \" %(S_x,S_y,S_x2,S_x3,S_x4,S_xy,S_x2y)\n", + "A=matrix([[3,S_x,S_x2],[S_x,S_x2,S_x3],[S_x2,S_x3,S_x4]])\n", + "B=matrix([[S_y],[S_xy],[S_x2y]])\n", + "C=A.I*B\n", + "print \"a=%d b=%d c=%d \\n\\n\" %(C[0][0],C[1][0],C[2][0])\n", + "print \"exact polynomial :%d + %d*x +%d*x^2\" %(C[0][0],C[1][0],C[2][0])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x\t y\t x^2\t x^3\t x^4\t x*y\t x^2*y\t\n", + "\n", + "\n", + "0\t 1\t 0\t 0\t 0\t 0\t 0\n", + "\n", + "1\t 6\t 1\t 1\t 1\t 6\t 6\n", + "\n", + "2\t 17\t 4\t 8\t 16\t 34\t 68\n", + "\n", + "--------------------------------------------------------------------------------------------------------------------------------\n", + "\n", + "\n", + "3\t 24\t 5\t 9\t 17\t 40\t 74\n", + " \n", + "a=1 b=2 c=3 \n", + "\n", + "\n", + "exact polynomial :1 + 2*x +3*x^2\n" + ] + } + ], + "prompt_number": 30 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex4.7:pg-134" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 4.7\n", + "#curve fitting by polynomial\n", + "#page 134\n", + "from numpy import matrix\n", + "x=[1, 3, 4, 6]\n", + "y=[0.63, 2.05, 4.08, 10.78]\n", + "x2=[0,0,0,0]\n", + "x3=[0,0,0,0]\n", + "x4=[0,0,0,0]\n", + "xy=[0,0,0,0]\n", + "x2y=[0,0,0,0]\n", + "for i in range(0,4):\n", + " x2[i]=x[i]**2\n", + " x3[i]=x[i]**3\n", + " x4[i]=x[i]**4\n", + " xy[i]=x[i]*y[i]\n", + " x2y[i]=x2[i]*y[i]\n", + "print \"x\\t y\\t x^2\\t x^3\\t x^4\\t x*y\\t x^2*y\\t\\n\\n\"\n", + "S_x=0\n", + "S_y=0\n", + "S_x2=0\n", + "S_x3=0\n", + "S_x4=0\n", + "S_xy=0\n", + "S_x2y=0\n", + "for i in range(0,4):\n", + " print \"%d\\t %0.3f\\t %d\\t %d\\t %d\\t %0.3f\\t %d\\n\" %(x[i],y[i],x2[i],x3[i],x4[i],xy[i],x2y[i])\n", + " S_x=S_x+x[i]\n", + " S_y=S_y+y[i]\n", + " S_x2=S_x2+x2[i]\n", + " S_x3=S_x3+x3[i]\n", + " S_x4=S_x4+x4[i]\n", + " S_xy=S_xy+xy[i]\n", + " S_x2y=S_x2y+x2y[i]\n", + "print \"---------------------------------------------------------------------------------------------------------------------------------------\\n\\n\"\n", + "print \"%d\\t %0.3f\\t %d\\t %d\\t %d\\t %0.3f\\t %0.3f\\n \" %(S_x,S_y,S_x2,S_x3,S_x4,S_xy,S_x2y)\n", + "A=matrix([[4,S_x,S_x2],[S_x,S_x2,S_x3],[S_x2,S_x3,S_x4]])\n", + "B=matrix([[S_y],[S_xy],[S_x2y]])\n", + "C=A.I*B\n", + "print \"a=%0.2f b=%0.2f c=%0.2f \\n\\n\" %(C[0][0],C[1][0],C[2][0])\n", + "print \"exact polynomial :%0.2f + %0.2f*x +%0.2f*x^2\" %(C[0][0],C[1][0],C[2][0])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x\t y\t x^2\t x^3\t x^4\t x*y\t x^2*y\t\n", + "\n", + "\n", + "1\t 0.630\t 1\t 1\t 1\t 0.630\t 0\n", + "\n", + "3\t 2.050\t 9\t 27\t 81\t 6.150\t 18\n", + "\n", + "4\t 4.080\t 16\t 64\t 256\t 16.320\t 65\n", + "\n", + "6\t 10.780\t 36\t 216\t 1296\t 64.680\t 388\n", + "\n", + "---------------------------------------------------------------------------------------------------------------------------------------\n", + "\n", + "\n", + "14\t 17.540\t 62\t 308\t 1634\t 87.780\t 472.440\n", + " \n", + "a=1.24 b=-1.05 c=0.44 \n", + "\n", + "\n", + "exact polynomial :1.24 + -1.05*x +0.44*x^2\n" + ] + } + ], + "prompt_number": 42 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex4.8:pg-137" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#curve fitting by sum of exponentials\n", + "#example 4.8\n", + "#page 137\n", + "import math\n", + "from numpy import matrix\n", + "x=[1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8]\n", + "y=[1.54, 1.67, 1.81, 1.97, 2.15, 2.35, 2.58, 2.83, 3.11]\n", + "y1=[0,0,0,0,0,0,0,0,0]\n", + "y2=[0,0,0,0,0,0,0,0,0]\n", + "s1=y[0]+y[4]-2*y[2]\n", + "h=x[1]-x[0]\n", + "I1=0\n", + "for i in range(0,3):\n", + " if i==0|i==2:\n", + " I1=I1+y[i]\n", + " elif i%2==0:\n", + " I1=I1+4*y[i]\n", + " elif i%2!=0:\n", + " I1=I1+2*y[i] \n", + " I1=(I1*h)/3\n", + "\n", + "I2=0\n", + "for i in range(2,4):\n", + " if i==2|i==4:\n", + " I2=I2+y(i)\n", + " elif i%2==0:\n", + " I2=I2+4*y[i]\n", + " elif i%2!=0:\n", + " I2=I2+2*y[i] \n", + " \n", + " I2=(I2*h)/3\n", + " for i in range(0,4):\n", + " y1[i]=(1.0-x[i])*y[i]\n", + " for i in range(4,8):\n", + " y2[i]=(1.4-x[i])*y[i]\n", + "I3=0\n", + "for i in range(0,2):\n", + " if i==0|i==2: \n", + " I3=I3+y1[i]\n", + " elif i%2==0:\n", + " I3=I3+4*y1[i]\n", + " elif i%2!=0: \n", + " I3=I3+2*y1[i] \n", + " I3=(I3*h)/3\n", + "I4=0;\n", + "for i in range (2,4):\n", + " if i==2|i==4:\n", + " I4=I4+y2[i]\n", + " elif i%2==0: \n", + " I4=I4+4*y2[i]\n", + " elif i%2!=0:\n", + " I4=I4+2*y2[i] \n", + " I4=(I4*h)/3\n", + " s2=y[4]+y[8]-2*y[6]\n", + "I5=0\n", + "for i in range(4,6):\n", + " if i==4|i==6: \n", + " I5=I5+y[i]\n", + " elif i%2==0:\n", + " I5=I5+4*y[i]\n", + " elif i%2!=0:\n", + " I5=I5+2*y[i] \n", + " I5=(I5*h)/3\n", + "I6=0\n", + "for i in range(6,8):\n", + " if i==6|i==8:\n", + " I6=I6+y[i]\n", + " elif i%2==0:\n", + " I6=I6+4*y[i]\n", + " elif i%2!=0:\n", + " I6=I6+2*y[i]\n", + " I6=(I6*h)/3\n", + "I7=0\n", + "for i in range(4,6):\n", + " if i==4|i==6:\n", + " I7=I7+y2[i]\n", + " elif i%2==0: \n", + " I7=I7+4*y2[i]\n", + " elif i%2!=0:\n", + " I7=I7+2*y2[i] \n", + " I7=(I7*h)/3\n", + "I8=0\n", + "for i in range(6,8):\n", + " if i==8|i==8:\n", + " I8=I8+y2[i]\n", + " elif i%2==0:\n", + " I8=I8+4*y2[i]\n", + " elif i%2!=0:\n", + " I8=I8+2*y2[i]\n", + " I8=(I8*h)/3\n", + "A=matrix([[1.81, 2.180],[2.88, 3.104]])\n", + "C=matrix([[2.10],[3.00]])\n", + "Z=A.I*C\n", + "p = np.poly1d([1,Z[0][0],Z[1][0]])\n", + "print \"the unknown value of equation is 1 -1 \" \n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the unknown value of equation is 1 -1 \n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Es4.9:pg-139" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#linear weighted least approx\n", + "#example 4.9\n", + "#page 139\n", + "from numpy import matrix\n", + "x=[0, 2, 5, 7]\n", + "y=[-1, 5, 12, 20]\n", + "w=10 #given weight 10\n", + "W=[1, 1, 10, 1]\n", + "Wx=[0,0,0,0]\n", + "Wx2=[0,0,0,0]\n", + "Wx3=[0,0,0,0]\n", + "Wy=[0,0,0,0]\n", + "Wxy=[0,0,0,0]\n", + "for i in range(0,4):\n", + " Wx[i]=W[i]*x[i]\n", + " Wx2[i]=W[i]*x[i]**2\n", + " Wx3[i]=W[i]*x[i]**3\n", + " Wy[i]=W[i]*y[i]\n", + " Wxy[i]=W[i]*x[i]*y[i]\n", + "S_x=0\n", + "S_y=0\n", + "S_W=0\n", + "S_Wx=0\n", + "S_Wx2=0\n", + "S_Wy=0\n", + "S_Wxy=0\n", + "for i in range(0,4):\n", + " S_x=S_x+x[i]\n", + " S_y=S_y+y[i]\n", + " S_W=S_W+W[i]\n", + " S_Wx=S_Wx+Wx[i]\n", + " S_Wx2=S_Wx2+Wx2[i]\n", + " S_Wy=S_Wy+Wy[i]\n", + " S_Wxy=S_Wxy+Wxy[i]\n", + "A=matrix([[S_W,S_Wx],[S_Wx,S_Wx2]])\n", + "C=matrix([[S_Wy],[S_Wxy]])\n", + "print \"x\\t y\\t W\\t Wx\\t Wx^2\\t Wy\\t Wxy\\t\\n\\n\"\n", + "for i in range(0,4):\n", + " print \"%d\\t %d\\t %d\\t %d\\t %d\\t %d\\t %d\\t\\n\" %(x[i],y[i],W[i],Wx[i],Wx2[i],Wy[i],Wxy[i])\n", + "print \"-------------------------------------------------------------------------------------------------------------------------------------\\n\\n\"\n", + "print \"%d\\t %d\\t %d\\t %d\\t %d\\t %d\\t %d\\t\\n\" %(S_x,S_y,S_W,S_Wx,S_Wx2,S_Wy,S_Wxy)\n", + "X=A.I*C;\n", + "print \"\\n\\nthe equation is y=%f+%fx\" %(X[0][0],X[1][0])\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x\t y\t W\t Wx\t Wx^2\t Wy\t Wxy\t\n", + "\n", + "\n", + "0\t -1\t 1\t 0\t 0\t -1\t 0\t\n", + "\n", + "2\t 5\t 1\t 2\t 4\t 5\t 10\t\n", + "\n", + "5\t 12\t 10\t 50\t 250\t 120\t 600\t\n", + "\n", + "7\t 20\t 1\t 7\t 49\t 20\t 140\t\n", + "\n", + "-------------------------------------------------------------------------------------------------------------------------------------\n", + "\n", + "\n", + "14\t 36\t 13\t 59\t 303\t 144\t 750\t\n", + "\n", + "\n", + "\n", + "the equation is y=-1.349345+2.737991x\n" + ] + } + ], + "prompt_number": 77 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex4.10:pg-139" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#linear weighted least approx\n", + "#example 4.10\n", + "#page 139\n", + "from numpy import matrix\n", + "x=[0, 2, 5, 7]\n", + "y=[-1, 5, 12, 20]\n", + "w=100 #given weight 100\n", + "W=[1, 1, 100, 1]\n", + "Wx=[0,0,0,0]\n", + "Wx2=[0,0,0,0]\n", + "Wx3=[0,0,0,0]\n", + "Wy=[0,0,0,0]\n", + "Wxy=[0,0,0,0]\n", + "for i in range(0,4):\n", + " Wx[i]=W[i]*x[i]\n", + " Wx2[i]=W[i]*x[i]**2\n", + " Wx3[i]=W[i]*x[i]**3\n", + " Wy[i]=W[i]*y[i]\n", + " Wxy[i]=W[i]*x[i]*y[i]\n", + "S_x=0\n", + "S_y=0\n", + "S_W=0\n", + "S_Wx=0\n", + "S_Wx2=0\n", + "S_Wy=0\n", + "S_Wxy=0\n", + "for i in range(0,4):\n", + " S_x=S_x+x[i]\n", + " S_y=S_y+y[i]\n", + " S_W=S_W+W[i]\n", + " S_Wx=S_Wx+Wx[i]\n", + " S_Wx2=S_Wx2+Wx2[i]\n", + " S_Wy=S_Wy+Wy[i]\n", + " S_Wxy=S_Wxy+Wxy[i]\n", + "A=matrix([[S_W,S_Wx],[S_Wx,S_Wx2]])\n", + "C=matrix([[S_Wy],[S_Wxy]])\n", + "print \"x\\t y\\t W\\t Wx\\t Wx^2\\t Wy\\t Wxy\\t\\n\\n\"\n", + "for i in range(0,4):\n", + " print \"%d\\t %d\\t %d\\t %d\\t %d\\t %d\\t %d\\t\\n\" %(x[i],y[i],W[i],Wx[i],Wx2[i],Wy[i],Wxy[i])\n", + "print \"-------------------------------------------------------------------------------------------------------------------------------------\\n\\n\"\n", + "print \"%d\\t %d\\t %d\\t %d\\t %d\\t %d\\t %d\\t\\n\" %(S_x,S_y,S_W,S_Wx,S_Wx2,S_Wy,S_Wxy)\n", + "X=A.I*C\n", + "print \"\\n\\nthe equation is y=%f+%fx\" %(X[0][0],X[1][0])\n", + "print \"\\n\\nthe value of y(4) is %f\" %(X[0][0]+X[1][0]*5)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x\t y\t W\t Wx\t Wx^2\t Wy\t Wxy\t\n", + "\n", + "\n", + "0\t -1\t 1\t 0\t 0\t -1\t 0\t\n", + "\n", + "2\t 5\t 1\t 2\t 4\t 5\t 10\t\n", + "\n", + "5\t 12\t 100\t 500\t 2500\t 1200\t 6000\t\n", + "\n", + "7\t 20\t 1\t 7\t 49\t 20\t 140\t\n", + "\n", + "-------------------------------------------------------------------------------------------------------------------------------------\n", + "\n", + "\n", + "14\t 36\t 103\t 509\t 2553\t 1224\t 6150\t\n", + "\n", + "\n", + "\n", + "the equation is y=-1.412584+2.690562x\n", + "\n", + "\n", + "the value of y(4) is 12.040227\n" + ] + } + ], + "prompt_number": 82 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter6_8.ipynb b/Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter6_8.ipynb new file mode 100644 index 00000000..e6d1c784 --- /dev/null +++ b/Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter6_8.ipynb @@ -0,0 +1,1072 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:546eba3f0f90d73f5edc184c058b924f619f796c9b3186db8b7795ca0c31c66d" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter06:Numerical Differentiation and Integration" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex6.1:pg-201" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 6.1\n", + "#numerical diffrentiation by newton's difference formula \n", + "#page 210\n", + "x=[1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2]\n", + "y=[2.7183, 3.3201, 4.0552, 4.9530, 6.0496, 7.3891, 9.0250]\n", + "c=0\n", + "d1=[0,0,0,0,0,0]\n", + "d2=[0,0,0,0,0]\n", + "d3=[0,0,0,0]\n", + "d4=[0,0,0]\n", + "d5=[0,0]\n", + "d6=[0]\n", + "for i in range(0,6):\n", + " d1[i]=y[i+1]-y[i]\n", + " c=c+1;\n", + "c=0\n", + "for i in range(0,5):\n", + " d2[i]=d1[i+1]-d1[i]\n", + " c=c+1;\n", + "c=0\n", + "for i in range(0,4):\n", + " d3[i]=d2[i+1]-d2[i]\n", + " c=c+1;\n", + "c=0\n", + "for i in range(0,3):\n", + " d4[i]=d3[i+1]-d3[i]\n", + " c=c+1;\n", + "c=0\n", + "for i in range(0,2):\n", + " d5[c]=d4[i+1]-d4[i]\n", + " c=c+1\n", + "c=0\n", + "for i in range(0,1):\n", + " d6[i]=d5[i+1]-d5[i]\n", + " c=c+1\n", + "x0=1.2 #first and second derivative at 1.2\n", + "h=0.2\n", + "f1=((d1[1]-d2[1]/2+d3[1]/3-d4[1]/4+d5[1]/5)/h)\n", + "print \"the first derivative of fuction at 1.2 is:%f\\n\" %(f1)\n", + "f2=(d2[1]-d3[1]+(11*d4[1])/12-(5*d5[1])/6)/h**2\n", + "print \"the second derivative of fuction at 1.2 is:%f\\n\" %(f2)\n", + "\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the first derivative of fuction at 1.2 is:3.320317\n", + "\n", + "the second derivative of fuction at 1.2 is:3.319167\n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex6.2:pg-211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 6.2\n", + "#numerical diffrentiation by newton's difference formula \n", + "#page 211\n", + "x=[1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2]\n", + "y=[2.7183, 3.3201, 4.0552, 4.9530, 6.0496, 7.3891, 9.0250]\n", + "c=0\n", + "d1=[0,0,0,0,0,0]\n", + "d2=[0,0,0,0,0]\n", + "d3=[0,0,0,0]\n", + "d4=[0,0,0]\n", + "d5=[0,0]\n", + "d6=[0]\n", + "for i in range(0,6):\n", + " d1[c]=y[i+1]-y[i]\n", + " c=c+1;\n", + "c=0\n", + "for i in range(0,5):\n", + " d2[c]=d1[i+1]-d1[i]\n", + " c=c+1;\n", + "c=0\n", + "for i in range(0,4):\n", + " d3[c]=d2[i+1]-d2[i]\n", + " c=c+1;\n", + "c=0\n", + "for i in range(0,3):\n", + " d4[c]=d3[i+1]-d3[i]\n", + " c=c+1;\n", + "c=0\n", + "for i in range(0,2):\n", + " d5[c]=d4[i+1]-d4[i]\n", + " c=c+1;\n", + "c=0\n", + "for i in range(0,1):\n", + " d6[c]=d5[i+1]-d5[i]\n", + " c=c+1;\n", + "x0=2.2 #first and second derivative at 2.2\n", + "h=0.2\n", + "f1=((d1[5]+d2[4]/2+d3[3]/3+d4[2]/4+d5[1]/5)/h)\n", + "print \"the first derivative of fuction at 1.2 is:%f\\n\" %(f1)\n", + "f2=(d2[4]+d3[3]+(11*d4[2])/12+(5*d5[1])/6)/h**2\n", + "print \"the second derivative of fuction at 1.2 is:%f\\n\" %(f2)\n", + "x1=2.0 # first derivative also at 2.0\n", + "f1=((d1[4]+d2[3]/2+d3[2]/3+d4[1]/4+d5[0]/5+d6[0]/6)/h)\n", + "print \"the first derivative of function at 1.2 is:%f\\n\" %(f1)\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the first derivative of fuction at 1.2 is:9.022817\n", + "\n", + "the second derivative of fuction at 1.2 is:8.992083\n", + "\n", + "the first derivative of function at 1.2 is:7.389633\n", + "\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex6.3:pg-211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 6.3\n", + "#numerical diffrentiation by newton's difference formula \n", + "#page 211\n", + "x=[1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2]\n", + "y=[2.7183, 3.3201, 4.0552, 4.9530, 6.0496, 7.3891, 9.0250]\n", + "c=0\n", + "d1=[0,0,0,0,0,0]\n", + "d2=[0,0,0,0,0]\n", + "d3=[0,0,0,0]\n", + "d4=[0,0,0]\n", + "d5=[0,0]\n", + "d6=[0]\n", + "for i in range(0,6):\n", + " d1[c]=y[i+1]-y[i]\n", + " c=c+1;\n", + "c=0\n", + "for i in range(0,5):\n", + " d2[c]=d1[i+1]-d1[i]\n", + " c=c+1;\n", + "c=0\n", + "for i in range(0,4):\n", + " d3[c]=d2[i+1]-d2[i]\n", + " c=c+1;\n", + "c=0\n", + "for i in range(0,3):\n", + " d4[c]=d3[i+1]-d3[i]\n", + " c=c+1;\n", + "c=0\n", + "for i in range(0,2):\n", + " d5[c]=d4[i+1]-d4[i]\n", + " c=c+1;\n", + "c=0\n", + "for i in range(0,1):\n", + " d6[c]=d5[i+1]-d5[i]\n", + " c=c+1;\n", + "x0=1.6 #first and second derivative at 1.6\n", + "h=0.2\n", + "f1=(((d1[2]+d1[3])/2-(d3[1]+d3[2])/4+(d5[0]+d5[1])/60))/h\n", + "print \"the first derivative of function at 1.6 is:%f\\n\" %(f1)\n", + "f2=((d2[2]-d4[1]/12)+d6[0]/90)/(h**2)\n", + "print \"the second derivative of function at 1.6 is:%f\\n\" %(f2)\n", + "\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the first derivative of function at 1.6 is:4.885975\n", + "\n", + "the second derivative of function at 1.6 is:4.953361\n", + "\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex6.4:pg-213" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 6.4\n", + "#estimation of errors \n", + "#page 213\n", + "x=[1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2]\n", + "y=[2.7183, 3.3201, 4.0552, 4.9530, 6.0496, 7.3891, 9.0250]\n", + "c=0\n", + "d1=[0,0,0,0,0,0]\n", + "d2=[0,0,0,0,0]\n", + "d3=[0,0,0,0]\n", + "d4=[0,0,0]\n", + "d5=[0,0]\n", + "d6=[0]\n", + "for i in range(0,6):\n", + " d1[c]=y[i+1]-y[i]\n", + " c=c+1;\n", + "c=0\n", + "for i in range(0,5):\n", + " d2[c]=d1[i+1]-d1[i]\n", + " c=c+1;\n", + "c=0\n", + "for i in range(0,4):\n", + " d3[c]=d2[i+1]-d2[i]\n", + " c=c+1;\n", + "c=0\n", + "for i in range(0,3):\n", + " d4[c]=d3[i+1]-d3[i]\n", + " c=c+1;\n", + "c=0\n", + "for i in range(0,2):\n", + " d5[c]=d4[i+1]-d4[i]\n", + " c=c+1;\n", + "c=0\n", + "for i in range(0,1):\n", + " d6[c]=d5[i+1]-d5[i]\n", + " c=c+1\n", + "x0=1.6 #first and second derivative at 1.6\n", + "h=0.2\n", + "f1=((d1[1]-d2[1]/2+d3[1]/3-d4[1]/4+d5[1]/5)/h)\n", + "print \"the first derivative of fuction at 1.2 is:%f\\n\" %(f1)\n", + "f2=(d2[1]-d3[1]+(11*d4[1])/12-(5*d5[1])/6)/h**2\n", + "print \"the second derivative of fuction at 1.2 is:%f\\n\" %(f2)\n", + "T_error1=((d3[1]+d3[2])/2)/(6*h) #truncation error\n", + "e=0.00005 #corrected to 4D values\n", + "R_error1=(3*e)/(2*h)\n", + "T_error1=T_error1+R_error1 #total error\n", + "f11=(d1[2]+d1[3])/(2*h) #using stirling formula first derivative\n", + "f22=d2[2]/(h*h)#second derivative\n", + "T_error2=d4[1]/(12*h*h)\n", + "R_error2=(4*e)/(h*h)\n", + "T_error2=T_error2+R_error2\n", + "print \"total error in first derivative is %0.4g:\\n\" %(T_error1)\n", + "print \"total error in second derivative is %0.4g:\" %(T_error2)\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the first derivative of fuction at 1.2 is:3.320317\n", + "\n", + "the second derivative of fuction at 1.2 is:3.319167\n", + "\n", + "total error in first derivative is 0.03379:\n", + "\n", + "total error in second derivative is 0.02167:\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex6.5:pg-214" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#cubic spline method\n", + "#example 6.5\n", + "#page 214\n", + "import math\n", + "from __future__ import division\n", + "x=[0, math.pi/2, math.pi]\n", + "y=[0, 1, 0]\n", + "M0=0\n", + "M2=0\n", + "h=math.pi/2\n", + "M1=(6*(y[0]-2*y[1]+y[2])/(h**2)-M0-M2)/4\n", + "def s1(x):\n", + " return (2/math.pi)*(-2*3*x*x/(math.pi**2)+3/2)\n", + "S1=s1(math.pi/4)\n", + "print \"S1(pi/4)=%f\" %(S1)\n", + "def s2(x):\n", + " return (-24*x)/(math.pi**3)\n", + "S2=s2(math.pi/4)\n", + "print \"S2(pi/4)=%f\" %(S2)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "S1(pi/4)=0.716197\n", + "S2(pi/4)=-0.607927\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex6.6:pg-216" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#derivative by cubic spline method\n", + "#example 6.6\n", + "#page 216\n", + "x=[-2, -1, 2, 3]\n", + "y=[-12, -8, 3, 5] \n", + "def f(x):\n", + " return x**3/15-3*x**2/20+241*x/60-3.9\n", + "def s2(x):\n", + " return (((2-x)**3)/6*(14/55)+((x+1)**3)/6*(-74/55))/3+(-8-21/55)*(2-x)/3+(3-(9/6)*(-74/55))*(x+1)/3\n", + "h=0.0001\n", + "x0=1.0\n", + "y1=(s2(x0+h)-s2(x0))/h\n", + "print \"the value y1(%0.2f) is : %f\" %(x0,y1)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the value y1(1.00) is : 3.527232\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex6.7:pg-218" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#maximun and minimun of functions\n", + "#example 6.7\n", + "#page 218\n", + "x=[1.2, 1.3, 1.4, 1.5, 1.6]\n", + "y=[0.9320, 0.9636, 0.9855, 0.9975, 0.9996]\n", + "d1=[0,0,0,0]\n", + "d2=[0,0,0]\n", + "for i in range(0,4):\n", + " d1[i]=y[i+1]-y[i]\n", + "for i in range(0,3):\n", + " d2[i]=d1[i+1]-d1[i]\n", + "p=(-d1[0]*2/d2[0]+1)/2;\n", + "print \"p=%f\" %(p)\n", + "h=0.1\n", + "x0=1.2\n", + "X=x0+p*h\n", + "print \" the value of X correct to 2 decimal places is : %0.2f\" %(X)\n", + "Y=y[4]-0.2*d1[3]+(-0.2)*(-0.2+1)*d2[2]/2\n", + "print \"the value Y=%f\" %(Y)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "p=3.757732\n", + " the value of X correct to 2 decimal places is : 1.58\n", + "the value Y=0.999972\n" + ] + } + ], + "prompt_number": 26 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex6.8:pg-226" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 6.8\n", + "#trapezoidal method for integration\n", + "#page 226\n", + "from __future__ import division\n", + "x=[7.47, 7.48, 7.49, 7.0, 7.51, 7.52]\n", + "f_x=[1.93, 1.95, 1.98, 2.01, 2.03, 2.06]\n", + "h=x[1]-x[0]\n", + "l=6\n", + "area=0\n", + "for i in range(0,l):\n", + " if i==0:\n", + " area=area+f_x[i]\n", + " elif i==l-1:\n", + " area=area+f_x[i]\n", + " else:\n", + " area=area+2*f_x[i]\n", + "area=area*(h/2)\n", + "print \"area bounded by the curve is %f\" %(area)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "area bounded by the curve is 0.099650\n" + ] + } + ], + "prompt_number": 25 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex6.9:pg-226" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 6.9\n", + "#simpson 1/3rd method for integration\n", + "#page 226\n", + "from __future__ import division\n", + "import math\n", + "x=[0,0.00, 0.25, 0.50, 0.75, 1.00]\n", + "y=[0,1.000, 0.9896, 0.9589, 0.9089, 0.8415]\n", + "h=x[2]-x[1]\n", + "area=0\n", + "for i in range(0,6):\n", + " y[i]=y[i]**2\n", + "for i in range(1,6):\n", + " if i==1:\n", + " area=area+y[i]\n", + " elif i==5:\n", + " area=area+y[i]\n", + " elif i%2==0:\n", + " area=area+4*y[i]\n", + " elif i%2!=0: \n", + " area=area+2*y[i]\n", + "area=(area/3)*(h*math.pi)\n", + "print \"area bounded by the curve is %f\" %(area)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "area bounded by the curve is 2.819247\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex6.10:pg-228" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 6.10\n", + "#integration by trapezoidal and simpson's method\n", + "#page 228\n", + "from __future__ import division\n", + "def f(x):\n", + " return 1/(1+x)\n", + "h=0.5\n", + "x=[0,0.0,0.5,1.0]\n", + "y=[0,0,0,0]\n", + "l=4\n", + "for i in range(0,l):\n", + " y[i]=f(x[i])\n", + "area=0 #trapezoidal method\n", + "for i in range(1,l):\n", + " if i==1:\n", + " area=area+y[i]\n", + " elif i==l-1:\n", + " area=area+y[i]\n", + " else:\n", + " area=area+2*y[i]\n", + "area=area*(h/2)\n", + "print \"area bounded by the curve by trapezoidal method with h=%f is %f\\n \\n\" %(h,area)\n", + "area=0 #simpson 1/3rd rule\n", + "for i in range(1,l):\n", + " if i==1: \n", + " area=area+y[i]\n", + " elif i==l-1:\n", + " area=area+y[i]\n", + " elif i%2==0:\n", + " area=area+4*y[i]\n", + " elif i%2!=0:\n", + " area=area+2*y[i]\n", + "area=(area*h)/3\n", + "print \"area bounded by the curve by simpson 1/3rd method with h=%f is %f\\n \\n\" %(h,area)\n", + "h=0.25\n", + "x=[0,0.0,0.25,0.5,0.75,1.0]\n", + "y=[0,0,0,0,0,0]\n", + "l=6\n", + "for i in range(0,l):\n", + " y[i]=f(x[i])\n", + "area=0 #trapezoidal method\n", + "for i in range(1,l):\n", + " if i==1: \n", + " area=area+y[i]\n", + " elif i==l-1:\n", + " area=area+y[i]\n", + " else:\n", + " area=area+2*y[i]\n", + "area=area*(h/2)\n", + "print \"area bounded by the curve by trapezoidal method with h=%f is %f\\n \\n\" %(h,area)\n", + "area=0 #simpson 1/3rd rule\n", + "for i in range(1,l):\n", + " if i==1:\n", + " area=area+y[i]\n", + " elif i==l-1:\n", + " area=area+y[i]\n", + " elif i%2==0:\n", + " area=area+4*y[i]\n", + " elif i%2!=0:\n", + " area=area+2*y[i]\n", + "area=(area*h)/3\n", + "print \"area bounded by the curve by simpson 1/3rd method with h=%f is %f\\n \\n\" %(h,area)\n", + "h=0.125\n", + "x=[0,0.0,0.125,0.25,0.375,0.5,0.625,0.75,0.875,1.0]\n", + "y=[0,0,0,0,0,0,0,0,0,0]\n", + "l=10\n", + "for i in range(0,l):\n", + " y[i]=f(x[i])\n", + "area=0 #trapezoidal method\n", + "for i in range(1,l):\n", + " if i==1:\n", + " area=area+y[i]\n", + " elif i==l-1:\n", + " area=area+y[i]\n", + " elif i%2==0:\n", + " area=area+2*y[i]\n", + " elif i%2!=0:\n", + " area=area+2*y[i]\n", + "area=area*(h/2)\n", + "print \"area bounded by the curve by trapezoidal method with h=%f is %f\\n \\n\" %(h,area)\n", + "area=0 #simpson 1/3rd rule\n", + "for i in range(1,l):\n", + " if i==1:\n", + " area=area+y[i]\n", + " elif i==l-1:\n", + " area=area+y[i]\n", + " elif i%2==0:\n", + " area=area+4*y[i]\n", + " elif i%2!=0:\n", + " area=area+2*y[i]\n", + "area=(area*h)/3\n", + "print \"area bounded by the curve by simpson 1/3rd method with h=%f is %f\\n \\n\" %(h,area)\n", + "\n", + "\n", + "\n", + "\n", + "\n", + " \n", + "\n", + "\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "area bounded by the curve by trapezoidal method with h=0.500000 is 0.708333\n", + " \n", + "\n", + "area bounded by the curve by simpson 1/3rd method with h=0.500000 is 0.694444\n", + " \n", + "\n", + "area bounded by the curve by trapezoidal method with h=0.250000 is 0.697024\n", + " \n", + "\n", + "area bounded by the curve by simpson 1/3rd method with h=0.250000 is 0.693254\n", + " \n", + "\n", + "area bounded by the curve by trapezoidal method with h=0.125000 is 0.694122\n", + " \n", + "\n", + "area bounded by the curve by simpson 1/3rd method with h=0.125000 is 0.693155\n", + " \n", + "\n" + ] + } + ], + "prompt_number": 36 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex6.11:pg-229" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 6.11\n", + "#rommberg's method\n", + "#page 229\n", + "from __future__ import division\n", + "def f(x):\n", + " return 1/(1+x)\n", + "k=0\n", + "h=0.5\n", + "x=[0,0.0,0.5,1.0]\n", + "y=[0,0,0,0]\n", + "I=[0,0,0]\n", + "I1=[0,0]\n", + "T2=[0]\n", + "l=4\n", + "for i in range(0,l):\n", + " y[i]=f(x[i])\n", + "area=0 #trapezoidal method\n", + "for i in range(1,l):\n", + " if i==1:\n", + " area=area+y[i]\n", + " elif i==l-1:\n", + " area=area+y[i]\n", + " else:\n", + " area=area+2*y[i]\n", + "area=area*(h/2)\n", + "I[k]=area\n", + "k=k+1\n", + "h=0.25\n", + "x=[0,0.0,0.25,0.5,0.75,1.0]\n", + "y=[0,0,0,0,0,0]\n", + "l=6\n", + "for i in range(0,l):\n", + " y[i]=f(x[i])\n", + "area=0 #trapezoidal method\n", + "for i in range(1,l):\n", + " if i==1:\n", + " area=area+y[i]\n", + " elif i==l-1:\n", + " area=area+y[i]\n", + " else:\n", + " area=area+2*y[i]\n", + "area=area*(h/2)\n", + "I[k]=area\n", + "k=k+1\n", + "h=0.125\n", + "x=[0,0.0,0.125,0.25,0.375,0.5,0.625,0.75,0.875,1.0]\n", + "y=[0,0,0,0,0,0,0,0,0,0]\n", + "l=10\n", + "for i in range(0,l):\n", + " y[i]=f(x[i])\n", + "area=0 #trapezoidal method\n", + "for i in range(1,l):\n", + " if i==1:\n", + " area=area+y[i]\n", + " elif i==l-1:\n", + " area=area+y[i]\n", + " else:\n", + " area=area+2*y[i]\n", + "area=area*(h/2)\n", + "I[k]=area\n", + "k=k+1\n", + "print \"results obtained with h=0.5 0.25 0.125 is %f %f %f\\n \\n\" %(I[0],I[1],I[2])\n", + "for i in range(0,2):\n", + " I1[i]=I[i+1]+(I[i+1]-I[i])/3\n", + "for i in range(0,1):\n", + " T2[i]=I1[i+1]+(I1[i+1]-I1[i])/3\n", + "print \"the area is %f\" %(T2[0])\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "results obtained with h=0.5 0.25 0.125 is 0.708333 0.697024 0.694122\n", + " \n", + "\n", + "the area is 0.693121\n" + ] + } + ], + "prompt_number": 43 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex6.13:pg-230" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#area using cubic spline method\n", + "#example 6.13\n", + "#page 230\n", + "x=[0, 0.5, 1.0]\n", + "y=[0, 1.0, 0.0]\n", + "h=0.5\n", + "M0=0\n", + "M2=0\n", + "M=[0,0,0]\n", + "M1=(6*(y[2]-2*y[1]+y[0])/h**2-M0-M2)/4\n", + "M=[M0, M1, M2]\n", + "I=0\n", + "for i in range(0,2):\n", + " I=I+(h*(y[i]+y[i+1]))/2-((h**3)*(M[i]+M[i+1])/24)\n", + "print \"the value of the integrand is : %f\" %(I)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the value of the integrand is : 0.625000\n" + ] + } + ], + "prompt_number": 45 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex6.15:pg-233" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#euler's maclaurin formula\n", + "#example 6.15\n", + "#page 233\n", + "import math\n", + "y=[0, 1, 0]\n", + "h=math.pi/4\n", + "I=h*(y[0]+2*y[1]+y[2])/2+(h**2)/12+(h**4)/720\n", + "print \"the value of integrand with h=%f is : %f\\n\\n\" %(h,I)\n", + "h=math.pi/8\n", + "y=[0, math.sin(math.pi/8), math.sin(math.pi*2/8), math.sin(math.pi*3/8), math.sin(math.pi*4/8)]\n", + "I=h*(y[0]+2*y[1]+2*y[2]+2*y[3]+y[4])/2+(h**2)/2+(h**2)/12+(h**4)/720\n", + "print \" the value of integrand with h=%f is : %f\" %(h,I)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the value of integrand with h=0.785398 is : 0.837331\n", + "\n", + "\n", + " the value of integrand with h=0.392699 is : 1.077106\n" + ] + } + ], + "prompt_number": 47 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex6.17:pg-236" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# example 6.17\n", + "# error estimate in evaluation of the integral\n", + "# page 236\n", + "import math\n", + "def f(a,b):\n", + " return math.cos(a)+4*math.cos((a+b)/2)+math.cos(b)\n", + "a=0\n", + "b=math.pi/2\n", + "c=math.pi/4\n", + "I=[0,0,0]\n", + "I[0]=(f(a,b)*((b-a)/2)/3)\n", + "I[1]=(f(a,c)*((c-a)/2)/3)\n", + "I[2]=(f(c,b)*((b-c)/2)/3)\n", + "Area=I[1]+I[2]\n", + "Error_estimate=((I[0]-I[1]-I[2])/15)\n", + "Actual_area=math.sin(math.pi/2)-math.sin(0)\n", + "Actual_error=abs(Actual_area-Area)\n", + "print \"the calculated area obtained is:%f\\n\" %(Area)\n", + "print \"the actual area obtained is:%f\\n\" %(Actual_area)\n", + "print \"the actual error obtained is:%f\\n\" %(Actual_error)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the calculated area obtained is:1.000135\n", + "\n", + "the actual area obtained is:1.000000\n", + "\n", + "the actual error obtained is:0.000135\n", + "\n" + ] + } + ], + "prompt_number": 49 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex6.18:pg-237" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# example 6.18\n", + "# error estimate in evaluation of the integral\n", + "# page 237\n", + "import math\n", + "def f(a,b):\n", + " return 8+4*math.sin(a)+4*(8+4*math.sin((a+b)/2))+8+4*math.sin(b)\n", + "a=0\n", + "b=math.pi/2\n", + "c=math.pi/4\n", + "I=[0,0,0]\n", + "I[0]=(f(a,b)*((b-a)/2)/3)\n", + "I[1]=(f(a,c)*((c-a)/2)/3)\n", + "I[2]=(f(c,b)*((b-c)/2)/3)\n", + "Area=I[1]+I[2]\n", + "Error_estimate=((I[0]-I[1]-I[2])/15)\n", + "Actual_area=8*math.pi/2+4*math.sin(math.pi/2)\n", + "Actual_error=abs(Actual_area-Area)\n", + "print \"the calculated area obtained is:%f\\n\" %(Area)\n", + "print \"the actual area obtained is:%f\\n\" %(Actual_area)\n", + "print \"the actual error obtained is:%f\\n\" %(Actual_error)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the calculated area obtained is:16.566909\n", + "\n", + "the actual area obtained is:16.566371\n", + "\n", + "the actual error obtained is:0.000538\n", + "\n" + ] + } + ], + "prompt_number": 50 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex6.19:pg-242" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#gauss' formula\n", + "#example 6.19\n", + "#page 242\n", + "u=[-0.86113, -0.33998, 0.33998, 0.86113]\n", + "W=[0.34785, 0.65214, 0.65214, 0.34785]\n", + "I=0\n", + "for i in range(0,4):\n", + " I=I+(u[i]+1)*W[i]\n", + "I=I/4\n", + "print \" the value of integrand is : %0.5f\" %(I)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " the value of integrand is : 0.49999\n" + ] + } + ], + "prompt_number": 51 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex6.20:pg-247" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 6.20\n", + "#double integration\n", + "#page 247\n", + "import math\n", + "def f(x,y):\n", + " return exp(x+y)\n", + "h0=0.5\n", + "k0=0.5\n", + "x=[[0,0,0],[0,0,0],[0,0,0]]\n", + "h=[0, 0.5, 1]\n", + "k=[0, 0.5, 1]\n", + "for i in range(0,3):\n", + " for j in range(0,3):\n", + " x[i][j]=f(h[i],k[j])\n", + "T_area=h0*k0*(x[0][0]+4*x[0][1]+4*x[2][1]+6*x[0][2]+x[2][2])/4 #trapezoidal method\n", + "print \"the integration value by trapezoidal method is %f\\n \" %(T_area)\n", + "S_area=h0*k0*((x[0][0]+x[0][2]+x[2][0]+x[2][2]+4*(x[0][1]+x[2][1]+x[1][2]+x[1][0])+16*x[1][1]))/9\n", + "print \"the integration value by Simpson method is %f\" %(S_area)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the integration value by trapezoidal method is 3.076274\n", + " \n", + "the integration value by Simpson method is 2.954484\n" + ] + } + ], + "prompt_number": 55 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter7_8.ipynb b/Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter7_8.ipynb new file mode 100644 index 00000000..2d8ce1c4 --- /dev/null +++ b/Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter7_8.ipynb @@ -0,0 +1,773 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:a9212af2db5ffe946462a8eeaa56adb14070726a9a2a333afd16dc2f562a8951" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter07:Numerical Linear Algebra" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex7.1:pg-256" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 7.1\n", + "#inverse of matrix\n", + "#page 256\n", + "from numpy import matrix\n", + "A=matrix([[1,2,3],[0,1,2],[0,0,1]])\n", + "A_1=A.I #inverse of matrix\n", + "print A_1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[[ 1. -2. 1.]\n", + " [ 0. 1. -2.]\n", + " [ 0. 0. 1.]]\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex-7.2:pg-259" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 7.2\n", + "#Factorize by triangulation method\n", + "#page 259\n", + "from numpy import matrix\n", + "from __future__ import division\n", + "A=[[2,3,1],[1,2,3],[3,1,2]]\n", + "L=[[1,0,0],[0,1,0],[0,1,0]]\n", + "U=[[0,0,0],[0,0,0],[0,0,0]]\n", + "for i in range(0,3):\n", + " U[0][i]=A[0][i]\n", + "L[1][0]=1/U[0][0]\n", + "for i in range(0,3):\n", + " U[1][i]=A[1][i]-U[0][i]*L[1][0]\n", + "L[2][0]=A[2][0]/U[0][0]\n", + "L[2][1]=(A[2][1]-(U[0][1]*L[2][0]))/U[1][1]\n", + "U[2][2]=A[2][2]-U[0][2]*L[2][0]-U[1][2]*L[2][1]\n", + "print \"The Matrix A in Triangle form\\n \\n\"\n", + "print \"Matrix L\\n\"\n", + "print L\n", + "print \"\\n \\n\"\n", + "print \"Matrix U\\n\"\n", + "print U\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The Matrix A in Triangle form\n", + " \n", + "\n", + "Matrix L\n", + "\n", + "[[1, 0, 0], [0.5, 1, 0], [1.5, -7.0, 0]]\n", + "\n", + " \n", + "\n", + "Matrix U\n", + "\n", + "[[2, 3, 1], [0.0, 0.5, 2.5], [0, 0, 18.0]]\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex7.3:pg-262" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 7.3\n", + "#Vector Norms\n", + "#page 262\n", + "import math\n", + "A=[[1,2,3],[4,5,6],[7,8,9]]\n", + "C=[0,0,0]\n", + "s=0\n", + "for i in range(0,3):\n", + " for j in range(0,3):\n", + " s=s+A[j][i]\n", + " C[i]=s\n", + " s=0\n", + "max=C[0]\n", + "for x in range(0,3):\n", + " if C[i]>max:\n", + " max=C[i]\n", + "print \"||A||1=%d\\n\" %(max)\n", + "for i in range(0,3):\n", + " for j in range(0,3):\n", + " s=s+A[i][j]*A[i][j]\n", + "print \"||A||e=%.3f\\n\" %(math.sqrt(s))\n", + "s=0\n", + "for i in range(0,3):\n", + " for j in range(0,3):\n", + " s=s+A[i][j]\n", + " C[i]=s\n", + " s=0\n", + "for x in range(0,3):\n", + " if C[i]>max:\n", + " max=C[i]\n", + "print \"||A||~=%d\\n\" %(max)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "||A||1=18\n", + "\n", + "||A||e=16.882\n", + "\n", + "||A||~=24\n", + "\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex7.4:pg-266" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 7.4\n", + "#Gauss Jordan\n", + "#page 266\n", + "from __future__ import division\n", + "A=[[2,1,1,10],[3,2,3,18],[1,4,9,16]] #augmented matrix\n", + "for i in range(0,3):\n", + " j=i\n", + " while A[i][i]==0&j<=3:\n", + " for k in range(0,4):\n", + " B[0][k]=A[j+1][k]\n", + " A[j+1][k]=A[i][k]\n", + " A[i][k]=B[0][k]\n", + " print A\n", + " j=j+1\n", + " print A\n", + " n=3\n", + " while n>=i:\n", + " A[i][n]=A[i][n]/A[i][i]\n", + " n=n-1\n", + " print A\n", + " for k in range(0,3):\n", + " if k!=i:\n", + " l=A[k][i]/A[i][i]\n", + " for m in range(i,4):\n", + " A[k][m]=A[k][m]-l*A[i][m]\n", + " \n", + "print A\n", + "for i in range(0,3):\n", + " print \"\\nx(%i )=%g\\n\" %(i,A[i][3])\n", + "\n", + " \n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[[2, 1, 1, 10], [3, 2, 3, 18], [1, 4, 9, 16]]\n", + "[[1.0, 0.5, 0.5, 5.0], [3, 2, 3, 18], [1, 4, 9, 16]]\n", + "[[1.0, 0.5, 0.5, 5.0], [0.0, 0.5, 1.5, 3.0], [0.0, 3.5, 8.5, 11.0]]\n", + "[[1.0, 0.5, 0.5, 5.0], [0.0, 1.0, 3.0, 6.0], [0.0, 3.5, 8.5, 11.0]]\n", + "[[1.0, 0.0, -1.0, 2.0], [0.0, 1.0, 3.0, 6.0], [0.0, 0.0, -2.0, -10.0]]\n", + "[[1.0, 0.0, -1.0, 2.0], [0.0, 1.0, 3.0, 6.0], [0.0, 0.0, 1.0, 5.0]]\n", + "[[1.0, 0.0, 0.0, 7.0], [0.0, 1.0, 0.0, -9.0], [0.0, 0.0, 1.0, 5.0]]\n", + "\n", + "x(0 )=7\n", + "\n", + "\n", + "x(1 )=-9\n", + "\n", + "\n", + "x(2 )=5\n", + "\n" + ] + } + ], + "prompt_number": 28 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex7.8:pg-273" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#LU decomposition method\n", + "#example 7.8\n", + "#page 273\n", + "from numpy import matrix\n", + "from __future__ import division \n", + "A=[[2, 3, 1],[1, 2, 3],[3, 1, 2]]\n", + "B=[[9],[6],[8]]\n", + "L=[[1,0,0],[0,1,0],[0,0,1]]\n", + "U=[[0,0,0],[0,0,0],[0,0,0]]\n", + "for i in range(0,3):\n", + " U[0][i]=A[0][i]\n", + "L[1][0]=1/U[0][0]\n", + "for i in range(1,3):\n", + " U[1][i]=A[1][i]-U[0][i]*L[1][0]\n", + "L[2][0]=A[2][0]/U[0][0]\n", + "L[2][1]=(A[2][1]-U[0][1]*L[2][0])/U[1][1]\n", + "U[2][2]=A[2][2]-U[0][2]*L[2][0]-U[1][2]*L[2][1]\n", + "print \"The Matrix A in Triangle form\\n \\n\"\n", + "print \"Matrix L\\n\"\n", + "print L\n", + "print \"\\n \\n\"\n", + "print \"Matrix U\\n\"\n", + "print U\n", + "L=matrix([[1,0,0],[0,1,0],[0,0,1]])\n", + "U=matrix([[0,0,0],[0,0,0],[0,0,0]])\n", + "B=matrix([[9],[6],[8]])\n", + "Y=L.I*B\n", + "X=matrix([[1.944444],[1.611111],[0.277778]])\n", + "print \"the values of x=%f,y=%f,z=%f\" %(X[0][0],X[1][0],X[2][0])\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The Matrix A in Triangle form\n", + " \n", + "\n", + "Matrix L\n", + "\n", + "[[1, 0, 0], [0.5, 1, 0], [1.5, -7.0, 1]]\n", + "\n", + " \n", + "\n", + "Matrix U\n", + "\n", + "[[2, 3, 1], [0, 0.5, 2.5], [0, 0, 18.0]]\n", + "the values of x=1.944444,y=1.611111,z=0.277778\n" + ] + } + ], + "prompt_number": 41 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex7.9:pg-276" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#ill conditioned linear systems\n", + "#example 7.9\n", + "#page 276\n", + "from numpy import matrix\n", + "import math\n", + "A=matrix([[2, 1],[2,1.01]])\n", + "B=matrix([[2],[2.01]])\n", + "X=A.I*B\n", + "A_e=0\n", + "Ae=math.sqrt(Ae)\n", + "inv_A=A.I\n", + "invA_e=0\n", + "invA_e=math.sqrt(invA_e)\n", + "C=A_e*invA_e\n", + "k=2\n", + "if k<1:\n", + " print \"the fuction is ill conditioned\"" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex7.10:pg-277" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#ill condiioned linear systems\n", + "#example 7.10\n", + "#page 277\n", + "import numpy\n", + "from __future__ import division \n", + "A=[[1/2, 1/3, 1/4],[1/5, 1/6, 1/7],[1/8,1/9, 1/10]] #hilbert's matrix\n", + "de_A=numpy.linalg.det(A)\n", + "if de_A<1:\n", + " print \"A is ill-conditioned\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "A is ill-conditioned\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex7.11:pg-277" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#ill conditioned linear system\n", + "#example 7.11\n", + "#page 277\n", + "import numpy\n", + "import math\n", + "A=[[25, 24, 10],[66, 78, 37],[92, -73, -80]]\n", + "de_A=numpy.linalg.det(A)\n", + "for i in range(0,2):\n", + " s=0\n", + " for j in range(0,2):\n", + " s=s+A[i][j]**2\n", + " s=math.sqrt(s)\n", + " k=de_A/s\n", + "if k<1:\n", + " print\" the fuction is ill conditioned\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " the fuction is ill conditioned\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex7.12:pg-278" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#ill-conditioned system\n", + "#example 7.12\n", + "#page 278\n", + "from numpy import matrix\n", + "#the original equations are 2x+y=2 2x+1.01y=2.01\n", + "A1=matrix([[2, 1],[2, 1.01]])\n", + "C1=matrix([[2],[2.01]])\n", + "x1=1\n", + "y1=1 # approximate values\n", + "A2=matrix([[2, 1],[2, 1.01]])\n", + "C2=matrix([[3],[3.01]])\n", + "C=C1-C2\n", + "X=A1.I*C\n", + "x=X[0][0]+x1\n", + "y=X[1][0]+y1\n", + "print \"the exact solution is X=%f \\t Y=%f\" %(x,y)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the exact solution is X=0.500000 \t Y=1.000000\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex7.14:pg-282" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#solution of equations by iteration method\n", + "#example 7.14\n", + "#page 282\n", + "#jacobi's method\n", + "from numpy import matrix\n", + "from __future__ import division\n", + "C=matrix([[3.333],[1.5],[1.4]])\n", + "X=matrix([[3.333],[1.5],[1.4]])\n", + "B=matrix([[0, -0.1667, -0.1667],[-0.25, 0, 0.25],[-0.2, 0.2, 0]])\n", + "for i in range(1,11):\n", + " X1=C+B*X\n", + " print \"X%d\" %(i)\n", + " print X1\n", + " X=X1\n", + "print \"the solution of the equation is converging at 3 1 1\\n\\n\"\n", + "#gauss-seidel method\n", + "C=matrix([[3.333],[1.5],[1.4]])\n", + "X=matrix([[3.333],[1.5],[1.4]])\n", + "B=matrix([[0, -0.1667, -0.1667],[-0.25, 0, 0.25],[-0.2, 0.2, 0]])\n", + "X1=C+B*X\n", + "x=X1[0][0]\n", + "y=X1[1][0]\n", + "z=X1[2][0]\n", + "for i in range(0,5):\n", + " x=3.333-0.1667*y-0.1667*z\n", + " y=1.5-0.25*x+0.25*z\n", + " z=1.4-0.2*x+0.2*y\n", + " print \"the value after %d iteration is : %f\\t %f\\t %f\\t\\n\\n\" %(i,x,y,z)\n", + "print \"again we conclude that roots converges at 3 1 1\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "X1\n", + "[[ 2.84957]\n", + " [ 1.01675]\n", + " [ 1.0334 ]]\n", + "X2\n", + "[[ 2.99124 ]\n", + " [ 1.0459575]\n", + " [ 1.033436 ]]\n", + "X3\n", + "[[ 2.9863651]\n", + " [ 1.010549 ]\n", + " [ 1.0109435]]\n", + "X4\n", + "[[ 2.9960172 ]\n", + " [ 1.0061446 ]\n", + " [ 1.00483678]]\n", + "X5\n", + "[[ 2.9977694 ]\n", + " [ 1.00220489]\n", + " [ 1.00202548]]\n", + "X6\n", + "[[ 2.9988948 ]\n", + " [ 1.00106402]\n", + " [ 1.0008871 ]]\n", + "X7\n", + "[[ 2.99927475]\n", + " [ 1.00049808]\n", + " [ 1.00043384]]\n", + "X8\n", + "[[ 2.99944465]\n", + " [ 1.00028977]\n", + " [ 1.00024467]]\n", + "X9\n", + "[[ 2.99951091]\n", + " [ 1.0002 ]\n", + " [ 1.00016902]]\n", + "X10\n", + "[[ 2.99953848]\n", + " [ 1.00016453]\n", + " [ 1.00013782]]\n", + "the solution of the equation is converging at 3 1 1\n", + "\n", + "\n", + "the value after 0 iteration is : 2.991240\t 1.010540\t 1.003860\t\n", + "\n", + "\n", + "the value after 1 iteration is : 2.997200\t 1.001665\t 1.000893\t\n", + "\n", + "\n", + "the value after 2 iteration is : 2.999174\t 1.000430\t 1.000251\t\n", + "\n", + "\n", + "the value after 3 iteration is : 2.999486\t 1.000191\t 1.000141\t\n", + "\n", + "\n", + "the value after 4 iteration is : 2.999545\t 1.000149\t 1.000121\t\n", + "\n", + "\n", + "again we conclude that roots converges at 3 1 1\n" + ] + } + ], + "prompt_number": 25 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex7.15:pg-285" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#eigenvalues and eigenvectors\n", + "#example 7.15\n", + "#page 285\n", + "from numpy import matrix\n", + "A=matrix([[5, 0, 1],[0, -2, 0],[1, 0, 5]])\n", + "X=[6,4,-2]\n", + "print \"the eigen values are \\n\\n\"\n", + "print X\n", + "X1=matrix([[0],[1],[0]])\n", + "X2=matrix([[1/math.sqrt(2)],[0],[-1/math.sqrt(2)]])\n", + "X3=matrix([[1/math.sqrt(2)],[0],[1/math.sqrt(2)]])\n", + "#after computation the eigen vectors \n", + "print \"the eigen vectors for value %0.2g is=\" %(X[2])\n", + "print X1\n", + "print \"the eigen vectors for value %0.2g is= \" %(X[1])\n", + "print X2\n", + "print \"the eigen vectors for value %0.2g is=\" %(X[0])\n", + "print X3\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the eigen values are \n", + "\n", + "\n", + "[6, 4, -2]\n", + "the eigen vectors for value -2 is=\n", + "[[0]\n", + " [1]\n", + " [0]]\n", + "the eigen vectors for value 4 is= \n", + "[[ 0.70710678]\n", + " [ 0. ]\n", + " [-0.70710678]]\n", + "the eigen vectors for value 6 is=\n", + "[[ 0.70710678]\n", + " [ 0. ]\n", + " [ 0.70710678]]\n" + ] + } + ], + "prompt_number": 19 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex7.16:pg-286" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#largest eigenvalue and eigenvectors\n", + "#example 7.16\n", + "#page 286\n", + "from numpy import matrix\n", + "A=matrix([[1,6,1],[1,2,0],[0,0,3]])\n", + "I=matrix([[1],[0],[0]]) #initial eigen vector\n", + "X0=A*I\n", + "print \"X0=\"\n", + "print X0\n", + "X1=A*X0\n", + "print \"X1=\"\n", + "print X1\n", + "X2=A*X1\n", + "print \"X2=\"\n", + "print X2\n", + "X3=X2/3\n", + "print \"X3=\"\n", + "print X3\n", + "X4=A*X3\n", + "X5=X4/4\n", + "print \"X5=\"\n", + "print X5\n", + "X6=A*X5;\n", + "X7=X6/(4*4)\n", + "print \"X7=\"\n", + "print X7\n", + "print \"as it can be seen that highest eigen value is 4 \\n\\n the eigen vector is %d %d %d\" %(X7[0],X7[1],X7[2])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "X0=\n", + "[[1]\n", + " [1]\n", + " [0]]\n", + "X1=\n", + "[[7]\n", + " [3]\n", + " [0]]\n", + "X2=\n", + "[[25]\n", + " [13]\n", + " [ 0]]\n", + "X3=\n", + "[[8]\n", + " [4]\n", + " [0]]\n", + "X5=\n", + "[[8]\n", + " [4]\n", + " [0]]\n", + "X7=\n", + "[[2]\n", + " [1]\n", + " [0]]\n", + "as it can be seen that highest eigen value is 4 \n", + "\n", + " the eigen vector is 2 1 0\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex7.17:pg-290" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#housrholder's method\n", + "#example 7.17\n", + "#page 290\n", + "from numpy import matrix\n", + "from __future__ import division\n", + "import math\n", + "A=[[1, 3, 4],[3, 2, -1],[4, -1, 1]]\n", + "print A[1][1]\n", + "S=math.sqrt(A[0][1]**2+A[0][2]**2)\n", + "v2=math.sqrt((1+A[0][1]/S)/2)\n", + "v3=A[0][2]/(2*S)\n", + "v3=v3/v2\n", + "V=matrix([[0],[v2],[v3]])\n", + "P1=matrix([[1, 0, 0],[0, 1-2*v2**2, -2*v2*v3],[0, -2*v2*v3, 1-2*v3**2]])\n", + "A1=P1*A*P1\n", + "print \"the reduced matrix is \\n\\n\"\n", + "print A1\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n", + "the reduced matrix is \n", + "\n", + "\n", + "[[ 1.00000000e+00 -5.00000000e+00 -8.88178420e-16]\n", + " [ -5.00000000e+00 4.00000000e-01 2.00000000e-01]\n", + " [ -8.88178420e-16 2.00000000e-01 2.60000000e+00]]\n" + ] + } + ], + "prompt_number": 35 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter8_8.ipynb b/Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter8_8.ipynb new file mode 100644 index 00000000..574fc9f1 --- /dev/null +++ b/Introductory_Methods_Of_Numerical_Analysis__by_S._S._Sastry/chapter8_8.ipynb @@ -0,0 +1,1090 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:ee994ede45e4613be3b5f877164a49e19d7f959a299f5a0fc2395032c8a823aa" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter08:Numerical Solution of Ordinary Differential Equations" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex8.1:pg-304" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 8.1\n", + "#taylor's method\n", + "#page 304\n", + "import math\n", + "f=1 #value of function at 0\n", + "def f1(x):\n", + " return x-f**2\n", + "def f2(x):\n", + " return 1-2*f*f1(x)\n", + "def f3(x):\n", + " return -2*f*f2(x)-2*f2(x)**2\n", + "def f4(x):\n", + " return -2*f*f3(x)-6*f1(x)*f2(x)\n", + "def f5(x):\n", + " return -2*f*f4(x)-8*f1(x)*f3(x)-6*f2(x)**2\n", + "h=0.1 #value at 0.1\n", + "k=f \n", + "for j in range(1,5):\n", + " if j==1:\n", + " k=k+h*f1(0);\n", + " elif j==2:\n", + " k=k+(h**j)*f2(0)/math.factorial(j)\n", + " elif j ==3:\n", + " k=k+(h**j)*f3(0)/math.factorial(j)\n", + " elif j ==4:\n", + " k=k+(h**j)*f4(0)/math.factorial(j)\n", + " elif j==5:\n", + " k=k+(h**j)*f5(0)/math.factorial(j)\n", + "print \"the value of the function at %.2f is :%0.4f\" %(h,k)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the value of the function at 0.10 is :0.9113\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex8.2:pg-304" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#taylor's method\n", + "#example 8.2\n", + "#page 304\n", + "import math\n", + "f=1 #value of function at 0\n", + "f1=0 #value of first derivatie at 0\n", + "def f2(x):\n", + " return x*f1+f\n", + "def f3(x):\n", + " return x*f2(x)+2*f1\n", + "def f4(x):\n", + " return x*f3(x)+3*f2(x)\n", + "def f5(x):\n", + " return x*f4(x)+4*f3(x)\n", + "def f6(x):\n", + " return x*f5(x)+5*f4(x)\n", + "h=0.1 #value at 0.1\n", + "k=f\n", + "for j in range(1,6):\n", + " if j==1:\n", + " k=k+h*f1\n", + " elif j==2:\n", + " k=k+(h**j)*f2(0)/math.factorial(j)\n", + " elif j ==3:\n", + " k=k+(h**j)*f3(0)/math.factorial(j)\n", + " elif j ==4:\n", + " k=k+(h**j)*f4(0)/math.factorial(j)\n", + " elif j==5:\n", + " k=k+(h**j)*f5(0)/math.factorial(j)\n", + " else:\n", + " k=k+(h**j)*f6(0)/math.factorial (j)\n", + "print \"the value of the function at %.2f is :%0.7f\" %(h,k)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the value of the function at 0.10 is :1.0050125\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex8.3:pg-306" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 8.3\n", + "#picard's method\n", + "#page 306\n", + "from scipy import integrate\n", + "from __future__ import division\n", + "def f(x,y):\n", + " return x+y**2\n", + "y=[0,0,0,0]\n", + "y[1]=1\n", + "for i in range(1,3):\n", + " a=integrate.quad(lambda x:x+y[i]**2,0,i/10)\n", + " y[i+1]=a[0]+y[1]\n", + " print \"\\n y (%g) = %g\\n\" %(i/10,y[i+1])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + " y (0.1) = 1.105\n", + "\n", + "\n", + " y (0.2) = 1.26421\n", + "\n" + ] + } + ], + "prompt_number": 22 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex8.4:pg-306" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 8.4\n", + "#picard's method\n", + "#page 306\n", + "from scipy import integrate\n", + "y=[0,0,0,0] #value at 0\n", + "c=0.25\n", + "for i in range(0,3):\n", + " a=integrate.quad(lambda x:(x**2/(y[i]**2+1)),0,c)\n", + " y[i+1]=y[0]+a[0]\n", + " print \"\\n y(%0.2f) = %g\\n\" %(c,y[i+1])\n", + " c=c*2" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + " y(0.25) = 0.00520833\n", + "\n", + "\n", + " y(0.50) = 0.0416655\n", + "\n", + "\n", + " y(1.00) = 0.332756\n", + "\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex8.5:pg-308" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 8.5\n", + "#euler's method\n", + "#page 308\n", + "def f(y):\n", + " return -1*y\n", + "y=[0,0,0,0,0]\n", + "y[0]=1 #value at 0\n", + "h=0.01\n", + "c=0.01\n", + "for i in range(0,4):\n", + " y[i+1]=y[i]+h*f(y[i])\n", + " print \"\\ny(%g)=%g\\n\" %(c,y[i+1])\n", + " c=c+0.01\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "y(0.01)=0.99\n", + "\n", + "\n", + "y(0.02)=0.9801\n", + "\n", + "\n", + "y(0.03)=0.970299\n", + "\n", + "\n", + "y(0.04)=0.960596\n", + "\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Ex8.6:pg-308" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#example 8.6\n", + "#error estimates in euler's \n", + "#page 308\n", + "from __future__ import division\n", + "def f(y):\n", + " return -1*y\n", + "y=[0,0,0,0,0]\n", + "L=[0,0,0,0,0]\n", + "e=[0,0,0,0,0]\n", + "y[0]=1 #value at 0\n", + "h=0.01\n", + "c=0.01;\n", + "for i in range(0,4):\n", + " y[i+1]=y[i]+h*f(y[i])\n", + " print \"\\ny(%g)=%g\\n\" %(c,y[i+1])\n", + " c=c+0.01\n", + "for i in range(0,4):\n", + " L[i]=abs(-(1/2)*(h**2)*y[i+1])\n", + " print \"L(%d) =%f\\n\\n\" %(i,L[i])\n", + "e[0]=0\n", + "for i in range(0,4):\n", + " e[i+1]=abs(y[1]*e[i]+L[0])\n", + " print \"e(%d)=%f\\n\\n\" %(i,e[i])\n", + "Actual_value=math.exp(-0.04)\n", + "Estimated_value=y[4]\n", + "err=abs(Actual_value-Estimated_value)\n", + "if err