#standard five point formula
#example 9.1
#page 350
u2=5.0
u3=1.0
for i in range(0,3):
u1=(u2+u3+6.0)/4.0
u2=(u1/2.0)+(5.0/2.0)
u3=(u1/2.0)+(1.0/2.0)
print" the values are u1=%d\t u2=%d\t u3=%d\t\n\n" %(u1,u2,u3)
#solution of laplace equation by jacobi method,gauss-seidel method and SOR method
#example 9.2
#page 351
u1=0.25
u2=0.25
u3=0.5
u4=0.5 #initial values
print "jacobis iteration process\n\n"
print"u1\t u2\t u3\t u4\t \n\n"
print "%f\t %f\t %f\t %f\t \n" %(u1,u2,u3,u4)
for i in range(0,7):
u11=(0+u2+0+u4)/4
u22=(u1+0+0+u3)/4
u33=(1+u2+0+u4)/4
u44=(1+0+u3+u1)/4
u1=u11
u2=u22
u3=u33
u4=u44
print "%f\t %f\t %f\t %f\t \n" %(u11,u22,u33,u44)
print " gauss seidel process\n\n"
u1=0.25
u2=0.3125
u3=0.5625
u4=0.46875 #initial values
print "u1\t u2\t u3\t u4\t \n\n"
print "%f\t %f\t %f\t %f\t \n" %(u1,u2,u3,u4)
for i in range(0,4):
u1=(0.0+u2+0.0+u4)/4.0
u2=(u1+0.0+0.0+u3)/4.0
u3=(1.0+u2+0.0+u4)/4.0
u4=(1.0+0.0+u3+u1)/4.0
print "%f\t %f\t %f\t %f\t \n" %(u1,u2,u3,u4)
#poisson equation
#exaample 9.4
#page 354
u2=0.0
u4=0.0
print " u1\t u2\t u3\t u4\t\n\n"
for i in range(0,6):
u1=(u2/2.0)+30.0
u2=(u1+u4+150.0)/4.0
u4=(u2/2.0)+45.0
print "%0.2f\t %0.2f\t %0.2f\t %0.2f\n" %(u1,u2,u2,u4)
print " from last two iterates we conclude u1=67 u2=75 u3=75 u4=83\n"
#bender-schmidt formula
#example 9.6
#page 362
def f(x):
return (4*x)-(x*x)
#u=[f(0),f(1),f(2),f(3),f(4)]
u1=f(0);u2=f(1);u3=f(2);u4=f(3);u5=f(4);
u11=(u1+u3)/2
u12=(u2+u4)/2
u13=(u3+u5)/2
print "u11=%0.2f\t u12=%0.2f\t u13=%0.2f\t \n" %(u11,u12,u13)
u21=(u1+u12)/2.0
u22=(u11+u13)/2.0
u23=(u12+0)/2.0
print "u21=%0.2f\t u22=%0.2f\t u23=%0.2f\t \n" %(u21,u22,u23)
u31=(u1+u22)/2.0
u32=(u21+u23)/2.0
u33=(u22+u1)/2.0
print "u31=%0.2f\t u32=%0.2f\t u33=%0.2f\t \n" % (u31,u32,u33)
u41=(u1+u32)/2.0
u42=(u31+u33)/2.0
u43=(u32+u1)/2.0
print "u41=%0.2f\t u42=%0.2f\t u43=%0.2f\t \n" % (u41,u42,u43)
u51=(u1+u42)/2.0
u52=(u41+u43)/2.0
u53=(u42+u1)/2.0
print "u51=%0.2f\t u52=%0.2f\t u53=%0.2f\t \n" % (u51,u52,u53)
#bender-schimdt's formula and crank-nicolson formula
#example 9.7
#page 363
#bender -schimdt's formula
import math
from numpy import matrix
z=math.pi
def f(x,t):
return math.exp(z*z*t*-1)*sin(z*x)
#u=[f(0,0),f(0.2,0),f(0.4,0),f(0.6,0),f(0.8,0),f(1,0)]
u1=f(0,0)
u2=f(0.2,0)
u3=f(0.4,0)
u4=f(0.6,0)
u5=f(0.8,0)
u6=f(1.0,0)
u11=u3/2
u12=(u2+u4)/2
u13=u12
u14=u11
print "u11=%f\t u12=%f\t u13=%f\t u14=%f\n\n" % (u11,u12,u13,u14)
u21=u12/2
u22=(u12+u14)/2
u23=u22
u24=u21
print "u21=%f\t u22=%f\t u23=%f\t u24=%f\n\n" % (u21,u22,u23,u24)
print "the error in the solution is: %f\n\n" % (math.fabs(u22-f(0.6,0.04)))
#crank-nicolson formula
#by putting i=1,2,3,4 we obtain four equation
A=matrix([[4, -1, 0, 0] ,[-1, 4, -1, 0],[0, -1, 4, -1],[0, 0, -1, 4]])
C=matrix([[0.9510],[1.5388],[1.5388],[0.9510]])
X=A.I*C
print "u00=%f\t u10=%f\t u20=%f\t u30=%f\t\n\n" %(X[0][0],X[1][0],X[2][0],X[3][0])
print "the error in the solution is: %f\n\n" %(abs(X[1][0]-f(0.6,0.04)))
#heat equation using crank-nicolson method
#example 9.8
#page 364
from numpy import matrix
import math
z=0.01878
#h=1/2 l=1/8,i=1
u01=0.0
u21=1.0/8.0
u11=(u21+u01)/6.0
print " u11=%f\n\n" % (u11)
print "error is %f\n\n" % (math.fabs(u11-z))
#h=1/4,l=1/8,i=1,2,3
A=matrix([[-3.0 ,-1.0 ,0.0],[1.0,-3.0,1.0],[0.0,1.0,-3.0]])
C=matrix([[0.0],[0.0],[-0.125]])
#here we found inverese of A then we multipy it with C
X=A.I*C
print "u12=%f\n\n" % (X[1][0])
print "error is %f\n\n" %(math.fabs(X[1][0]-z))