Homework 4

CHE 116: Numerical Methods and Statistics

2/8/2018


1. String Formatting (4 Points)

Answer these in Python

  1. [1 point] Print $\pi$ to 4 digits of precision. Use pi from the math module.
  2. [2 points] Print the square root of the first 5 integers starting from 1 with 4 digits of precision. Print the terms with one per line and have them all take up exactly 5 spaces. 2 Bonus points if you use a for loop
  3. [1 point] Show how to print the same variable twice using the variable index part of the : operator in a string.}

2. Representing Numbers (5 Points)

Answers these symbolically

  1. [1 point] What is the mantissa and what is the exponent of $0.3422 \times 10^{-4}$.
  2. [1 point] What is the largest representable number with 10 bits?
  3. [1 point] In a number system of base 8, how many numbers can be represented with 4 digits places?
  4. [2 points] How do we do boolean expressions with floating numbers? Is it valid to compare floating numbers at all?

3. Booleans (20 Points)

  1. [4 points] Write python code that will print 'greater than 10' when the variable z is greater than 10.
  2. [4 points] Write python code that will print 'special' if the variable z less than -25 but greater than -35.
  3. [2 points] Use the % operator in python, which computes the remainder of the left number after being divided by the right number, to compute the remainder of 12 divided by 5.
  4. [4 points] Use the % operator in a boolean expression to print 'even' if the variable z is even.
  5. [6 points] Write code that prints out if the variable z is positive or negative, even or odd, and greater in magnitude than 10.

4. Lists and Slicing (11 Points)

Answers these questions in Python. Print your result

  1. [2 points] Create a list of all positive even integers less than 20 using the range function.
  2. [1 point] Using the list created above and a slice, print out the third-largest even integer less than 20.
  3. [2 points] Print out the first 3 integers from your list in question 4.1
  4. [2 points] Create a list of the integers 1 to 10 and then using the assignment operator (=), override the last element to be 100.
  5. [4 points] Create a list of the integers 1 to 4 and then use a for loop print out $2^i$, where $i$ is the elements in your list.

5. Numpy (12 Points)

Answers these questions in Python

When specifying an interval, parenthesis indicate an exclusive end-point and brackets indicate an inclusive end-point. So $\left(0,1\right)$ means from 0 to 1, not including 0 or 1. $\left[0, 1\right)$ means from 0 to 1 including 0 but not including 1.

  1. [2 points] Create an array of numbers that goes from $\left[1, 10\right)$ in increments of 0.25 using the arange function.
  2. [2 points] Create an array of numbers that goes from $\left[1, 10\right)$ in increments of 0.25 using the linspace function.
  3. [2 points] Create an array of 21 numbers that go from $\left[0,1\right]$ using the arange function.
  4. [2 points] Create an array of 21 numbers that go from $\left[0,1\right]$ using the linspace function.
  5. [4 points] Using an array, print out the first 8 powers of 3 (e.g., $3^0$, $3^1$, $3^2$, etc)

6. Plotting (16 Points)

Answer these in questions in Python. Label your axes as x-axis and y-axis.

  1. [4 points] Make a plot of the cosine from $-pi$ to $0$.

  2. [4 points] We will see that the equation $e^{-x^2}$ is important in a few weeks. Make a plot of it from -2, to 2.

  3. [8 pints] Plot $x$, $x^2$, and $x^3$ from -1 to 1. Label your lines with a legend, add a title, use the seaborn-darkgrid style, and set the figure size as $4\times3$.