{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 1 - Linear Algebraic Equations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exa1.1 Page 24" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the solution of ex 1.1 by TDMA method is\n", "317.5\n", "395.0\n", "432.5\n", "430.0\n", "387.5\n", "305.0\n", "182.5\n" ] } ], "source": [ "from numpy import zeros\n", "from __future__ import division\n", "a=[0];b=[];c=[]\n", "for i in range(1,7):\n", " a.append(1) #sub diagonal assignment\n", "\n", "for j in range(0,7):\n", " b.append(-2) #main diagonal assignment\n", "\n", "for k in range(0,6):\n", " c.append(1) #super diagonal assignment\n", "\n", "d=[-240] #given values assignment\n", "for l in range(1,6):\n", " d.append(-40) \n", "\n", "d.append(-60)\n", "i=1#\n", "n=7#\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-1-1]/beta1[j-1-1])\n", " gamma1.append((d[j-1]-a[j-1]*gamma1[j-1-1])/beta1[j-1])\n", "\n", "#x(n)=gamma1(n)# #since c7=0\n", "x=zeros(n-1)\n", "#x[n-1]=gamma1[n-1]\n", "x=list(x)\n", "x.append(gamma1[-1])\n", "n1=n-i# \n", "\n", "for k in range(0,n1):\n", " j=n-k-1\n", " x[j-1]=gamma1[j-1]-c[j-1]*x[j+1-1]/beta1[j-1]\n", "\n", "\n", "print \"the solution of ex 1.1 by TDMA method is\"\n", "for i in range(0,7):\n", " print x[i]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exa1.2 Page 24" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the solution using gauss elimination method is 3, 4 and 5\n" ] } ], "source": [ "from __future__ import division\n", "a1=10; a2=1; a3=2; #1st row\n", "b1=2; b2=10; b3=1; #2nd row\n", "c1=1; c2=2; c3=10; #3rd row \n", "d1=44; d2=51; d3=61; #given values\n", "\n", "b3=b3-(b1/a1)*a3 # for making b1=0\n", "b2=b2-(b1/a1)*a2\n", "d2=d2-(b1/a1)*d1\n", "b1=b1-(b1/a1)*a1\n", "\n", "c3=c3-(c1/a1)*a3 # for making c1=0\n", "c2=c2-(c1/a1)*a2\n", "d3=d3-(c1/a1)*d1\n", "c1=c1-(c1/a1)*a1\n", "\n", "c3=c3-(c2/b2)*b3 # for making c2=0\n", "d3=d3-(c2/b2)*d2\n", "c2=c2-(c2/b2)*b2\n", "\n", "x3=d3/c3# # final values of x\n", "x2=(d2-(b3*x3))/b2#\n", "x1=(d1-(x3*a3)-(x2*a2))/a1#\n", "print \"the solution using gauss elimination method is %d, %d and %d\"%(x1,x2,x3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exa1.3 Page 26" ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the solution using gauss elimination method is 3, -2 and 0\n" ] } ], "source": [ "from __future__ import division\n", "a1=3; a2=1; a3=-2; #1st row\n", "b1=-1; b2=4; b3=-3; #2nd row\n", "c1=1; c2=-1; c3=4; #3rd row \n", "d1=9; d2=-8; d3=1; #given values\n", "\n", "b3=b3-(b1/a1)*a3 # for making b1=0\n", "b2=b2-(b1/a1)*a2\n", "d2=d2-(b1/a1)*d1\n", "b1=b1-(b1/a1)*a1\n", "\n", "c3=c3-(c1/a1)*a3 # for making c1=0\n", "c2=c2-(c1/a1)*a2\n", "d3=d3-(c1/a1)*d1\n", "c1=c1-(c1/a1)*a1\n", "\n", "c3=c3-(c2/b2)*b3 # for making c2=0\n", "d3=d3-(c2/b2)*d2\n", "c2=c2-(c2/b2)*b2\n", "\n", "x3=d3/c3# # final values of x\n", "x2=(d2-(b3*x3))/b2#\n", "x1=(d1-(x3*a3)-(x2*a2))/a1#\n", "print \"the solution using gauss elimination method is %d, %d and %d\"%(x1,x2,x3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exa1.4 Page 27" ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the values of MOLAR FLOW RATES of D1, B1, D2, B2 respectively are : 26, 18, 9 and 17\n", "the composition of stream B is 0.077, 0.247, 0.467 & 0.210\n", "the composition of stream D is 0.274 0.492 0.12 0.114\n" ] } ], "source": [ "from __future__ import division\n", "\n", "a1=.35; a2=.16; a3=.21; a4=.01 #1st row \n", "b1=.54; b2=.42; b3=.54; b4=.1 #2nd row\n", "c1=.04; c2=.24; c3=.1; c4=.65 #3rd row\n", "d1=.07; d2=.18; d3=.15; d4=.24 #4th row \n", "r1=14; r2=28; r3=17.5; r4=10.5 #given values\n", "\n", "b4=b4-(b1/a1)*a4 # for making b1=0\n", "b3=b3-(b1/a1)*a3\n", "b2=b2-(b1/a1)*a2\n", "r2=r2-(b1/a1)*r1\n", "b1=b1-(b1/a1)*a1\n", "\n", "c4=c4-(c1/a1)*a4 # for making c1=0\n", "c3=c3-(c1/a1)*a3\n", "c2=c2-(c1/a1)*a2\n", "r3=r3-(c1/a1)*r1\n", "c1=c1-(c1/a1)*a1\n", "\n", "d4=d4-(d1/a1)*a4 # for making d1=0\n", "d3=d3-(d1/a1)*a3\n", "d2=d2-(d1/a1)*a2\n", "r4=r4-(d1/a1)*r1\n", "d1=d1-(d1/a1)*a1\n", "\n", "c4=c4-(c2/b2)*b4 # for making c2=0\n", "c3=c3-(c2/b2)*b3\n", "r3=r3-(c2/b2)*r2\n", "c2=c2-(c2/b2)*b2\n", "\n", "d4=d4-(d2/b2)*b4 # for making d2=0\n", "d3=d3-(d2/b2)*b3\n", "r4=r4-(d2/b2)*r2\n", "d2=d2-(d2/b2)*b2\n", "\n", "d4=d4-(d3/c3)*c4 #for making d3=0\n", "r4=r4-(d3/c3)*r3\n", "d3=d3-(d3/c3)*c3\n", "\n", "B2=r4/d4#\n", "D2=(r3-(c4*B2))/c3#\n", "B1=(r2-(D2*b3)-(B2*b4))/b2#\n", "D1=(r1-(B2*a4)-(D2*a3)-(B1*a2))/a1#\n", "print \"the values of MOLAR FLOW RATES of D1, B1, D2, B2 respectively are : %.f, %.f, %.f and %.f\"%(D1,B1,D2,B2)\n", "\n", "B=D2+B2#\n", "x1B=(.21*D2 + .01*B2)/B#\n", "x2B=(.54*D2 + .1*B2)/B#\n", "x3B=(.1*D2 + .65*B2)/B#\n", "x4B=(.15*D2 + .24*B2)/B#\n", "print \"the composition of stream B is %.3f, %.3f, %.3f & %.3f\"%(x1B,x2B,x3B,x4B)\n", "\n", "D=D1+B1#\n", "x1D=(.35*D1 + .16*B1)/D#\n", "x2D=(.54*D1 + .42*B1)/D#\n", "x3D=(.04*D1 + .24*B1)/D#\n", "x4D=(.07*D1 + .18*B1)/D#\n", "print \"the composition of stream D is\",x1D,x2D,x3D,x4D" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exa1.5 Page 28" ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the values of x1,x2,x3 respectively is\n", "3\n", "4\n", "5\n" ] } ], "source": [ "from __future__ import division\n", "xnew=[];e=[]\n", "for i in range(0,3):\n", " xnew.append(2)\n", " e.append(1)\n", "\n", "x=1e-6\n", "while e[0]>x and e[1]>x and e[2]>x:\n", " xold=[]\n", " for i in range(0,3):\n", " xold.append(xnew[i])\n", " \n", " xnew[0]=(44-xold[1]-2*xold[2])/10\n", " xnew[1]=(-2*xnew[0]+51-xold[2])/10\n", " xnew[2]=(-2*xnew[1]-xnew[0]+61)/10\n", " e=[]\n", " for i in range(0,3):\n", " e.append(abs(xnew[i]-xold[i]))\n", " \n", "print \"the values of x1,x2,x3 respectively is\"\n", "for i in range(0,3):\n", " print '%.f'%xnew[i]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exa1.6 Page 28" ] }, { "cell_type": "code", "execution_count": 75, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the values of x1,x2,x3 respectively is\n", "3\n", "-2\n", "-1\n" ] } ], "source": [ "from __future__ import division\n", "xnew=[];e=[];\n", "for i in range(0,3):\n", " xnew.append(2)\n", " e.append(1)\n", "\n", "x=1e-6\n", "while e[0]>x and e[1]>x and e[2]>x:\n", " xold=[]\n", " for i in range(0,3):\n", " xold.append(xnew[i])\n", " \n", " xnew[0]=(9-xold[1]+2*xold[2])/3\n", " xnew[1]=(xnew[0]-8+3*xold[2])/4\n", " xnew[2]=(xnew[1]-xnew[0]+1)/4\n", " e=[]\n", " for i in range(0,3):\n", " e.append(abs(xnew[i]-xold[i]))\n", " \n", "print \"the values of x1,x2,x3 respectively is\"\n", "for i in range(0,3):\n", " print '%.f'%xnew[i]" ] } ], "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 }