CompSciHW5

From Predictive Chemistry
Jump to: navigation, search

Homework 5 - Due Friday, March 4, 2016

1) Use numpy and pyplot to plot the following functions:
    (remember to start by choosing an appropriate range for x)
 a) f(x) = sin(x) cos(2x)
 b) g(x) = x (if x is in 0,1), 2-x (if x is in 1,2), 0 otherwise
 c) h(x) = exp(-x*x/(2*sigma)), for sigma = 1, 2, and 3
 d) w(x) = g(x) - 4*g(x-2.0)
 e) u(x, y) = sin(x)*cos(3*y) - cos(2*x)*sin(y) [Hint: use pylab.imshow()]

2) Name the type of the following functions:
   (remember that *a means a list is input and **a means a dictionary)
 a) def joinwith(space, *a):
        return space.join(a)
 b) def map_on_n(f, n):
        return [f(i) for i in range(n)]
 c) def get_elem(k="default", **d):
        return d[k] + 2.0
 d) def chaos(*a, **b):
        return random.random()*random.random()
 e) def sliceof(st, stop, step, *a):
        return a[st:stop:step]

Hint: Try defining x as an array of x-values, and f,g,h, etc. as a python function with type float -> float. Then plot(x, f(x)) or calculate the values using vals = map(f, x) The first one tricks the function into the type (array -> array), and the second computes 1 float at a time.

<source lang="python"> import numpy as np from pylab import plot, show, imshow N = 100 x = np.linspace(-3.0, 3.0, N) # plotting 1D functions y = np.linspace(-2.0, 2.0, N)

X, Y = np.meshgrid(x, y) # plotting 2D functions (1.e) </source>