{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 5 - Boundary Value Problems" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exa 5.1 Page 87" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the value of y at x=.5 from finite difference method is -0.25\n", "the value of y from exact soln at x=.5 is -0.25\n" ] } ], "source": [ "from __future__ import division\n", "\n", "#finite difference method\n", "#given d2y_by_dx2-2=0 hence it is dirichlet's problem\n", "\n", "x_1=0 ; y_1=0 #initial boundary conditions\n", "x_3=1 ; y_3=0\n", "\n", "delta_x=.5 #since we have to find y_2 at x=.5 so x_2=.5 \n", "#in the central difference method substituting i=2 we have\n", "#(y_3-2*y_2+y_1)/(delta_x)**2=2\n", "#since y_1 & y_3=0 as per B.C.\n", "y_2=(y_3+y_1-2*delta_x**2)/2\n", "print \"the value of y at x=.5 from finite difference method is\",y_2\n", "x=.5\n", "y_exact=x**2-x\n", "print \"the value of y from exact soln at x=.5 is\",y_exact" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exa 5.2 Page 89" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "to solve this problem we will take delta x=.5 since we have to find the value at x=.5\n", "the value of y at x=.5 is -0.25\n" ] } ], "source": [ "from __future__ import division\n", "#boundary conditions are: x=0 at y=0# dy/dx=1 at x=1\n", "print \"to solve this problem we will take delta x=.5 since we have to find the value at x=.5\"\n", "delta_x=.5\n", "y_1=0\n", "#using central difference eqn\n", "dy_by_dx=1 #at x=1, i=3\n", "#y_4=dy/dx*2*delta_x+y_2 sincefrom B.C. at node 3\n", "\n", "#y_2=delta_x**2+y_3-delta_x on substituting the value of y_4\n", "y_3=-(2*delta_x**2+2*(delta_x**2-delta_x)-y_1) #on substituting the value of y_2\n", "y_2=delta_x**2+y_3-delta_x\n", "print \"the value of y at x=.5 is\",y_2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exa 5.3 Page 89" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " the solution of eg 5.3 -->Discretization in 1-D space\n", "the value of y at x= 0.333333333333 0.666666666667 is respectively 0.111111111111 -0.222222222222\n" ] } ], "source": [ "from __future__ import division\n", "from numpy import arange\n", "print \"the solution of eg 5.3 -->Discretization in 1-D space\"\n", "#given the source term f(x)=4x**2-2x-4\n", "#given eqn d2y/dx2-2y=f(x)\n", "y_1=0\n", "y_4=-1\n", "delta_x=1/3 #since given 3 parts and length=1\n", "for i in range(0,4):\n", " j=arange(0,1+delta_x,delta_x)\n", "\n", "#given to divide the line in 3 parts\n", "#at node 2\n", "#y_3-2*y_2\n", "def fx3(x):\n", " d=(4*x**2-2*x-4)\n", " return d\n", "f2=fx3(j[1])\n", "f3=fx3(j[2])\n", "y_3=((f2)*delta_x**2+(2+2*delta_x**2)*((f3)*delta_x**2-y_4)-y_1)/(1-(2+2*delta_x**2)**2)\n", "y_2=(f3+2*y_3)*delta_x**2+2*y_3-y_4\n", "print \"the value of y at x=\",j[1],j[2],\"is respectively\",y_2,y_3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ## Exa 5.4 Page 90" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the exact analytical soln are\n", "0.111111111111\n", "-0.222222222222\n", "-1.0\n", "the value of f(x) at node 2 3 and 4 are -4.22222222222 -3.55555555556 -2.0\n", "the values of y2, y3, y4 by TDMA method are\n", "0.111111111111\n", "-0.222222222222\n", "-1.0\n" ] } ], "source": [ "from __future__ import division\n", "from numpy import arange,zeros\n", "y_1=0\n", "dy_by_dx=-3 #at x=1\n", "delta_x=1/3 #since given 3 parts and length=1\n", "for i in range(0,4):\n", " j=arange(0,1+delta_x,delta_x)\n", "\n", "#given to divide the line in 3 parts\n", "def fx3(x):\n", " d=(4*x**2-2*x-4)\n", " return d\n", "\n", "f2=fx3(j[1])\n", "f3=fx3(j[2])\n", "f4=fx3(j[3])\n", "print \"the exact analytical soln are\"\n", "y=[]\n", "for i in range(1,4):\n", " y.append(-2*j[i]**2+j[i])\n", " print y[i-1]\n", "\n", "#using B.C.-2 at node 4 we get\n", "#(y_5-y_3)/(2*delta_x)=-3\n", "#eqn at node 2\n", "#-20*y_2+9*y_3=f2\n", "#at node 3\n", "#9*y_2-20*y_3+9*y_4=f3\n", "#at node 4\n", "#18*y_3-20*y_4=16\n", "print \"the value of f(x) at node 2 3 and 4 are\",f2,f3,f4\n", "#now solving the equations using TDMA method \n", "\n", "a=[0,9,18] #sub diagonal assignment\n", "\n", "b=[]\n", "for j in range(1,4):\n", " b.append(-20) #main diagonal assignment\n", "c=[]\n", "for k in range(1,3):\n", " c.append(9) #super diagonal assignment\n", "\n", "d=[f2,f3,16] #given values assignment\n", "\n", "i=1#\n", "n=3#\n", "beta1=[b[i-1]]# #initial b is equal to beta since a1=0\n", "gamma1=[d[i-1]/beta1[i-1]] #since c7=0\n", "m=i+1#\n", "for j in range(m,n+1):\n", " beta1.append(b[j-1]-a[j-1]*c[j-2]/beta1[j-2])\n", " gamma1.append((d[j-1]-a[j-1]*gamma1[j-2])/beta1[j-1])\n", "x=zeros(n)\n", "x[-1]=gamma1[n-1] #since c7=0\n", "n1=n-i#\n", "for k in range(1,n1+1):\n", " j=n-k# \n", " x[j-1]=gamma1[j-1]-c[j-1]*x[j]/beta1[j-1]\n", "\n", "print \"the values of y2, y3, y4 by TDMA method are\"\n", "for i in range(1,4):\n", " print x[i-1]\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exa 5.5 Page 91" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the analytical soln are\n", "0.648054273664\n", "0.651297246159\n", "0.661058620401\n", "0.677436091507\n", "0.70059357071\n", "0.730762825846\n", "0.768245800962\n", "0.81341763827\n", "0.8667304327\n", "0.92871775662\n", "the values of y from y1 to y10 by TDMA method are\n", "0.648259699273\n", "0.65150099777\n", "0.661257306244\n", "0.67762618778\n", "0.700771331194\n", "0.730924187921\n", "0.768386286526\n", "0.813532247997\n", "0.866813531948\n", "0.928762951218\n" ] } ], "source": [ "from __future__ import division\n", "from math import cosh\n", "from numpy import zeros\n", "delta_x=.1\n", "y_11=1\n", "dy_by_dx_1=0 #dy/dx at x=0\n", "# given eqn d2y/dx2=y\n", "print \"the analytical soln are\"\n", "y_an=[]\n", "for i in range(1,11):\n", " y_an.append(cosh((i-1)/10)/cosh(1))\n", " print y_an[i-1]\n", "\n", "#using central difference method we have \n", "#y(i-1) - (2+delat_x**2)y(i) + y(i+1)=0\n", "#therefore the eqns can be solved using TDMA method\n", "a=[None]\n", "for i in range(2,11):\n", " a.append(1) #sub diagonal assignment\n", "b=[]\n", "for j in range(1,11):\n", " b.append(-2.01) #main diagonal assignment\n", "\n", "c=[2]\n", "for k in range(2,10):\n", " c.append(1) #super diagonal assignment\n", "d=[]\n", "for l in range(1,10):\n", " d.append(0)\n", " #given values assignment\n", "d.append(-1)\n", "i=1#\n", "n=10#\n", "beta1=[b[i-1]] #initial b is equal to beta since a1=0\n", "gamma1=[d[i-1]/beta1[i-1]] #since c7=0\n", "m=i+1#\n", "for j in range(m,n+1):\n", " beta1.append(b[j-1]-a[j-1]*c[j-2]/beta1[j-2])\n", " gamma1.append((d[j-1]-a[j-1]*gamma1[j-2])/beta1[j-1])\n", "\n", "x=zeros(10)\n", "x[n-1]=gamma1[n-1] #since c7=0\n", "n1=n-i#\n", "for k in range(1,n1+1):\n", " j=n-k\n", " x[j-1]=gamma1[j-1]-c[j-1]*x[j]/beta1[j-1]\n", "\n", "print \"the values of y from y1 to y10 by TDMA method are\"\n", "for i in range(1,11):\n", " print x[i-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exa 5.6 Page 92" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the values of T from T2 to T9 by TDMA method are\n", "111.111111111\n", "122.222222222\n", "133.333333333\n", "144.444444444\n", "155.555555556\n", "166.666666667\n", "177.777777778\n", "188.888888889\n" ] } ], "source": [ "from __future__ import division\n", "from numpy import zeros\n", "T_1=100 ; T_10=200\n", "delta_x=(T_10-T_1)/9 #since 9 divisions are to be made\n", "#using central difference method we get\n", "#for node 2--> 2*T_2-T_3=100\n", "a=[0]\n", "for i in range(2,9):\n", " a.append(-1) #sub diagonal assignment\n", "b=[]\n", "for j in range(1,9):\n", " b.append(2) #main diagonal assignment\n", "c=[]\n", "for k in range(1,8):\n", " c.append(-1) #super diagonal assignment\n", "\n", "d=[100]\n", "for l in range(2,8):\n", " d.append(0); #given values assignment\n", "\n", "d.append(200)\n", "\n", "\n", "i=1;\n", "n=8;\n", "beta1=[b[i-1]] #initial b is equal to beta since a1=0\n", "gamma1=[d[i-1]/beta1[i-1]]; #since c7=0\n", "m=i+1;\n", "for j in range(m,n+1):\n", " beta1.append(b[j-1]-a[j-1]*c[j-2]/beta1[j-2])\n", " gamma1.append((d[j-1]-a[j-1]*gamma1[j-2])/beta1[j-1])\n", "\n", "x=zeros(n) \n", "x[-1]=gamma1[n-1] #since c7=0\n", "n1=n-i;\n", "for k in range(1,n1+1):\n", " j=n-k\n", " x[j-1]=gamma1[j-1]-c[j-1]*x[j]/beta1[j-1];\n", "\n", "print \"the values of T from T2 to T9 by TDMA method are\"\n", "for i in range(1,9):\n", " print x[i-1]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exa 5.7 Page 93" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the values of T from T1 to T4 in Celsius by TDMA method are\n", "231.893171899\n", "199.529667042\n", "180.886766374\n", "174.799288605\n" ] } ], "source": [ "from __future__ import division\n", "from math import pi,sqrt\n", "from numpy import zeros\n", "dia=.02\n", "l=.05\n", "T_0=320\n", "delta_x=l/4\n", "k=50\n", "h=100\n", "T_surr=20\n", "#B.C--> d(theta)/dx+h(theta)/k=0 at x=0.05\n", "#let m=sqrt(hP/kA)\n", "P=pi*dia\n", "A=pi*dia**2/4\n", "m=sqrt(h*P/(k*A))#\n", "#using central difference method we get \n", "#-theta(i-1)+(2+(m*delta_x**2)*theta(i)+theta(i+1))=0\n", "theta_0=T_0-T_surr\n", "#using B.C. at node 4 we get--> theta(5)=theta(3)-2h*theta(4)*delta_x/k\n", "#now the eqns can be solved using TDMA method\n", "a=[None]\n", "for i in range(2,4):\n", " a.append(-1 ) #sub diagonal assignment\n", "\n", "a.append(-2)\n", "b=[]\n", "for j in range(1,4):\n", " b.append(2.0625)# #main diagonal assignment\n", "\n", "b.append(2.1125)\n", "for k in range(1,4):\n", " c.append(-1) #super diagonal assignment\n", "d=[300]\n", "for l in range(2,5):\n", " d.append(0)\n", " #given values assignment\n", "\n", "i=1#\n", "n=4#\n", "beta1=[b[i-1]] #initial b is equal to beta since a1=0\n", "gamma1=[d[i-1]/beta1[i-1]] #since c7=0\n", "m=i+1\n", "for j in range(m,n+1):\n", " beta1.append(b[j-1-1]-a[j-1]*c[j-2-1]/beta1[j-1-1])\n", " gamma1.append((d[j-1]-a[j-1]*gamma1[j-2])/beta1[j-1])\n", "\n", "x=zeros(n) \n", "x[n-1]=gamma1[n-1]# #since c7=0\n", "n1=n-i#\n", "for k in range(1,n1+1):\n", " j=n-k#\n", " x[j-1]=gamma1[j-1]-c[j-1]*x[j]/beta1[j-1]\n", "\n", "print \"the values of T from T1 to T4 in Celsius by TDMA method are\"\n", "for i in range(1,5):\n", " print x[i-1]-T_surr\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exa 5.8 Page 94" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the values of conc. at x=.5mm or at the 50th node is 0.730764441227\n" ] } ], "source": [ "from __future__ import division\n", "from math import pi,sqrt\n", "from numpy import zeros\n", "\n", "lnght=.001\n", "k_const=.001\n", "D=10**-9\n", "delta_x=l/100\n", "C=1 #in mol/m3\n", "#B.C. are C=1 at x=0\n", "# dC/dx=0 at x=10**-3 since at the end point conc. is const.\n", "#using central difference method we get the following eqns which can be solved using TDMA method\n", "a=[0]\n", "for i in range(2,100):\n", " a.append(1) #sub diagonal assignment\n", "\n", "a.append(2) #since C99=C100 using B.C. \n", "\n", "b=[]\n", "for j in range(1,101):\n", " b.append(-2.0001) #main diagonal assignment\n", "\n", "c=[]\n", "for k in range(1,100):\n", " c.append(1) #super diagonal assignment\n", "\n", "d=[-1]\n", "for l in range(2,101):\n", " d.append(0)\n", " #given values assignment\n", "i=1#\n", "n=100#\n", "beta1 =[b[i-1]] #initial b is equal to beta since a1=0\n", "gamma1=[d[i-1]/beta1[i-1]] #since c7=0\n", "m=i+1#\n", "for j in range(m,n+1):\n", " beta1.append(b[j-1]-a[j-1]*c[j-2]/beta1[j-2])\n", " gamma1.append((d[j-1]-a[j-1]*gamma1[j-2])/beta1[j-1])\n", "\n", "x=zeros(n) \n", "x[n-1]=gamma1[n-1] #since c7=0\n", "n1=n-i#\n", "for k in range(1,n1+1):\n", " j=n-k# \n", " x[j-1]=gamma1[j-1]-c[j-1]*x[j]/beta1[j-1]\n", "\n", "print \"the values of conc. at x=.5mm or at the 50th node is\",x[49]" ] } ], "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 }