You are on page 1of 7

Assignment 5: Numerical Integration

Aarav Ratra
Enrolment no. 21122002
August 31, 2023

1 Problem 1:
For a simple pendulum, with the motion not restricted to small amplitudes, the time period can
be expressed as

s
l dθ
Z θ0
T=4 √
2g 0 cosθ − cosθ0
(1)
where θ0 is the amplitude at t = 0. This integration can be converted into another form by using
sin θ/2
sin θ20 = γ and sin θ0 /2 = sin η

s
l dη
Z π/2
T=4 p
2g 0 1 − γ2 sin2 η
(2)
1. From Eq. (2), calculate the period of the simple pendulum using different types of integra-
tion methods and compare the results.
2. Take different values of the amplitude θ0 form small to large values (atleast 10 values) and
calculate the time periods.
3. Plot the graph between the amplitudes and time periods.
4. Find the physical significance of the results.

1.1 Method:
We can use the methods discussed in the previous assignment
1. Trapezoidal Rule
2. Simpson’s 1/3rd rule
We use the function definitions used last time for the former two, and use a similar approach for
Monte-Carlo.
We shall display the result for 10 values of theta0 , varying from values close to 0 to those close to
π/2.

1
We shall demonstrate the plot for variation in time period with respect to θ0 .

1.2 Program:

[140]: #Importing Necessary Libraries


from random import random
import numpy as np
from math import *
import matplotlib.pyplot as plt

1.2.1 Trapezoidal Rule and Simpson’s Rules

[141]: def trap(func,a,b,N):


x = np.linspace(a,b,N+1)
h = x[1]-x[0]
s = 0
for i in range(N):
s += (func(x[i])+func(x[i+1]))*h/2
return s

[142]: def simp(func,a,b,N):


x = np.linspace(a,b,N+1)
h = x[1]-x[0]
s = h/3 * (func(x[0]+func(x[N])))
for i in range(1,N):
s += h/3 * (4*(i%2) + 2*int(not (i%2) ))* func(x[i])
return s

Solution using Trapezoidal and Simpson’s Methods


[143]: def integrand(gamma,eta):
return 1/np.sqrt(1-gamma*gamma*eta*eta)

def TimePeriod(l,g,theta_0,integral_method): #I shall be using N=2000 for all of␣


,→the examples here.

gamma = np.sin(theta_0/2)

def func(eta):
return integrand(gamma,eta)

a = 0
b = np.pi/2

T = 4*sqrt(0.5*l/g)*integral_method(func,a,b,1000)

2
return T

[144]: #Solving for the following values and printing results: Simpson's and␣
,→Trapezoidal Method

g = 9.8
l = 2*9.8

Amps = [i*np.pi/32 for i in range(12)]


Simps = []
Traps = []

for thet in Amps:


Simps.append(TimePeriod(l,g,thet,simp))
Traps.append(TimePeriod(l,g,thet,trap))

print({'theta':thet,'Simpson Soln':Simps[-1],'Trapezoidal Soln':Traps[-1]})

