Chapter 31 : Numerical Methods

Example 31.1 Page no 486

In [1]:
from __future__ import division
from numpy import mat
print "Example 31.1 Page no 486\n\n "
#set of linear algebric equation using gauss elimination
A=mat([[3,-2,1],[1,4,-2],[2,-3,-4]])#matrix A
B=mat([[7],[21],[9]])#matrix B
X=(A**-1)*B
print "\n X= \n",(X)#
X1=X[0]#value of X1
X2=X[1]#value of X2
X3=X[2]#value of X3
print "\nX1=%.2f\nX2=%.2f \nX3=%.2f"%(X1,X2,X3)#
Example 31.1 Page no 486

 

 X= 
[[ 5.]
 [ 3.]
 [-2.]]

X1=5.00
X2=3.00 
X3=-2.00

Example 31.2 Page no 492

In [2]:
from __future__ import division
print "Example 31.2 Page no 492\n\n"
#the vapor pressure p' for a new synthetic chemical at a given temperature
t1=1100#assume intial actual temperature,k
T1=t1*1e-3#temperature,k
print "\n T1=%.2f k"%(T1)#
f1=T1**3 -2*T1**2 + 2*T1 -1#function of T,f(T)
f_d1=3*T1**2 -4*T1 + 2#derivative of f(T)
#using newton rapson formula to estimate T2
T2=T1 -(f1/f_d1)#temperature T2
print "\n T2=%.2f k"%(T2)#
f2=T2**3 -2*T2**2 + 2*T2 -1
f_d2=3*T2**2 -4*T2 + 2
T3=T2 -(f2/f_d2)#temperature T3
print "\n T3=%.2f k"%(T3)#
#finally the best estimate is T3,t=1.000095
Example 31.2 Page no 492



 T1=1.10 k

 T2=1.01 k

 T3=1.00 k

Example 31.3 Page no 493

In [3]:
from __future__ import division
from sympy import symbols, solve
print "Example 31.3 Page no 493\n\n"
#friction factor for smooth tubes can be approximated by
#f = 0.079*R_e**(-1/4),if 2000< R_e<2e-5
# average velocity in the system ,involving the flow of water at 60 deg F is given by 
#v =sqrt(2180/(213.4R_e**(-1/4) + 10), flow of water at 60 deg F
#R_e=12168v,putting this value and by simplifying we get
v=symbols('v')
f=213.5*v**2 +105.03*v- 22896.08*v
#df=derivat(213.5*v**2 +105.03*v- 22896.08*v)
df=- 22791.05 + 427*v 
v1=5
f1=213.5*v1**2 +105.03*v1- 22896.08*v1# value of f at v=5
df1=- 22791.05 + 427*v1#value of df at v=5
v2=v1-(f1/df1)
#by iteration we get values of v3,v4,v5,v6
#at v6 result converges
v6=10.09
print "\n v6=%.2f ft/s "%(v6)#
Example 31.3 Page no 493



 v6=10.09 ft/s 

Example 31.4 Page no 497

In [4]:
from __future__ import division
from sympy import mpmath
print "Example 31.4 Page no 497\n\n"
#integration
I=mpmath.quad(lambda x:(1-0.4*x**2)/((1-x)*(1-0.4*x)-1.19*x**2),[0,0.468])
print "\n I=%.2f "%(I)#
Example 31.4 Page no 497



 I=0.90