You are on page 1of 10

Numerical methods: Assignment #2

Due on Fri, February 13, 2015

P. Ajith 04:30pm

Gaurav Kumar Tiwari


13/02/2015

Gaurav Kumar Tiwari

Numerical methods (P. Ajith 04:30pm): Assignment #2

Contents
Problem 1

Problem 2

Page 2 of 10

Gaurav Kumar Tiwari

Numerical methods (P. Ajith 04:30pm): Assignment #2

Problem 1
Listing 1 shows a My python code.
Listing 1: My Pythone code for given problem
import numpy as np
import matplotlib.pyplot as plt
import time
from scipy import integrate
5

##To calculate the numerical integration of analytic function ##

10

15

20

25

30

35

40

45

t_initial=time.time()
#===============================================================================
def f(x):
return(np.exp(-x**2))
def mid_point(a,b,h):
x_1=np.arange(a,b,h)
z=np.zeros(x_1.shape,np.float)
z[0:]=f((x_1[0:])+0.5*h )
a=h
z=a*z
y=np.zeros(z.shape,np.float)
y[0]=z[0]
for i in range(len(x_1)-1):
y[i+1]=y[i]+z[i+1]
return(y)
def simp(a,b,h):
x_1=np.arange(a,b,h)
z=np.zeros(x_1.shape,np.float)
x0=x_1[0:]
x1=x_1[0:]+h
x2=x_1[0:]+h+h
z[0:]=( f(x0)+f(x2)+4*f(x1) )/(6/h)
y=np.zeros(z.shape,np.float)
y[0]=z[0]
for i in range(len(x_1)-1):
y[i+1]=y[i]+z[i+1]
return(y)
def trap(a,b,h):
x_1=np.arange(a,b,h)
z=np.zeros(x_1.shape,np.float)
z[0:]=(h/2)*( f(x_1[0:])+f(x_1[0:]+h) )
y=np.zeros(z.shape,np.float)
y[0]=z[0]
for i in range(len(x_1)-1):
y[i+1]=y[i]+z[i+1]
return(y)
def order(a,b,c):

Problem 1 continued on next page. . .

Page 3 of 10

Gaurav Kumar Tiwari

50

Numerical methods (P. Ajith 04:30pm): Assignment #2

Problem 1 (continued)

n=np.zeros(c.shape,np.float)
n=np.abs((c-b)/(b-a))
return(np.log(n)/np.log(2));

#======================================================================#
55

60

65

70

#h=np.linspace(10**(-1),10**(-3),3)
h1=0.05
h=[h1,2*h1,4*h1]
x_1=np.arange(-5.0,5.0,h[0])
m_1=mid_point(-5.0,5.0,h[0])
s_1=simp(-5.0,5.0,h[0])
t_1=trap(-5.0,5.0,h[0])
x_2=np.arange(-5.0,5.0,h[1])
m_2=mid_point(-5.0,5.0,h[1])
s_2=simp(-5.0,5.0,h[1])
t_2=trap(-5.0,5.0,h[1])
x_3=np.arange(-5.0,5.0,h[2])
m_3=mid_point(-5.0,5.0,h[2])
s_3=simp(-5.0,5.0,h[2])
t_3=trap(-5.0,5.0,h[2])
t_final=time.time()-t_initial

75

#======================================================================
#=======================================================================

80

85

90

#=========================================Plot==============================
plt.figure(1)
# the first figure
for i in range(len(h)):
plt.subplot(3,1,i+1)
i f (i==0):
plt.plot(x_1[::4],m_1[::4],x_2[::2],m_2[::2],x_3,m_3)
plt.ylabel(" Numerical Derivative")
plt.title(For mid point methode $ )
i f (i==1):
plt.plot(x_1[::4],s_1[::4],x_2[::2],s_2[::2],x_3,s_3)
plt.title(For mid point methode $ )
i f (i==2):
plt.plot(x_1[::4],t_1[::4],x_2[::2],t_2[::2],x_3,t_3)
plt.title(For mid point methode $ )

95

100

#========================================================================
#print(len(x_3),len(s_2[::2]))
#=======================================================================
#Order#
#=======================================================================
plt.figure(2)

Problem 1 continued on next page. . .

Page 4 of 10

Gaurav Kumar Tiwari

105

110

Numerical methods (P. Ajith 04:30pm): Assignment #2

Problem 1 (continued)

plt.title(Order of Convergence)
for i in range(len(h)):
plt.subplot(3,1,i+1)
i f (i==0):
plt.plot(x_3,order(m_1[::4],m_2[::2],m_3),ro)
plt.ylabel("mid point method ")
i f (i==1):
plt.plot(x_3,order(s_1[::4],s_2[::2],s_3),ko)
plt.ylabel(" Simpson 1/3 method ")
i f (i==2):
plt.plot(x_3,order(t_1[::4],t_2[::2],t_3))
plt.ylabel("Trapezoidal rule")

115

120

125

130

135

140

145

150

#=======================================================================
#Error#
#=======================================================================
plt.figure(3)
plt.title(Error)
for i in range(len(h)):
plt.subplot(3,1,i+1)
i f (i==0):
plt.plot(x_3,np.abs(m_1[::4]-m_3),x_2,np.abs(m_2-m_1[::2]))
plt.ylabel("mid point method ")
i f (i==1):
plt.plot(x_3,np.abs(s_1[::4]-s_3),x_2,np.abs(s_2-s_1[::2]))
plt.ylabel(" Simpson 1/3 method ")
i f (i==2):
plt.plot(x_3,np.abs(t_1[::4]-t_3),ro,x_2,np.abs(t_2-t_1[::2]))
plt.ylabel("Trapezoidal rule")