{'theta': 0.0, 'Simpson Soln': 6.28109091207722, 'Trapezoidal Soln':


6.283185307179473}
{'theta': 0.09817477042468103, 'Simpson Soln': 6.2873248810076925, 'Trapezoidal
Soln': 6.2894229871359135}
{'theta': 0.19634954084936207, 'Simpson Soln': 6.306168661975222, 'Trapezoidal
Soln': 6.308277962456456}
{'theta': 0.2945243112740431, 'Simpson Soln': 6.338060662619357, 'Trapezoidal
Soln': 6.340188812434506}
{'theta': 0.39269908169872414, 'Simpson Soln': 6.383776803001226, 'Trapezoidal
Soln': 6.385931690917242}
{'theta': 0.4908738521234052, 'Simpson Soln': 6.444508546503775, 'Trapezoidal
Soln': 6.446698225709965}
{'theta': 0.5890486225480862, 'Simpson Soln': 6.5219966515179, 'Trapezoidal
Soln': 6.524228919469}
{'theta': 0.6872233929727672, 'Simpson Soln': 6.618757734641041, 'Trapezoidal
Soln': 6.621038733005602}
{'theta': 0.7853981633974483, 'Simpson Soln': 6.7384798091575, 'Trapezoidal
Soln': 6.7408096125288735}
{'theta': 0.8835729338221293, 'Simpson Soln': 6.886756255864752, 'Trapezoidal
Soln': 6.889113542710184}
{'theta': 0.9817477042468103, 'Simpson Soln': 7.0725905732426515, 'Trapezoidal
Soln': 7.07486380169225}
{'theta': 1.0799224746714913, 'Simpson Soln': 7.312243407574615, 'Trapezoidal
Soln': 7.31361721559676}

[145]: #Plotting for Simpson's and Trapezoidal Method.

#We need to find maximum permissible value of Theta_0 such that we do not␣
,→encounter sqrt error

3
#we need so

Amps_ = np.linspace(0,1.1,1000+1)
Simps = []
Traps = []

for thet in Amps_:


Simps.append(TimePeriod(l,g,thet,simp))
Traps.append(TimePeriod(l,g,thet,trap))

Simps = np.array(Simps)
Traps = np.array(Traps)

[146]: plt.plot(Amps_,Simps)
plt.plot(Amps_,Traps)

[146]: [<matplotlib.lines.Line2D at 0x7ff0da56b828>]

[147]: plt.plot(Amps_,Traps-Simps)

[147]: [<matplotlib.lines.Line2D at 0x7ff1cddde198>]

4
1.3 Results and Discussion
Both the methods give a similar result and the difference in results appears to be minimal. The
methods deviate initially but later they converge to a similar value.
It can be observed
q that the time period, infact, increases for larger values of amplitude θ0 , and it is
l
equal to 2π g for the limiting case of θ0 → 0.

2 Problem 2:
Consider a quantum harmonic oscillator in the first excited state described by the wave function
2
ψ( x ) = xe− x /2 . Normalize this wave function using the Monte Carlo integration method.

2.1 Method:
(x) We use Monte-Carlo’s method to find the area under the curve for the function |ψ( x )|2 .
However, since our range of x is from −∞ to ∞, we use the following result:

Z ∞
t2 + 1
Z 1
t
f ( x )dx = f( ) dt
−∞ −1 1 − t2 ( t2 − 1)2

where we substitute f ( x ) = |ψ( x )|2


We first have to find the maxima, so as to choose an appropritate rectangular region. We do that
by defining the integrand as a function of t and finidng the maximum element in the array. As thr
numerical methods have inaccuracies, we choose the ceil of the maxima.

5
2.2 Program:

[127]: def psi_x(x):


return x*np.exp(-x**2/2)

def psi_2(x):
return x**2 * exp(-x**2)

def f_t(t):
if np.abs(t) == 1:
val = 0
else:
t1 = (t*t+1)
t2 = (t*t-1)*(t*t-1)
arg = t/((-t*t+1))
val = psi_2(arg)*t1/t2
return val

mval = 2

[128]: def points(n):


points_ = []
for i in range(n):
x = -1+ 2*random()
y = 2*random()
xy = np.array([x,y])
points_.append(xy)
return points_

def point_in(xy):
return int(xy[1] <= f_t(xy[0]) )

def integral(n):
per = sum(point_in(i) for i in points(n))/n
integral_ = 4*per
return integral_

[139]: print('| Number of Points | Integral Value |')


print('| :----: | :----: |')
for i in range(2,7):
print('| 10$^',i,'$ |',integral(10**i),'|')

| Number of Points | Integral Value |


| :----: | :----: |
| 10$ˆ 2 $ | 0.8 |
| 10$ˆ 3 $ | 0.888 |
| 10$ˆ 4 $ | 0.8544 |

6
| 10$ˆ 5 $ | 0.87504 |
| 10$ˆ 6 $ | 0.886696 |

[149]: sqrt(np.pi)/2

[149]: 0.8862269254527579

2.3 Result and Discussion


Here is the result for different number of points:

Number of Points Integral Value


102 0.8
103 0.888
104 0.8544
105 0.87504
106 0.886696


π
Which is pretty close to the true value, which is supposed to be equal to 2 .

This proves that more the number of points, the more accurate our integral will be.

You might also like