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.}

In [1]:
#1.1
import math
print('{:.4}'.format(math.pi))


3.142

In [2]:
#1.2 - no for loop
print('{:5.4}'.format(math.sqrt(1)))
print('{:5.4}'.format(math.sqrt(2)))
print('{:5.4}'.format(math.sqrt(3)))
print('{:5.4}'.format(math.sqrt(4)))
print('{:5.4}'.format(math.sqrt(5)))


  1.0
1.414
1.732
  2.0
2.236

In [3]:
#1.2 - for loop
for i in range(1, 6):
    print('{:5.4}'.format(math.sqrt(i)))


  1.0
1.414
1.732
  2.0
2.236

In [4]:
print('{0:} {0:}'.format('test'))


test test

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?

2.1

0.3442

2.2

$$ 2^{10} - 1 = 1023 $$

2.3

$$ 8^4 = 4096 $$

2.4

Floating point comparisons are fine with inequalities (<, >), but not equalities (==)

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.

In [5]:
#3.1
z = -27
if z > 10:
    print('greater than 10')

In [6]:
#3.2
if z < -25 and z > -35:
    print('special')


special

In [7]:
#3.3
12 % 5


Out[7]:
2

In [8]:
#3.4
if z % 2 == 0:
    print('even')

In [9]:
#3.5
if z % 2 == 0:
    print('even')
else:
    print('odd')

if z > 0:
    print ('positive')
elif z < 0:
    print('negative')

if abs(z) > 10:
    print('greater in magnitude than 10')


odd
negative
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.

In [10]:
#4.1
x = list(range(2,20,2))
x


Out[10]:
[2, 4, 6, 8, 10, 12, 14, 16, 18]

In [11]:
#4.2
x[-3]


Out[11]:
14

In [12]:
#4.3
x[:3]


Out[12]:
[2, 4, 6]

In [13]:
#4.4
x = list(range(1,11))
x[-1] = 100
x


Out[13]:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 100]

In [14]:
#4.5
x = list(range(1,5))
for thing in x:
    print(2**thing)


2
4
8
16

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)

In [15]:
#5.1
import numpy as np
np.arange(1, 10, 0.25)


Out[15]:
array([ 1.  ,  1.25,  1.5 ,  1.75,  2.  ,  2.25,  2.5 ,  2.75,  3.  ,
        3.25,  3.5 ,  3.75,  4.  ,  4.25,  4.5 ,  4.75,  5.  ,  5.25,
        5.5 ,  5.75,  6.  ,  6.25,  6.5 ,  6.75,  7.  ,  7.25,  7.5 ,
        7.75,  8.  ,  8.25,  8.5 ,  8.75,  9.  ,  9.25,  9.5 ,  9.75])

In [16]:
#5.2
np.linspace(1,9.75,36)


Out[16]:
array([ 1.  ,  1.25,  1.5 ,  1.75,  2.  ,  2.25,  2.5 ,  2.75,  3.  ,
        3.25,  3.5 ,  3.75,  4.  ,  4.25,  4.5 ,  4.75,  5.  ,  5.25,
        5.5 ,  5.75,  6.  ,  6.25,  6.5 ,  6.75,  7.  ,  7.25,  7.5 ,
        7.75,  8.  ,  8.25,  8.5 ,  8.75,  9.  ,  9.25,  9.5 ,  9.75])

In [17]:
#5.3
np.arange(0,1.05, 0.05)


Out[17]:
array([ 0.  ,  0.05,  0.1 ,  0.15,  0.2 ,  0.25,  0.3 ,  0.35,  0.4 ,
        0.45,  0.5 ,  0.55,  0.6 ,  0.65,  0.7 ,  0.75,  0.8 ,  0.85,
        0.9 ,  0.95,  1.  ])

In [18]:
#5.4
np.linspace(0,1,21)


Out[18]:
array([ 0.  ,  0.05,  0.1 ,  0.15,  0.2 ,  0.25,  0.3 ,  0.35,  0.4 ,
        0.45,  0.5 ,  0.55,  0.6 ,  0.65,  0.7 ,  0.75,  0.8 ,  0.85,
        0.9 ,  0.95,  1.  ])

In [19]:
#5.5
x = np.arange(0,8)
print(3**x)


[   1    3    9   27   81  243  729 2187]

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$.


In [20]:
%matplotlib inline
import matplotlib.pyplot as plt

#6.1
x = np.linspace(-np.pi, 0, 100)
y = np.cos(x)
plt.plot(x,y)
plt.xlabel('x')
plt.ylabel('y')
plt.show()



In [21]:
#6.2
x = np.linspace(-2, 2, 100)
y = np.exp(-x**2)
plt.plot(x,y)
plt.xlabel('x')
plt.ylabel('y')
plt.show()



In [22]:
#6.3
plt.style.use('seaborn-darkgrid')
plt.figure(figsize=(4,3))
x = np.linspace(-1, 1, 100)
y1 = x
y2 = x**2
y3 = x**3

plt.plot(x,y1, label='x')
plt.plot(x,y2, label='x^2')
plt.plot(x,y3, label='x^3')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.title('Nice Title')
plt.show()



In [ ]: