Difference between revisions of "CompSciHW5"

From Predictive Chemistry
Jump to: navigation, search
(Created page with "Homework 6 - Due Friday, March 4, 2016 <pre> 1) Create a function that parses the following files: a) mat33 = """ 1.0 2.0 3.0 -2.0 0.0 1.0 1.0 1.1 1.2 """ b) csv = """ # ti...")
 
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
Homework 6 - Due Friday, March 4, 2016
+
Homework 5 - Due Friday, March 4, 2016
 
<pre>
 
<pre>
1) Create a function that parses the following files:
+
1) Use numpy and pyplot to plot the following functions:
a) mat33 = """
+
(remember to start by choosing an appropriate range for x)
1.0 2.0 3.0
+
a) f(x) = sin(x) cos(2x)
-2.0 0.0 1.0
+
b) g(x) = x (if x is in 0,1), 2-x (if x is in 1,2), 0 otherwise
1.0 1.1 1.2
+
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()]
   
b) csv = """
 
  +
2) Name the type of the following functions:
# time temperature sea_level
 
  +
(remember that *a means a list is input and **a means a dictionary)
0.0, 300.0, 44.5
 
  +
a) def joinwith(space, *a):
1.5, 299.5, 44.0
 
  +
return space.join(a)
3.0, 299.8, 44.2
 
  +
b) def map_on_n(f, n):
4.5, 301.0, 43.9
 
  +
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]
  +
</pre>
   
c) name_val = """
 
  +
Hint: Try defining x as an array of x-values, and f,g,h, etc. as a python function with type float -> float.
seed = 1553
 
  +
Then plot(x, f(x))
name = "test name"
 
  +
or calculate the values using vals = map(f, x)
trials = 10
 
  +
The first one tricks the function into the type (array -> array), and the second computes 1 float at a time.
tolerance = 1e-8
 
"""
 
   
d) group_data = """
 
  +
<source lang="python">
[Group 1]
 
  +
import numpy as np
1 2 3 4 5 6
 
  +
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)
   
[Group 2]
 
  +
X, Y = np.meshgrid(x, y) # plotting 2D functions (1.e)
4 5 11 14 1
 
  +
</source>
 
[Group 3]
 
7 8 9 10 11
 
"""
 
 
2) Write code to carry out matrix multiplication manually
 
and test it against the result of numpy.dot.
 
 
3) Write code to multiply every column of matrix, A,
 
with every other column. Use this to find the set of angles
 
between the column vectors.
 
</pre>
 

Latest revision as of 15:47, 4 March 2016

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>