diff options
author | Trupti Kini | 2017-01-12 23:33:05 +0600 |
---|---|---|
committer | Trupti Kini | 2017-01-12 23:33:05 +0600 |
commit | ade107a07680916a90da3e540d184972006d755a (patch) | |
tree | 874c6258684f87ae5ce306e7850256a9599810ac /Numerical_Methods_by_E._Balaguruswamy/chapter6.ipynb | |
parent | b85db0ac762e815fb10f6100f28021c1475f2235 (diff) | |
download | Python-Textbook-Companions-ade107a07680916a90da3e540d184972006d755a.tar.gz Python-Textbook-Companions-ade107a07680916a90da3e540d184972006d755a.tar.bz2 Python-Textbook-Companions-ade107a07680916a90da3e540d184972006d755a.zip |
Added(A)/Deleted(D) following books
A Basic_mechanical_engineering_by_Basant_Agrawal_,_C.M_Agrawal/README.txt
A Numerical_Methods_by_E._Balaguruswamy/chapter10.ipynb
A Numerical_Methods_by_E._Balaguruswamy/chapter11.ipynb
A Numerical_Methods_by_E._Balaguruswamy/chapter12.ipynb
A Numerical_Methods_by_E._Balaguruswamy/chapter13.ipynb
A Numerical_Methods_by_E._Balaguruswamy/chapter14.ipynb
A Numerical_Methods_by_E._Balaguruswamy/chapter15.ipynb
A Numerical_Methods_by_E._Balaguruswamy/chapter3.ipynb
A Numerical_Methods_by_E._Balaguruswamy/chapter4.ipynb
A Numerical_Methods_by_E._Balaguruswamy/chapter6.ipynb
A Numerical_Methods_by_E._Balaguruswamy/chapter7.ipynb
A Numerical_Methods_by_E._Balaguruswamy/chapter8.ipynb
A Numerical_Methods_by_E._Balaguruswamy/chapter9.ipynb
A Numerical_Methods_by_E._Balaguruswamy/screenshots/greatest-precision-4.png
A Numerical_Methods_by_E._Balaguruswamy/screenshots/rounding-off-4.png
A Numerical_Methods_by_E._Balaguruswamy/screenshots/truncation-error-4.png
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_4JhcI7F.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_5CfOfQx.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_B30VPml.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_BE8QjoS.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_Gb015bo.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_KQ8ycMr.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_LIZWeY4.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_Opo6g3L.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_Pq40WOu.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_QLXi0uM.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_TdOIeIQ.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_UiM06tF.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_VQabJzR.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_XKPPUxj.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_dfFlLnm.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_fegIkl6.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_g1GxlUN.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_gGvuusH.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_gZeNd4b.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_khAt4Y6.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_oJQM5Mb.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_p22tFeA.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_qNHcrb8.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_qeUxd0G.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_r0nfWNs.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_uWQUwaW.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_vkMAnbH.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_wP2wGMS.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_x09tDSO.ipynb
A Physics_BSc(Paper_4)_by_Sanjeeva_Rao,_Bhikshmaiah,_Ramakrishna_Reddy,_Ananta_Ramaiah/C_yCW2orc.ipynb
Diffstat (limited to 'Numerical_Methods_by_E._Balaguruswamy/chapter6.ipynb')
-rw-r--r-- | Numerical_Methods_by_E._Balaguruswamy/chapter6.ipynb | 1039 |
1 files changed, 1039 insertions, 0 deletions
diff --git a/Numerical_Methods_by_E._Balaguruswamy/chapter6.ipynb b/Numerical_Methods_by_E._Balaguruswamy/chapter6.ipynb new file mode 100644 index 00000000..76664ab7 --- /dev/null +++ b/Numerical_Methods_by_E._Balaguruswamy/chapter6.ipynb @@ -0,0 +1,1039 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 6 - Roots of non linear equations" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 6_01 Pg No. 126" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The largest possible root is x1 = [[4]]\n", + "No root can be larger than the value = [[4]]\n", + "\n", + "All real roots lie in the interval (-3.741657,3.741657)\n", + "\n", + "We can use these two points as initial guesses for the bracketing methods and one of them for open end methods\n" + ] + } + ], + "source": [ + "from numpy import mat,shape\n", + "from math import sqrt\n", + "\n", + "#Possible Initial guess values for roots\n", + "\n", + "A = mat([[ 2],[-8] ,[2],[12]]) # Coefficients of x terms in the decreasing order of power\n", + "n = shape(A)#\n", + "x1 = -A[1]/A[0]#\n", + "print 'The largest possible root is x1 =',x1\n", + "print 'No root can be larger than the value =',x1\n", + "\n", + "x = sqrt((A[1]/A[0])**2 - 2*(A[2]/A[0])**2)\n", + "\n", + "print '\\nAll real roots lie in the interval (-%f,%f)\\n'%(x,x)\n", + "print 'We can use these two points as initial guesses for the bracketing methods and one of them for open end methods'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 6_03 Pg No. " + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "p(4) = 1\n", + "\n", + " p(3)= -2\n", + "\n", + "\n", + " p(2)= 1\n", + "\n", + "\n", + " f(2) = p(1) = 0\n" + ] + } + ], + "source": [ + "from numpy import mat,shape\n", + "from math import sqrt\n", + "#Evaluating Polynomial using Horner's rule\n", + "\n", + "#Coefficients of x terms in the increasing order of power\n", + "A = mat([[6],[1],[-4],[1]])\n", + "x = 2\n", + "N=shape(A)\n", + "n,c = N[0],N[1]\n", + "p=[0,0,0]\n", + "p.append(A[n-1])\n", + "print 'p(4) =',p[n-1][0,0]\n", + "for i in range(1,n-1):\n", + " p[n-i]= p[n-i+1-1]*x + A[n-i-1]\n", + " print '\\n p(%d)= %d\\n'%(n-i,p[n-i])\n", + "\n", + "print '\\n f(%d) = p(1) = %d'%(x,p[0])\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 6_04 Pg No. 132" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "First finding the interval that contains a root,this can be done by using Eq 6.10\n", + "\n", + " Both the roots lie in the interval (-6,6) \n", + "\n", + "\n", + " The root lies in the interval (5,5)\n", + "\n" + ] + } + ], + "source": [ + "from numpy import poly1d,polyval, sqrt\n", + "\n", + "#Root of a Equation Using Bisection Method\n", + "\n", + "#Coefficients in increasing order of power of x starting from 0\n", + "A = [-10 ,-4, 1]#\n", + "print 'First finding the interval that contains a root,this can be done by using Eq 6.10'\n", + "xmax = sqrt((A[1]/A[2])**2 - 2*(A[0]/A[2]))\n", + "print '\\n Both the roots lie in the interval (-%d,%d) \\n'%(xmax,xmax)\n", + "x = range(-6,7)\n", + "p= poly1d(A)# p = poly(A,'x'%('c'\n", + "\n", + "fx = [p(xx) for xx in x]#\n", + "for i in range(0,12):\n", + " if fx[i]*fx[i] < 0:\n", + " break \n", + " \n", + "\n", + "print '\\n The root lies in the interval (%d,%d)\\n'%(x[i],x[i])\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 6_05 Pg No. 139" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Iteration No. 1 \n", + "\n", + "x0 = 1.000000 \n", + "\n", + "Iteration No. 2 \n", + "\n", + "x0 = 1.000000 \n", + "\n", + "Iteration No. 3 \n", + "\n", + "x0 = 1.000000 \n", + "\n", + "Iteration No. 4 \n", + "\n", + "x0 = 1.000000 \n", + "\n", + "Iteration No. 5 \n", + "\n", + "x0 = 1.000000 \n", + "\n", + "Iteration No. 6 \n", + "\n", + "x0 = 1.000000 \n", + "\n", + "Iteration No. 7 \n", + "\n", + "x0 = 1.000000 \n", + "\n", + "Iteration No. 8 \n", + "\n", + "x0 = 1.000000 \n", + "\n", + "Iteration No. 9 \n", + "\n", + "x0 = 1.000000 \n", + "\n", + "Iteration No. 10 \n", + "\n", + "x0 = 1.000000 \n", + "\n", + "Iteration No. 11 \n", + "\n", + "x0 = 1.000000 \n", + "\n", + "Iteration No. 12 \n", + "\n", + "x0 = 1.000000 \n", + "\n", + "Iteration No. 13 \n", + "\n", + "x0 = 1.000000 \n", + "\n", + "Iteration No. 14 \n", + "\n", + "x0 = 1.000000 \n", + "\n", + "Iteration No. 15 \n", + "\n", + "x0 = 1.000000 \n", + "\n" + ] + } + ], + "source": [ + "from numpy import poly1d,polyval\n", + "\n", + "#False Position Method\n", + "\n", + "#Coefficients of polynomial in increasing order of power of x\n", + "A = [-2 , -1, 1]\n", + "x1 = 1 #\n", + "x2 = 3 #\n", + "fx = poly1d(A)\n", + "for i in range(1,16):\n", + " print 'Iteration No. %d \\n'%(i)\n", + " fx1 = fx(x1)\n", + " fx2 = fx(x2)\n", + " x0 = x1 - fx1*(x2-x1)/(fx2-fx1) \n", + " print 'x0 = %f \\n'%(x0)#\n", + " fx0 = fx(x0)#\n", + " if fx1*fx0 < 0:\n", + " x2 = x0 \n", + " else:\n", + " x1 = x0 #\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 6_07 Pg No. 147" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x2 = 1.000000\n", + "\n", + "Since f(1.000000) = 0, the root closer to the point x = 0 is 1.000000 \n", + "\n" + ] + } + ], + "source": [ + "from numpy import poly1d,polyval,polyder\n", + "#Root of the Equation using Newton Raphson Method\n", + "\n", + "#Coefficients of polynomial in increasing order of power of x\n", + "A = [ 2, -3, 1]#\n", + "fx = poly1d(A)\n", + "dfx = polyder(fx)\n", + "\n", + "x=[0]\n", + "f=[]\n", + "df=[]\n", + "for i in range(1,11):\n", + " f.append(fx(x[i-1]))\n", + " if f[i-1] != 0:\n", + " df.append(dfx(x[i-1]))\n", + " x.append(x[i-1] - f[i-1]/df[i-1])\n", + " print 'x%d = %f\\n'%(i+1,x[i])# \n", + " else:\n", + " print 'Since f(%f) = 0, the root closer to the point x = 0 is %f \\n'%(x[i-1],x[i-1] )\n", + " break\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 6_08 Pg No. 151" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x2 = 3.342105\n", + "\n", + "x3 = 2.248631\n", + "\n", + "x4 = 1.535268\n", + "\n", + "x5 = 1.079139\n", + "\n", + "x6 = 0.797330\n", + "\n", + "x7 = 0.632716\n", + "\n", + "From the results we can see that number of correct digits approximately doubles with each iteration\n" + ] + } + ], + "source": [ + "from numpy import poly1d,polyval,polyder\n", + "\n", + "#Root of the Equation using Newton Raphson Method\n", + "\n", + "#Coefficients of polynomial in increasing order of power of x\n", + "A = [ 6, 1 , -4 , 1 ]\n", + "fx = poly1d(A)\n", + "dfx = polyder(fx)\n", + "f=[];df=[]\n", + "x = [5.0] #\n", + "for i in range(1,7):\n", + " f.append(fx(x[i-1]))\n", + " if f[i-1] != 0:\n", + " df.append(dfx(x[i-1]))\n", + " x.append(x[i-1] - f[i-1]/df[i-1])\n", + " print 'x%d = %f\\n'%(i+1,x[i])\n", + " \n", + "\n", + "print 'From the results we can see that number of correct digits approximately doubles with each iteration'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 6_09 Pg No. 153" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + " For Iteration No. 1\n", + "\n", + "\n", + " x1 = 4.000000\n", + " x2 = 2.000000 \n", + " fx1 = -175.000000 \n", + " fx2 = -47.000000 \n", + " x3 = 1.265625 \n", + "\n", + "\n", + " For Iteration No. 2\n", + "\n", + "\n", + " x1 = 2.000000\n", + " x2 = 1.265625 \n", + " fx1 = -47.000000 \n", + " fx2 = -20.080566 \n", + " x3 = 0.717818 \n", + "\n", + "\n", + " For Iteration No. 3\n", + "\n", + "\n", + " x1 = 1.265625\n", + " x2 = 0.717818 \n", + " fx1 = -20.080566 \n", + " fx2 = -7.023891 \n", + " x3 = 0.423122 \n", + "\n", + "\n", + " For Iteration No. 4\n", + "\n", + "\n", + " x1 = 0.717818\n", + " x2 = 0.423122 \n", + " fx1 = -7.023891 \n", + " fx2 = -2.482815 \n", + " x3 = 0.261999 \n", + "\n", + "\n", + " For Iteration No. 5\n", + "\n", + "\n", + " x1 = 0.423122\n", + " x2 = 0.261999 \n", + " fx1 = -2.482815 \n", + " fx2 = -0.734430 \n", + " x3 = 0.194317 \n", + "\n", + "\n", + " For Iteration No. 6\n", + "\n", + "\n", + " x1 = 0.261999\n", + " x2 = 0.194317 \n", + " fx1 = -0.734430 \n", + " fx2 = -0.154860 \n", + " x3 = 0.176233 \n", + "\n", + "This can be still continued further for accuracy\n" + ] + } + ], + "source": [ + "from __future__ import division\n", + "from numpy import poly1d\n", + "#Root of the equation using SECANT Method\n", + "\n", + "#Coefficients of polynomial in increasing order of power of x\n", + "A = [ -10, -4, 1]\n", + "x1 = 4 #\n", + "x2 = 2 #\n", + "fx = poly1d(A)\n", + "for i in range(1,7):\n", + " print '\\n For Iteration No. %d\\n'%(i)\n", + " fx1 = fx(x1)\n", + " fx2 = fx(x2)\n", + " x3 = x2 - fx2*(x2-x1)/(fx2-fx1) #\n", + " print '\\n x1 = %f\\n x2 = %f \\n fx1 = %f \\n fx2 = %f \\n x3 = %f \\n'%(x1,x2,fx1,fx2,x3) #\n", + " x1 = x2#\n", + " x2 = x3#\n", + "\n", + "print 'This can be still continued further for accuracy'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 6_11 Pg No. 161" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + " x1 = -1.000000\n", + "\n", + "\n", + " x2 = 1.000000\n", + "\n", + "\n", + " x3 = 1.000000\n", + "\n", + "\n", + "1.000000 is root of the equation,since x3 - x2 = 0 \n", + "\n", + "\n", + "x1 = 1.000000\n", + "\n", + "\n", + "x2 = 1.000000\n", + "\n", + "\n", + " 1.000000 is root of the equation,since x2 - x1 = 0\n" + ] + } + ], + "source": [ + "from numpy import poly1d\n", + "from __future__ import division\n", + "#Fixed point method\n", + "\n", + "#Coefficients of polynomial in increasing order of power of x\n", + "A = [ -2, 1, 1 ]#\n", + "B = [ 2, 0, -1 ]#\n", + "gx = poly1d(B)\n", + "x = [0] ##initial guess x0 = 0\n", + "for i in range(2,11):\n", + " x.append (gx(x[i-2]))\n", + " print '\\n x%d = %f\\n'%(i-1,x[i-1])\n", + " if (x[i-1]-x[(i-2)]) == 0:\n", + " print '\\n%f is root of the equation,since x%d - x%d = 0 \\n'%(x[i-1],i-1,i-2)\n", + " break\n", + " \n", + "\n", + "#Changing initial guess x0 = -1\n", + "x[0] = -1 #\n", + "for i in range(2,11):\n", + " x[i-1]= gx(x[i-2])\n", + " print '\\nx%d = %f\\n'%(i-1,x[i-1])\n", + " if (x[i-1]-x[i-2]) == 0:\n", + " print '\\n %f is root of the equation,since x%d - x%d = 0'%(x[i-1],i-1,i-2)\n", + " break" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 6_12 Pg No. 162" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + " x0 = 1.000000 \n", + "\n", + " x1 = 2.500000 \n", + "\n", + " x2 = 1.666667 \n", + "\n", + " x3 = 1.250000 \n", + "\n", + " x4 = 1.000000 \n", + "\n", + "\n", + " x0 = 0.000000 \n", + "\n", + " x1 = 1.000000 \n", + "\n", + " x2 = 7.000000 \n", + "\n", + " x3 = 15.000000 \n", + "\n", + " x4 = 25.000000 \n", + "\n", + "\n", + " x0 = 1.000000 \n", + "\n", + " x1 = 2.250000 \n", + "\n", + " x2 = 2.333333 \n", + "\n", + " x3 = 2.625000 \n", + "\n", + " x4 = 3.000000 \n", + "\n", + " x5 = 3.416667 \n", + "\n", + " x6 = 3.857143 \n", + "\n" + ] + } + ], + "source": [ + "#Fixed point method\n", + "\n", + "A = [ -5, 0, 1 ]#\n", + "def g(x):\n", + " x = 5.0/x\n", + " return x\n", + "x = [1] #\n", + "print '\\n x0 = %f \\n'%(x[0])\n", + "for i in range(2,6):\n", + " x.append(g(i))\n", + " print ' x%d = %f \\n'%(i-1,x[i-1])\n", + " \n", + "\n", + "#Defining g(x) in different way\n", + "def g(x):\n", + " x = x**2 + x - 5\n", + " return x\n", + "x=[0]\n", + "print '\\n x0 = %f \\n'%(x[0])\n", + "for i in range(2,6):\n", + " x.append(g(i))\n", + " print ' x%d = %f \\n'%(i-1,x[i-1])\n", + "\n", + "#Third form of g(x)\n", + "def g(x):\n", + " x = (x + 5/x)/2\n", + " return x\n", + "x=[1]\n", + "print '\\n x0 = %f \\n'%(x[0])\n", + "for i in range(2,8):\n", + " x.append(g(i))\n", + " print ' x%d = %f \\n'%(i-1,x[i-1])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 6_13 Pg No. 169" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " x**2 - y**2 = 3 \n", + " x**2 + x*y \n", + "\n", + "\n", + " x0 = 1.000000 \n", + " y0 = 1.000000 \n", + "\n", + "\n", + " x1 = 2.500000 \n", + " y1 = 5.000000 \n", + "\n", + "\n", + " x2 = 2.750000 \n", + " y2 = 1.000000 \n", + "\n", + "\n", + " x3 = 3.500000 \n", + " y3 = -1.000000 \n", + "\n" + ] + } + ], + "source": [ + "#Solving System of non-linear equations using FIXED POINT METHOD\n", + "\n", + "print ' x**2 - y**2 = 3 \\n x**2 + x*y \\n'\n", + "def f(x,y):\n", + " x = y + 3/(x+y)\n", + " return x\n", + "def g(x):\n", + " y = (6-x**2)/x\n", + " return y\n", + "x=[1]\n", + "y=[1]\n", + "print '\\n x0 = %f \\n y0 = %f \\n'%(x[0],y[0])\n", + "for i in range(2,5):\n", + " x.append(f((i-1),(i-1)))\n", + " y.append(g((i-1)))\n", + " print '\\n x%d = %f \\n y%d = %f \\n'%(i-1,x[i-1],i-1,y[i-1])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 6_14 Pg No. 172" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x**2 + x*y = 6 \n", + " x**2 - y**2 = 3 \n", + "\n", + "\n", + " x1 = 0.875000 \n", + " y1 = -0.625000 \n", + "\n", + "\n", + " x2 = -0.437500 \n", + " y2 = -2.687500 \n", + "\n" + ] + } + ], + "source": [ + "#Solving System of Non-linear equations using Newton Raphson Method\n", + "\n", + "\n", + "print 'x**2 + x*y = 6 \\n x**2 - y**2 = 3 \\n'#\n", + "def F(x,y):\n", + " f = x**2 + x*y - 6\n", + " return f\n", + "def G(x,y):\n", + " g = x**2 - y**2 -3\n", + " return g\n", + "def dFx(x,y):\n", + " f1 = 2*x + y\n", + " return f1\n", + "def dFy(x,y):\n", + " f2 = y\n", + " return f2\n", + "def dGx(x,y):\n", + " g1 = 2*x\n", + " return g1\n", + "def dGy(x,y):\n", + " g2 = -2*y\n", + " return g2\n", + "x=[1]\n", + "y=[1]\n", + "\n", + "for i in range(2,4):\n", + " Fval = F(i,i)\n", + " Gval = G(i,i)\n", + " f1 = dFx(i-1,i-1)\n", + " f2 = dFy(i-1,i-1)\n", + " g1 = dGx(i-1,i-1)\n", + " g2 = dGy(i-1,i-1)\n", + " D = f1*g2 - f2*g1 \n", + " \n", + " x.append(x[i-2] - (Fval*g2 - Gval*f2)/D )\n", + " y.append(y[i-2] - (Gval*f1 - Fval*g1)/D )\n", + " print '\\n x%d = %f \\n y%d = %f \\n'%(i-1,x[i-1],i-1,y[i-1]) " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 6_15 Pg No. 176" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "b3 = 0.000000\n", + "\n", + "b2 = 0.000000\n", + "\n", + "b1 = 0.000000\n", + "\n", + "Thus the polynomial is\n", + " 2\n", + "15 x - 7 x + 1\n" + ] + } + ], + "source": [ + "from __future__ import division\n", + "from numpy import poly1d, arange\n", + "#Synthetic Division\n", + "\n", + "a = [-9 ,15 ,-7, 1]\n", + "b=[0,0,0,0]\n", + "for i in arange(3,0,-1):\n", + " b[i]= a[i] + b[i]*3\n", + " print 'b%d = %f\\n'%(i,b[i-1])\n", + "print 'Thus the polynomial is' \n", + "print poly1d(b)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 6_16 Pg No. 187" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "b3 = 1.000000 \n", + "\n", + "b2 = 1.800000 \n", + "\n", + "b1 = 3.240000 \n", + "\n", + "b0 = 14.032000 \n", + "\n", + "c3 = 1.000000 \n", + "\n", + "c2 = 1.800000 \n", + "\n", + "c1 = 3.240000 \n", + "\n", + "c0 = 14.032000 \n", + "\n", + "\n", + " D = 4.240000 \n", + " du = -0.694340 \n", + " dv = -5.250566 \n", + " u = 1.105660\n", + " v = -1.694340 \n", + "\n" + ] + } + ], + "source": [ + "#Quadratic factor of a polynomial using Bairstow's Method\n", + "\n", + "a = [ 10, 1 ,0 ,1]#\n", + "n = len(a)#\n", + "u = 1.8 #\n", + "v = -1 #\n", + "b=[];c=[]\n", + "for nn in range(n):\n", + " b.append(0)\n", + " c.append(0)\n", + "b[n-1] = a[n-1]\n", + "b[n-2] = a[n-2] + u*b[n-1]\n", + "c[n-1] = 0 \n", + "c[n-2] = b[n-1]\n", + "\n", + "\n", + "for i in range(n-2,0,-1):\n", + " b[i-1]= a[i-1]+ u*b[i] + v*b[i+1]\n", + " c[i-1]= b[i] + u*c[i] + v*c[i+1] \n", + "\n", + "\n", + "for i in range(n,0,-1):\n", + " print 'b%d = %f \\n'%(i-1,b[i-1])\n", + "\n", + "for i in range(n,0,-1):\n", + " print 'c%d = %f \\n'%(i-1,b[i-1])\n", + "\n", + "\n", + "D = c[1]*c[1] - c[0]*c[2]\n", + "du = -1*(b[1]*c[1] - c[0]*c[2])/D \n", + "dv = -1*(b[0]*c[1] - b[1]*c[0])/D \n", + "u = u + du #\n", + "v = v + du #\n", + "print '\\n D = %f \\n du = %f \\n dv = %f \\n u = %f\\n v = %f \\n'%(D,du,dv,u,v)\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 6_17 Pg No. 197" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + " x1 = 0.000000\n", + " x2 = 1.000000\n", + " x3 = 2.000000\n", + " f1 = -20.000000\n", + " f2 = -7.000000\n", + " f3 = 16.000000\n", + " h1 = -2.000000\n", + " h2 = -1.000000\n", + " d1 = -36.000000\n", + " d2 = -23.000000\n", + " a0 = 16.000000\n", + " a1 = 28.000000\n", + " a2 = 5.000000\n", + " h4 = -0.645934\n", + " x4 = 1.354066\n", + " \n", + "\n", + " x1 = 1.000000\n", + " x2 = 2.000000\n", + " x3 = 1.354066\n", + " f1 = -7.000000\n", + " f2 = 16.000000\n", + " f3 = -0.309679\n", + " h1 = -0.354066\n", + " h2 = 0.645934\n", + " d1 = -6.690321\n", + " d2 = 16.309679\n", + " a0 = -0.309679\n", + " a1 = 21.145451\n", + " a2 = 6.354066\n", + " h4 = 0.014581\n", + " x4 = 1.368647\n", + " \n", + "\n", + " x1 = 2.000000\n", + " x2 = 1.354066\n", + " x3 = 1.368647\n", + " f1 = 16.000000\n", + " f2 = -0.309679\n", + " f3 = -0.003394\n", + " h1 = 0.631353\n", + " h2 = -0.014581\n", + " d1 = 16.003394\n", + " d2 = -0.306286\n", + " a0 = -0.003394\n", + " a1 = 21.103381\n", + " a2 = 6.722713\n", + " h4 = 0.000161\n", + " x4 = 1.368808\n", + " \n", + "\n", + " x1 = 1.354066\n", + " x2 = 1.368647\n", + " x3 = 1.368808\n", + " f1 = -0.309679\n", + " f2 = -0.003394\n", + " f3 = -0.000001\n", + " h1 = -0.014742\n", + " h2 = -0.000161\n", + " d1 = -0.309678\n", + " d2 = -0.003392\n", + " a0 = -0.000001\n", + " a1 = 21.096136\n", + " a2 = 6.091521\n", + " h4 = 0.000000\n", + " x4 = 1.368808\n", + " \n", + "root of the polynomial is x4 = 1.368808\n" + ] + } + ], + "source": [ + "from math import sqrt\n", + "#Solving Leonard's equation using MULLER'S Method\n", + "\n", + "def f(x):\n", + " y = x**3 + 2*x**2 + 10*x - 20\n", + " return y\n", + "x1 = 0 #\n", + "x2 = 1 #\n", + "x3 = 2 #\n", + "for i in range(1,11):\n", + " f1 = f(x1)\n", + " f2 = f(x2)\n", + " f3 = f(x3)\n", + " h1 = x1-x3 #\n", + " h2 = x2-x3 #\n", + " d1 = f1 - f3 #\n", + " d2 = f2 - f3 #\n", + " D = h1*h2*(h1-h2)#\n", + " a0 = f3 #\n", + " a1 = (d2*h1**2 - d1*h2**2)/D #\n", + " a2 = (d1*h2 - d2*h1)/D #\n", + " if abs(-2*a0/( a1 + sqrt( a1**2 - 4*a0*a2 ) )) < abs( -2*a0/( a1 - sqrt( a1**2 - 4*a0*a2 ) )):\n", + " h4 = -2*a0/(a1 + sqrt(a1**2 - 4*a0*a2))#\n", + " else:\n", + " h4 = -2*a0/(a1 - sqrt(a1**2 - 4*a0*a2))\n", + " \n", + " x4 = x3 + h4 #\n", + " print '\\n x1 = %f\\n x2 = %f\\n x3 = %f\\n f1 = %f\\n f2 = %f\\n f3 = %f\\n h1 = %f\\n h2 = %f\\n d1 = %f\\n d2 = %f\\n a0 = %f\\n a1 = %f\\n a2 = %f\\n h4 = %f\\n x4 = %f\\n '%(x1,x2,x3,f1,f2,f3,h1,h2,d1,d2,a0,a1,a2,h4,x4) #\n", + " relerr = abs((x4-x3)/x4)#\n", + " if relerr <= 0.00001:\n", + " print 'root of the polynomial is x4 = %f'%(x4)\n", + " break\n", + " \n", + " x1 = x2 #\n", + " x2 = x3 #\n", + " x3 = x4 #" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.9" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} |