Python programming exam

profilesvenkata
Assigment05.doc.docx

SHIVAKIRAN RAO PAMULAPARTHI VENKATA

ID: 244705

Assignment - 5

Exercise 5.1

Code :

def m(n): return 1 / sqrt(2 X pi) * exp(-0.5 * x * x) Z = 40 dx = 8 / (n - 1) mlist = [-4 + i * dx] nlist = [m(n)] plot(mlist, nlist) xlabel('x') ylabel('h') show()

Output:

Exercise: 5.2:-

def l(m):
    return 1 / sqrt(2 x pi) * exp(-0.5 * x * x)

xlist = linspace(-5, 5, 45)
hlist = zeros(len(xlist))

for i in xrange(len(xlist)):
    hlist[i] = h(xlist[i])

plot(xlist, hlist)
xlabel('l')
ylabel('m')
show()

Console:

Exercise 5:3.

def z(x):
    return 1 / sqrt(2 * pi) * exp(-0.5 * x * x)

xlist = linspace(-3, 3, 45)
hlist = h(xlist)

plot(xlist, zlist)
xlabel('x')
ylabel('z')
show()

Console:

Exercise 5:5

from numpy imp meshgrid cos, sin, zeros, exp, array
x = array([0, 5])
t = array([1, 4.5])

ylist = zeros([3, 3])


def y(x, t):
    return cos(sin(x) + exp(1 / t))


print '\n'

xv, tv = meshgrid(x, t)
ylist_check = y(xv, tv)

print xlist_check


    

Exercise 5.8.

v0 = 10
g = 9.81

tlist = np.linspace(0, 2 * v0 / g, 100)


def y(t):
    return v0 * t - 0.5 * g * t ** 2

plot(tlist, y(tlist),
     xlabel=' (s)',
     ylabel=' (m)',
     axis=[tlist[0], tlist[-1], y(tlist).min(), y(tlist).max()]
     )

raw_input()

console:

Exercise 5.9.

g = 9.81


def y(t, v0):
    return v0 * t - 0.5 * g * t ** 2

ax = plt.subplot(111)
plt.hold(1)


def plot_trajectory(v0):
    tlist = np.linspace(0, 2 * v0 / g, 100)
    ax.plot(tlist, y(tlist, v0))

ax.set_xlabel('(s)')
ax.set_ylabel(' (m)')

velocities = np.array(sys.argv[1:], dtype=np.float)

for x in xrange(len(vel)):
    plot_trajectory(vel[x])

output:-

Exercise 5.10.

Gravity g = 9.81


def y(t, v0):
    return v0 * t - 0.5 * g * t ** 2

ax = plt.subplot(111)
plt.hold(1)


def plot_trajectory(v0):
    tlist = np.linspace(0, 2 * v0 / g, 100)
    ax.plot(tlist, y(tlist, v0))

ax.set_xlabel('time (s)')
ax.set_ylabel('height (m)')

velocities = np.array(sys.argv[1:], dtype=np.float)

for x in xrange(len(velocities)):
    plot_trajectory(velocities[x])



vmax = velocities.max()

max_yval = y(np.linspace(0, 2 * vmax / g, 100), vmax).max()

ax.set_xlim([0, 2 * velocities.max() / g])
ax.set_ylim([0, max_yval + 0.1])
plt.legend(['v0 = %g' % v0 for v0 in velocities])
plt.show()

Console:

Exercise 5.11.

def exact_conv(F):
    return 5 / 9. * (F - 32)


def approx_conv(F):
    return 0.5 * (F - 30)

Flist = np.linspace(-20, 120, 141)

ax = plt.subplot(111)

ax.plot(Flist, exact_conv(Flist))
ax.plot(Flist, approx_conv(Flist))
ax.set_xlabel('Farenheit')
ax.set_ylabel('Celsius')

plt.show()

Console :

Exercise 5.15

Code :

def wave_packet(z, t):
    return np.exp(-(m - 3 * t) ** 2) * np.sin(3 * np.pi * (z - t))

zlist = np.linspace(-5, 5, 10)
ylist = wave_packet(zlist, 0)

plt.plot(zlist, ylist)
plt.zlabel('x')
plt.llabel('amp')
plt.show()

Output: -