diff options
Diffstat (limited to 'Numerical_Methods_by_E._Balaguruswamy/chapter10.ipynb')
-rw-r--r-- | Numerical_Methods_by_E._Balaguruswamy/chapter10.ipynb | 282 |
1 files changed, 282 insertions, 0 deletions
diff --git a/Numerical_Methods_by_E._Balaguruswamy/chapter10.ipynb b/Numerical_Methods_by_E._Balaguruswamy/chapter10.ipynb new file mode 100644 index 00000000..7de04b99 --- /dev/null +++ b/Numerical_Methods_by_E._Balaguruswamy/chapter10.ipynb @@ -0,0 +1,282 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 10 - Curve fitting regression" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 10_01 Pg No. 326" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "b = [-1 -1 0 0 1]\n", + "a = [ 3 3 1 1 -2]\n", + "y= [-x + 3 -x + 3 1 1 x - 2]\n" + ] + } + ], + "source": [ + "from numpy import array\n", + "from sympy.abc import x\n", + "#Fitting a Straight Line\n", + "\n", + "X = range(1,6)\n", + "Y = array([[ 3, 4, 5 ,6 ,8 ]])\n", + "n = len(X)#\n", + "X=array(X)\n", + "b = ( n*sum(X*Y) - sum(X)*sum(Y) )/( n*sum(X*X) - (sum(X))**2 )\n", + "a = sum(Y)/n - b*sum(X)/n\n", + "print 'b = ',b\n", + "print 'a = ',a\n", + "y = a + b*x\n", + "print 'y=',y" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 10_02 Pg No. 331" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "b = 2.0\n", + "lna = -0.69314718056\n", + "a = 0.5\n", + "\n", + " The power function equation obtained is \n", + " y = 0.5x**2\n" + ] + } + ], + "source": [ + "from numpy import array,log,exp\n", + "from sympy.abc import x\n", + "\n", + "#Fitting a Power-Function model to given data\n", + "\n", + "X = array(range(1,6))\n", + "Y = [ 0.5, 2 ,4.5 ,8 ,12.5 ]\n", + "Xnew = log(X)\n", + "Ynew = log(Y)\n", + "n = len(Xnew)\n", + "b = ( n*sum(Xnew*Ynew) - sum(Xnew)*sum(Ynew) )/( n*sum(Xnew*Xnew) - ( sum(Xnew) )**2 )\n", + "lna = sum(Ynew)/n - b*sum(Xnew)/n\n", + "a = exp(lna)\n", + "print 'b = ',b\n", + "print 'lna = ',lna\n", + "print 'a = ',a\n", + "print '\\n The power function equation obtained is \\n y = %.4Gx**%.4G'%(a,b)#" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 10_03 Pg No. 332" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "b = 37.6294062985\n", + "a = 20.9234245534\n", + "The relationship between T and t is \n", + "T = 37.63*e**(t/4) + 20.92 \n", + "\n", + "The temperature at t = 6 is 189.566723485\n" + ] + } + ], + "source": [ + "from numpy import array,log,exp\n", + "time = array(range(1,5))\n", + "T = [ 70 ,83 ,100 ,124 ]\n", + "t = 6\n", + "Fx = exp(time/4.0)\n", + "n = len(Fx)\n", + "Y = T #\n", + "b = ( n*sum(Fx*Y) - sum(Fx)*sum(Y) )/( n*sum(Fx*Fx) - (sum(Fx))**2 )\n", + "a = sum(Y)/n - b*sum(Fx)/n\n", + "print 'b = ',b\n", + "print 'a = ',a\n", + "print 'The relationship between T and t is \\nT = %.4G*e**(t/4) + %.4G \\n'%(b,a)\n", + "#deff('T = T(t)'%('T = b*exp(t/4) + a '\n", + "def T(t):\n", + " tt=b*exp(t/4.0)+a\n", + " return tt\n", + " \n", + "T_6 = T(6)\n", + "print 'The temperature at t = 6 is',T_6" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 10_04 Pg NO. 335" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Using CA = B form , we get\n", + "B [ 6. 62. 190.]\n", + "C [[ 1. 1. 4.]\n", + " [ 1. 4. 10.]\n", + " [ 4. 10. 30.]]\n", + "A = [[ 20. 103.33333333 -190. ]\n", + " [ 10. 144.66666667 -190. ]\n", + " [ -6. -62. 95. ]]\n", + "Therefore the least sqaures polynomial is\n", + " y = 1J + 1J*x + 1J*x**2 \n", + "[ 20. 103.33333333 -190. ]\n", + "[ 10. 144.66666667 -190. ]\n", + "[ -6. -62. 95.]\n" + ] + } + ], + "source": [ + "from numpy import array,ones,identity\n", + "from numpy.linalg import inv\n", + "\n", + "#Curve Fitting\n", + "\n", + "x = array(range(1,5))\n", + "y = [6, 11, 18, 27 ]\n", + "n = len(x) #Number of data points\n", + "m = 2+1 #Number of unknowns\n", + "print 'Using CA = B form , we get'\n", + "C=identity(m)\n", + "B=ones(m)\n", + "for j in range(0,m):\n", + " for k in range(0,m):\n", + " C[j,k] = sum(x**(j+k-2))\n", + " \n", + " B[j] = sum( y*( x**( j-1 ) ) )\n", + "\n", + "print 'B',B\n", + "print 'C',C\n", + "A = inv(C)*B\n", + "print 'A = ',A\n", + "print 'Therefore the least sqaures polynomial is\\n y = 1J + 1J*x + 1J*x**2 \\n',(A[0])\n", + "print A[1]\n", + "print A[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example No. 10_05 Pg No. 342" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "C=\n", + "[[ 4. 10. 6.]\n", + " [ 10. 30. 20.]\n", + " [ 6. 20. 14.]]\n", + "B=\n", + "[ 84. 240. 156.]\n", + "\n", + " The regression plane is \n", + " y = 5 + 6*x + 0*z \n", + "\n" + ] + } + ], + "source": [ + "from numpy import array,ones,identity,arange,vstack,transpose\n", + "from scipy.sparse.linalg import lsqr\n", + "#Plane Fitting\n", + "\n", + "x = range(1,5)\n", + "z = range(0,4)\n", + "y = arange(12,31,6)\n", + "n = len(x) #Number of data points\n", + "m = 3 #Number of unknowns\n", + "G = vstack([ones(n),x,z])\n", + "H = transpose(G)\n", + "C = G.dot(H)\n", + "B = y.dot(H)\n", + "D = lsqr(C,B)\n", + "print 'C=\\n',C\n", + "print 'B=\\n',B\n", + "print '\\n The regression plane is \\n y = %d + %.f*x + %d*z \\n'%(D[0][0],D[0][1],D[0][2])\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.9" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} |