from numpy import zeros,mat
from numpy.linalg import solve
#Elliptic Equations
l = 15
h = 5
n = 1 + 15/5
f=zeros([4,4])
f[0,0:3]=100
f[0:3,0]=100
#At point 1 : f2 + f3 - 4f1 + 100 + 100 = 0
#At point 2 : f1 + f4 - 4f2 + 100 + 0 = 0
#At point 3 : f1 + f4 - 4f3 + 100 + 0 = 0
#At point 4 : f2 + f3 - 4f4 + 0 + 0 = 0
#
#Final Equations are
# -4f1 + f2 + f3 + 0 = -200
# f1 - 4f2 + 0 + f4 = -100
# f1 + 0 - 4f3 + f4 = -100
# 0 + f2 + f3 - 4f4 = 0
A = mat([[ -4, 1 ,1 ,0],[1, -4, 0 ,1],[1, 0 ,-4 ,1],[0, 1, 1, -4 ]])
B = mat([[-200],[-100],[-100],[0]])
C = solve(A,B)
print '\n The solution of the system is \n f1 = %d \n f2 = %d \n f3 = %d \n f4 = %d \n '%(C[0],C[1],C[2],C[3])
from numpy import zeros,mat
from numpy.linalg import solve
#Liebmann's Iterative method
f=zeros([4,4])
f[0,0:3]=100
f[0:3,0]=100
A=zeros([5,5])
A[0,0:5] = range(0,5)
for n in range(1,6):
for i in range(2,4):
for j in range(2,4):
if n == 1 and i == 2 and j == 2 :
f[i-1,j-1] = ( f[i,j] + f[i-2,j-2] + f[i-2,j] + f[i,j-2] )/4
else:
f[i,j] = ( f[i,j-1] + f[i-2,j-1] + f[i-1,j]+ f[i-1,j-2] )/4
A[1:5,n-1] = mat([f[1,1],f[1,2],f[2,1],f[2,2] ])
print 'First row of below matrix represents iteration number:\n',A
from numpy import zeros,mat
from numpy.linalg import solve
#Poisson's Equation
#D2f = 2*x**2 * y**2
# f = 0
# h = 1
#Point 1 : 0 + 0 + f2 + f3 - 4f1 = 2(1)**2 * 2**2
# f2 + f3 - 4f1 = 8
#Point 2 : 0 + 0 + f1 + f4 -4f2 = 2*(2)**2*2**2
# f1 - 4f2 = f4 = 32
#Point 3 : 0 + 0 + f1 + f4 - 4f4 = 2*(1**2)*1**2
# f1 -4f3 + f4 = 2
#Point 4 : 0 + 0 + f2 + f3 - 4f4 = 2* 2**2 * 1**2
# f2 + f3 - 4f4 = 8
#Rearranging the equations
# -4f1 + f2 + f3 = 8
# f1 - 4f2 + f4 = 32
# f1 - 4f3 + f4 = 2
# f2 + f3 - 4f4 = 8
A = mat([[ -4, 1, 1, 0],[1, -4 ,0 ,1],[1, 0, -4, 1],[0, 1, 1, -4]])
B = mat([[ 8],[32],[2],[8 ]])
C = solve(A,B)
print 'The solution is \n f1 = %f \n f2 = %f \n f3 = %f \n f4 = %f \n '%( C[0],C[1],C[2],C[3])
#Gauss-Seidel Iteration
f2 = 0
f3 = 0
for i in range(1,5):
f1 = (f2 + f3 - 8)/4
f4 = f1
f2 = (f1 + f4 -32)/4
f3 = (f1 + f4 - 2)/4
print '\n Iteration %d\n f1 = %f, f2 = %f, f3 = %f, f4 = %f\n'%(i,f1,f2,f3,f4)
from numpy import zeros,mat
#Initial Value Problems
h = 1 #
k = 2 #
tau = h**2/(2*k)
f=zeros([7,5])
for i in range(1,5):
f[0,i] = 50*( 4 - (i) )
f[0:7,0] = 0
f[0:7,4] = 0
for j in range(0,6):
for i in range(1,4):
f[j+1,i] = ( f[j,i-1] + f[j,i+1] )/2
print 'The final results are \n',f
from numpy import zeros,mat,linalg
#Crank-Nicholson Implicit Method
h = 1 #
k = 2 #
tau = h**2/(2*k)
f=zeros([5,5])
for i in range(1,4):
f[0,i] = 50*( 4 - (i) )
f[0:5,0] = 0
f[0:5,4] = 0
A = mat([[4, -1, 0 ],[-1 , 4 , -1 ],[0 , -1 , 4]])
B=zeros([3,1])
for j in range(0,4):
for i in range(1,4):
B[i-1,0] = f[j,i-1] + f[j,i+1]
C = linalg.solve(A,B)
f[j+1,1] = C[0]
f[j+1,2] = C[1]
f[j+1,3] = C[2]
print 'The final solution using crank nicholson implicit method is :\n',f
from __future__ import division
from numpy import sqrt
#Hyperbolic Equations
h = 1
Tbyp = 4
tau = sqrt(h**2 /4)
r = int(1+(2.5 - 0)/tau)
c = int(1+(5 - 0)/h)
f=zeros([6,6])
for i in range(1,int(c)-1):
f[0,i] = (i)*(5 - (i) )
f[0:r-1,0] = 0
f[0:r-1,c-1] = 0
g=[]
for i in range(1,c-1):
g.append(0)
f[1,i] = (f[0,i+1] + f[0,i-1])/2 + tau*g[i-1]
for j in range(1,r-1):
for i in range(1,c-1):
f[j+1,i] = -f[j-1,i] + f[j,i+1] + f[j,i-1]
print 'The values estimated are :\n',f