Homework 9

CHE 116: Numerical Methods and Statistics

3/29/2018


Homework Requirements:

  1. Write all equations in $\LaTeX$
  2. Simplify all expressions
  3. Put comments in your Python code
  4. Explain or show your work
  5. Follow the academic honesty guidelines in the syllabus

1. Slicing (20 Points)

Answer the following questions by slicing a numpy array containing the given array. Each answer should be done using slicing and summing multiple copies of the array. Do not use for loops or any other approach. See example answer. 4 Points each.

$$ [1, 4, 7, 11, 34, -3, 5, 7, 5, 2, 3, 13] $$
  1. $x_0 , x_2, x_4, \ldots $
  2. $x_0 + x_1, x_1 + x_2, \ldots$
  3. $x_1 - x_0, x_2 - x_1, \ldots$
  4. $ x_0 / x_1, x_2 / x_3, x_4 / x_5 \ldots$
  5. $ x_0 \cdot x_N, x_1 \cdot x_{N - 1}, \ldots x_{\frac{N}{2} - 1} \cdot x_{\frac{N}{2}}$

1.1 Answer


In [1]:
import numpy as np
x = np.array([1, 4, 7, 11, 34, -3, 5, 7, 5, 2, 3, 13])
###-------###
x[::2]


Out[1]:
array([ 1,  7, 34,  5,  5,  3])

2. Matrix Calculations (12 Points)

Use the two matrices given to answer the following problems. Answer in Python.

$$ \mathbf{A} = \left[\begin{array}{lcr} 3 & 2 & -1\\ 6 & 4 & -2\\ 5 & 0 & 3\\ \end{array}\right] \quad \mathbf{B} = \left[\begin{array}{lcr} 2 & 3 & 2\\ 3 & -4 & -2\\ 4 & -1 & 1\\ \end{array}\right] $$
  1. [2 points] Report the rank of matrix $\mathbf{A}$
  2. [2 points] Compute $\mathbf{A \cdot B}$
  3. [4 points] What is the second eigenvector of $\mathbf{B}$?
  4. [4 points] Solve $\mathbf{B}\vec{x} = \vec{b}$ where $\vec{b} = \left[14, -1, 11\right]$

3. Definite Integrals (16 Points)

Evaluate the following definite integrals using the quad function and the lambda keyword. You may only report your answer in Python and you should only print the integral area to 4 significant figures and nothing else. Do not define a function, use lambda to define your integrands if necessary. 4 points each.

  1. $ \int_0^{\pi / 4} \tan x\, dx $

  2. $\int_{-2}^0 3x^3\, dx$

  3. $\int_0^3 4x^2 - x \,dx$

  4. $\int_{-2}^2 \sin x^2 \, dx$

4. Complex Functions (14 Points)

1 (8 Points)

Plot [4 points] & evaluate [4 points] the following definite integral. Report your answer to 4 significant figures.

$$ \int_0^3 f(x)\, dx, \quad f(x) = \; \left. \begin{array}{llr} x^2 & \textrm{if} & |x| < 2\\ 4 & \textrm{if} & x > 2 \\ x & \textrm{otherwise} & \\ \end{array}\right\} $$

2 (6 Points)

Evlaute the following integral. Report your answer to 4 significant figures.

$$ \int_0^{1}\int_{-2 x}^x \sin xy \,dy\, dx $$

5. Working with Data (26 Points)

Use the given datasets to compute the requested quantities:

x = [-0.42,1.34,1.6,2.65,3.53,4.48,5.48,6.21,7.49,8.14,8.91,10.1]
y = [1.58,1.61,2.04,5.47,9.8,16.46,25.34,33.32,49.7,58.79,71.26,93.34]
  1. [6 points] Plot $x$ vs $y$
  2. [6 points] Compute $\frac{dy}{dx}$ using the central difference rule. Make sure your sigfigs are not overstated.
  3. [6 points] Compute $\int y\, dx$ using the trapezoidal rule
  4. [6 points] Compute $\int y\, dx$ using Riemann sums
  5. [2 points] Why is the difference so large between the two of them?

Extra Credit Confidence Interval (20 Points)

Write a function which will compute a confidence interval given a sample and confidence level. Your function should optionally take in the population standard deviation. Your function should return a value, $y$, such that $\mu = \bar{x} \pm y$. Demonstrate it on 3 examples: (1) 10 samples with known population standard deviation, (2) 30 samples with unknonw population standard deviation, and (3) 5 samples with unknown population standard deviation. You must both define the function (+ document it) and show it on these 3 examples to receive full credit. The confidence level (90%, 95%, etc) should be adjustable.