#=======================================================================
#Gravtional Energy
#=======================================================================
t,hp,hn= np.loadtxt("Richardson.dat", usecols=(0,1,2),comments=#, unpack=True)
#=========================================================================
#Function for Energy
#========================================================================
def c_d(x,y):
dy=np.zeros(y.shape,np.float)
dy[0]=(y[1]-y[0])/(x[1]-x[0])
dy[-1]=(y[-1]-y[-2])/(x[-1]-x[-2])
dy[1:-1]=(y[2:]-y[0:-2])/(x[2:]-x[0:-2])
return(dy);

def simp_1(t,y,h):
z=np.zeros(t.shape,np.float)
z[0:-2]=(h/6)*( y[0:-2]+y[2:]+4*y[1:-1] )
z[-2]=(h/2)*(y[-2]+y[-1])
z[-1]=(h/2)*(y[-2]+y[-1])

Problem 1 continued on next page. . .

Page 5 of 10

Gaurav Kumar Tiwari

155

Numerical methods (P. Ajith 04:30pm): Assignment #2

Problem 1 (continued)

w=np.zeros(z.shape,np.float)
w[0]=z[0]
for i in range(len(t)-1):
w[i+1]=w[i]+z[i+1]
return(w)

160

165

def trap_1(t,y,h):
z=np.zeros(t.shape,np.float)
z[0:-1]=(h/2)*( y[0:-1]+y[1:] )
z[-1]=z[-2]
w=np.zeros(z.shape,np.float)
w[0]=z[0]
for i in range(len(t)-1):
w[i+1]=w[i]+z[i+1]
return(w)

170

175

def energy_density(a,b):
z=np.zeros(a.shape,np.float)
z=a*a+b*b
return(z);
#z_1=integrate.cumtrapz(,t,initial=0)

180

#========================================================================
#Raw Caluclation for Energy
#=======================================================================
185

190

n=1
n2=2*n
n4=4*n
hp_dt_1=c_d(t,hp)
hp_dt_2=hp_dt_1[::n2]
hp_dt_3=hp_dt_1[::n4]

195

hn_dt_1=c_d(t,hn)
hn_dt_2=hn_dt_1[::n2]
hn_dt_3=hn_dt_1[::n4]

200

energy_density_1=energy_density(hp_dt_1,hn_dt_1)
energy_density_2=energy_density(hp_dt_2,hn_dt_2)
energy_density_3=energy_density(hp_dt_3,hn_dt_3)

205

s1_1=simp_1(t,energy_density_1,t[n]-t[0])
t1_1=trap_1(t,energy_density_1,t[n]-t[0])
plt.figure(10)
plt.plot(t,s1_1)
#t1_1=integrate.cumtrapz(energy_density_1,t,initial=0)
s1_2=simp_1(t[::n2],energy_density_2,t[n2]-t[0])

Problem 1 continued on next page. . .

Page 6 of 10

Gaurav Kumar Tiwari

210

Numerical methods (P. Ajith 04:30pm): Assignment #2

Problem 1 (continued)

t1_2=trap_1(t[::n2],energy_density_2,t[n2]-t[0])
print(s1_1[-1])
#t1_2=integrate.cumtrapz(energy_density_2,t[::n2],initial=0)
s1_3=simp_1(t[::n4],energy_density_3,t[n4]-t[0])
t1_3=trap_1(t[::n4],energy_density_3,t[n4]-t[0])
#t1_3=integrate.cumtrapz(energy_density_3,t[::n4],initial=0)

215

220

#====================================================================
def n1(x,y,z):
order=np.zeros(z.shape,np.float)
order=np.log(np.abs((z-y)/(y-x) ))/np.log(2)
return(order);
a1,b1,c1=s1_1[::n4],s1_2[::2],s1_3
print(len(a1),len(b1),len(c1))

225

230

235

240

O_1=-n1(a1,b1,c1)
a2,b2,c2=t1_1[::n4],t1_2[::2],t1_3
O_2=-n1(a2,b2,c2)
plt.figure(5)
plt.plot(t[::n4],O_1)
plt.plot(t[::n4],O_2)
#====================================================================
"""plt.figure(4)
plt.title(Order of Convergence)
for i in range(2):
plt.subplot(2,1,i+1)
if(i==0):
plt.plot(t[::4],O_1)
plt.ylabel(" Simpson 1/3 method ")
if(i==10):
plt.plot(t[::4],O_2)
plt.ylabel("Trapezoidal rule")
"""

245

250

#=====================================================================
#=====================================================================
#print(len((s1_2[::2])),len(t[::4*n]))
plt.show()

Problem 1 continued on next page. . .

Page 7 of 10

Gaurav Kumar Tiwari

Numerical methods (P. Ajith 04:30pm): Assignment #2

Problem 1 continued on next page. . .

Problem 1 (continued)

Page 8 of 10

Gaurav Kumar Tiwari

Numerical methods (P. Ajith 04:30pm): Assignment #2

Problem 1 (continued)

Problem 2

Problem 2 continued on next page. . .

Page 9 of 10

Gaurav Kumar Tiwari

Numerical methods (P. Ajith 04:30pm): Assignment #2

Problem 2 (continued)

Page 10 of 10

You might also like