Homework 9

CHE 116: Numerical Methods and Statistics

3/31/2019


1. Creating Matrices (12 Points)

Create the following matrices using the given constraints. 4 Points each.

  1. In 3 lines of python (not including prints/imports), create a 6x12 matrix whose second column (where we count "first", "second", etc.) is the powers of 2 (e.g., 1, 2, 4, 8, 16). Its third is all 4's. All other elements are zero
  2. In 5 lines of python, accomplish the following: create a random 10x10 matrix using np.random.normal centered at 10 with standard deviation of 5, replace all negative values with 0 and then modify it so its rows sum to 1 and 0, use np.round to round to one decimal place.
  3. In at most 3 lines of python, create a 9x9 matrix where all elements are 0 except in the diagonal, which is 1's

2. Solving Systems of Equations (8 Points)

Solve the following systems of equations. 4 Points each. Write out your answer in Markdown

1.

$$\begin{array}{ll} 4x - 2y + z &= 0\\ 2x - 4y + z &= 1 \\ 2x + y + 3z &= 3\\ \end{array}$$

2.

$$\begin{array}{ll} e^{x} + y + 4z &= 4\\ 4e^{x} - 2y - z &= -1 \\ 3e^{x} + y + z &= 2\\ \end{array}$$

3. Eigenvalue/Eigenvector Problems (8 Points)

Calculate the eigenvalues and eigenvectors for the following matrices. Solve in Python and then write out the eigenvalues/eigenvectors in LaTeX. When writing decimals, only report two significant figures.

  1. [3 Points] $$A = \left[\begin{array}{lcr} 4 & 2 & 2\\ 4 & -8 & 4\\ 8 & 6 & -10\\ \end{array}\right]$$
  1. [3 Points] $$A = \left[\begin{array}{lcr} 1 & 5 & 1\\ 2 & -1 & 2\\ 1 & 2 & -3\\ \end{array}\right]$$
  1. [2 Points] Why would you use eigh over eig?

4. Slicing Practice (6 Points)

Using numpy, create a sum or difference of array slices that yields the requested quantity. Consider the following example:

To create this sequence: $$x_0 , x_2, x_4, \ldots $$

Use this slice

x[::2]

Use this particular array for this: x = np.arange(15), but use len(x) when you need to refer to the length of the array. 2 Points each.

  1. $x_1 \cdot x_0, x_2 \cdot x_1, \ldots x_N\cdot x_{N - 1}$
  2. $ x_0 + x_N, x_1 + x_{N - 1}, \ldots, x_{N} + x_0$
  3. $x_N - x_{N - 1}, x_{N - 1} - x_{N - 2},\ldots ,x_1 - x_0$

5. Numerical Differentation/Integration Methods (5 Points)

Given the following problems, what is the correct method to use? Do not solve the problems, just state the best method. 1 Point each

  1. Compute $\int_0^1 e^{-x^2}\,dx$
  2. You are given $f(x)$ evaluated at the following x values: $[0,0.1, 0.5, 0.9, 1.2]$. Compute the derivative at $f(0.5)$.
  3. Using the example from 5.2, compute $\int_{0.1}^{1.2} f(x)\,dx$
  4. $g(x) = x^3$. What is the derivative $g$ at $g'(x = 0.5)$
  5. Compute $\int_0^1 e^{-x}\,dx$

6. Numerical Differentation/Integration Methods (22 Points)

  1. [4 Points] Compute $\int_{-\infty}^{\infty} x^2 e^{-x^2}\,dx$. Use np.inf to refer to infinity and use a lambda function. Make sure it's clear what is the value of the integral in your print.

  2. [4 Points] Compute the numerical derivative of the following data:

    x = [0, 1, 2, 3, 4, 6, 7, 9]
    fx = [0.0, 0.84, 0.91, 0.14, -0.76, -0.28, 0.66, 0.41]
  3. [2 Points] Compute the numerical derivative of the following data:

    x2 = [0.0, 0.42, 0.83, 1.25, 1.67, 2.08, 2.5, 2.92, 3.33, 3.75, 4.17, 4.58, 5.0, 5.42, 5.83, 6.25, 6.67, 7.08, 7.5, 7.92, 8.33, 8.75, 9.17, 9.58, 10.0]
    fx2 = [0.0, 0.4, 0.74, 0.95, 1.0, 0.87, 0.6, 0.22, -0.19, -0.57, -0.85, -0.99, -0.96, -0.76, -0.43, -0.03, 0.37, 0.72, 0.94, 1.0, 0.89, 0.62, 0.26, -0.16, -0.54]
  4. [6 Points] Plot your data from 6.2 and 6.3 against $\cos(x)$, which is the correct derivative. Does the numerical derivative work even with the non-uniform coarse data in part 2?

  5. [6 Points] Integrate the data from 6.2 and compare against the true integral $\int_0^{9} \sin(x)\,dx$. How accurate is it?