Homework 3

CHE 116: Numerical Methods and Statistics

Prof. Andrew White

Version 1.1 (1/26/2015)


$\DeclareMathOperator{\P}{P}$ $\DeclareMathOperator{\E}{E}$

1. Python Math and Boolean (6 Points)

Answer the following problems in Python

  1. Is $3^{14}$ greater than $10^6$?

  2. Using the frexp function, show how the floating point number $0.4$ is represented and rebuild the number from its pieces. Use base 10

  3. Is $0.4 + 0.2$ exactly equal to $(4.0 + 2.0) / 10.0$ with floats? Why or why not?

  4. The next cell contains a joint probability mass function for $x$ and $y$. $x$ is the first number and $y$ is the second. You may access elements like this: P[0,2]. P[0,2] is the probability that $x=0$ and $y=2$. Demonstrate that the two random variables are not independent.

  5. Calculate the marginal $\P(x=2)$, where $x$ is the first index using the next cell's joint probability mass function.

  6. Calculate the conditional $\P(x=2 | y = 1)$, where $x$ is the first index using the next cell's joint probability mass function.


In [3]:
#This is loading the data for question 1.4 through 1.6
#Execute this cell and use new cells below. Do not answer in this cell!

import numpy as np
P = np.zeros( (3,3) )
P[0,0] = 0.
P[0,1] = 1. / 9
P[0,2] = 1. / 9.
P[1,0] = 1. / 3
P[1,1] = 0.
P[1,2] = 1. / 9
P[2,0] = 1. / 18
P[2,1] = 1. / 9
P[2,2] = 1. / 6

2. Slicing Lists (6 Points)

Using this sentence: "This pangram contains four As, one B, two Cs, one D, thirty Es, six Fs, five Gs, seven Hs, eleven Is, one J, one K, two Ls, two Ms, eighteen Ns, fifteen Os, two Ps, one Q, five Rs, twenty-seven Ss, eighteen Ts, two Us, seven Vs, eight Ws, two Xs, three Ys, & one Z", Create slices of the string to answer the following questions. Note that character/element mean the same thing, so that every element in the sentence is a character. Answer in Python

  1. What is the first characeter?
  2. What is the sentence without the last 10 characeter?
  3. What are the first 5 characeters?
  4. What is the first half of the sentence?
  5. What is the second half?
  6. What is every 8th character, starting from the 16th?

3. Loops (20 Points)

  1. [4 points] Using a for loop, compute $10!$.
  2. [4 points] Using a for loop compute $$\sum_{n=0}^{25}\frac{1}{n!}$$ What famous number is that?
  3. [8 points] Now that you know what famous number that is, how many terms are required in the sum before it is accurate to 5 decimal places? Your code should print out the number of terms. Think carefully about how to turn "accurate to 5 decimal places" into a python expression.

Numpy (6 Points)

  1. Create a numpy array containing a set of points between 5 and 10 spaced apart by 0.025. This should be done with a numpy function.
  2. Using a numpy array, compute: $$\sum_{i=1}^{100} \cos{i}$$ you should not use a for loop.
  3. Demonstrate 3 methods for creating a numpy array

In [ ]: