Assigment Python 2
Assigment Python 2
Amrita Bhattacharya
Listing 1: locate
locate numpy
locate scipy
Add the following in your preamble (if required), depending upon your search result to the path of
the libraries;
Listing 2: path
#!/usr/bin/python
#!/usr/share/pyshared/
#!/usr/share/pyshared/scipy
Listing 3: Import
1.2 Plotting
Hereforth, we will use the in built library of python called matplotlib for plotting.
1
Listing 4: harmonics
def func_duo(x):
return np.sin(x) , np.cos(x)
sin(x)
2 cos(x)
harmonics(x)
0
y
−1
−2
−3 −2 −1 0 1 2 3
x
2
Listing 5: polyfit
data
p_1
3.0
p_2
p_3
2.5
2.0
y
1.5
1.0
0.5
0.0
0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0
x
3
1.3 Numerical Differentiation
Listing 6: Numerical
x = np.linspace(-2*np.pi,2*np.pi)
plt.plot(x,func(x)[1])
plt.xlabel("x")
plt.ylabel("y")
plt.plot(x,nd_func(x)[1])
plt.legend(('sin(x)', 'cos(x)'))
plt.savefig('num_diff_harmonics.eps')
plt.show()
4
150
125
100
75
50
y
25
−25
fn(x)
−50 d_fn(x)
−6 −4 −2 0 2 4 6
x
1.00
0.75
0.50
0.25
0.00
y
−0.25
−0.50
−0.75
sin(x)
−1.00 cos(x)
−6 −4 −2 0 2 4 6
x
5
1.4 Symbolic Differentiation
Listing 7: Symbolic
x = sympy.symbols('x')
fn1, dfn1, fn2, dfn2 = diff_func(x)
x = np.linspace(-6,6)
plt.plot(x,fx1)
plt.plot(x,diffx1)
plt.xlabel("x")
plt.ylabel("y")
plt.plot(x,fx2)
plt.plot(x,diffx2)
plt.legend(('fx1', 'dfx1', 'fx2', 'dfx2'))
plt.savefig('sym_diff.png')
plt.show()
6
150 fx1
dfx1
125 fx2
dfx2
100
75
50
y
25
−25
−50
−6 −4 −2 0 2 4 6
x
2 Assignments
(i) Write a program that does the following:
(a) creates a function, f (x) = aex + be−x that takes the parameters a = 2, 4 and b = 3, 6
(b) plots the function for −6 < x < 6.
(c) fits the function with polynomials of the order two, three and four.
(ii) The Lennard-Jones (LJ) model is used to model the inter atomic force/ potential between two
atoms. It consists of a steep repulsive term, and smooth attractive term. The 12-6 LJ potential is
given by the following equation:
B A
V(r) = − (1)
r12 r6
Say,
A = 1.024 × 10−23 (J nm6 )
B = 1.582 × 10−26 (J nm12 )
The force between the atom is given by F(r) = − dVdr . At equilibrium separation, the potential between
the two atoms is minimum, which implies the force is equal to zero, i.e. F(r) = 0.
Write a program that does the following:
(a) plots the LJ potential V(r) as a function of r (in nm) varying in range 0.3 6 r 6 0.8.
(b) plots the force F(r) between the atoms as a function of r.
(c) calculates the equilibrium separation r0 between the atoms for which the force is minimum.
Hints: It is easier if you divide A and B by the Boltzmann’s constant, 1.381 × 10−23 JK−1 so as to
measure V(r) in units of K.