{ "metadata": { "name": "", "signature": "sha256:2221c3190a93e10b0d60c9c13471ed510a28f24c08c158331f416508f81a0bfb" }, "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": 120 }, { "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;u2=0.25;u3=0.5;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;u2=u22;u3=u33;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;u2=0.3125;u3=0.5625;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": [ "import math\n", "#bender-schimdt's formula and crank-nicolson formula\n", "#example 9.7\n", "#page 363\n", "#bender -schimdt's formula\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;u12=(u2+u4)/2;u13=u12;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;u22=(u12+u14)/2;u23=u22;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)))" ], "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" ] } ], "prompt_number": 119 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Ex9.8:pg-364" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from numpy import matrix\n", "#heat equation using crank-nicolson method\n", "#example 9.8\n", "#page 364\n", "z=0.01878;\n", "#h=1/2;l=1/8,i=1\n", "u01=0.0;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' ,'-1' ,'0'],['1','-3','1'],['0','1','-3']])\n", "C=matrix([['0'],['0'],['-1/8']])\n", "#here we found inverese of A then we multipy it with C\n", "u12=0.01786\n", "print \"u12=%f\\n\\n\" % (u12)\n", "print \"error is %f\\n\\n\" %(math.fabs(u12-z))\n" ], "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.017860\n", "\n", "\n", "error is 0.000920\n", "\n", "\n" ] } ], "prompt_number": 105 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }