CS计算机代考程序代写 python assignment_3

assignment_3

MSE3114 Computational Methods for Physicists and Materials Engineers

Assignment 3

Q1. Atom probe tomography (APT) is an experimental technique to measure the spatial distribution of certain elements inside a material at atomic scale. Li et al. measured the spatial distribution of carbon atoms in cold-drawn pearlitic steels [Li, et al. Acta Mater. 59 (2011)]. The experimental results are shown in the following figures. The first figure corresponds to the case where the steel is cold-drawn by 200\% (strain $\epsilon = 2$); the second figure to the case where the steel is cold-drawn by 500\% (strain $\epsilon = 5$). You can download these two figures with high resolution from Canvas. Let’s analyze these results by python.

Define a function which takes in the path of an image and returns a list of the distances where the carbon atoms are found (see the above figure for the definition of “distance”). Hint:

An image can be read by matplotlib.image.imread().
The RGB code for the carbon atom (i.e., the yellow points in the above figure) is about $(216, 233, 53)$. Note that you should allow $\pm 40$ error about each value.
Loop over each pixel and check if the RGB at each pixel is in the range $(216 \pm 40, 233 \pm 40, 53 \pm 40)$. If a pixel is associated with the RGB in this range, we will view this pixel as a carbon atom and store its distance to a list.

Apply the function defined in Question 1 to the above two figures. Plot the histogram of the probablity of carbon atoms vs. distance for each figure. Hint:

Since you need to analyze two figures, finally you should plot two histograms. So, you need to use fig, axs = plt.subplots(2) (plt is short for matplotlib.pyplot).
Each histogram can be plotted by axs[i].hist(). Set the width of bin to $0.5$. Do normalization by setting density=True.
The final result for the analysis of the first figure may look like (NOT exactly same as) the following figure:

(30 marks)

Q2. Derived from the tight-binding Hamiltonian of graphene, the band structure of graphene is composed by two branches:
$$
E_+(k_x, k_y) = \frac{\gamma_0 \left|f(k_x, k_y)\right|}{1 – s_0 \left|f(k_x, k_y)\right|}
\quad \text{and} \quad
E_-(k_x, k_y) = \frac{- \gamma_0 \left|f(k_x, k_y)\right|}{1 + s_0 \left|f(k_x, k_y)\right|},
$$
where $s_0 = 0.129$ and
$$
\left|f(k_x, k_y)\right|
= \sqrt{1 + 4 \cos\left(\frac{\sqrt{3}k_x a}{2}\right) \cos\left(\frac{k_y a}{2}\right) + 4 \cos^2\left(\frac{k_y a}{2}\right)}.
$$
Plot the band structure of graphene by matplotlib.

Define the dimensionless quantities: $\tilde{E}_+ = E_+/\gamma_0$, $\tilde{E}_- = E_-/\gamma_0$, $\tilde{k}_x = k_x a$ and $\tilde{k}_y = k_y a$. Rewrite the above equations by $\tilde{E}_+$, $\tilde{E}_-$, $\tilde{k}_x$ and $\tilde{k}_y$. In the new equations, $\gamma_0$ and $a$ are eliminated. So, we don’t need to know the values of $\gamma_0$ and $a$.

Plot $\tilde{E}_+(\tilde{k}_x, \tilde{k}_y)$ and $\tilde{E}_-(\tilde{k}_x, \tilde{k}_y)$ by plot_surface() in the same figure.

(20 marks)

Q3. Suppose that an event repeatedly occurs over time at a constant rate but at random (Poisson process). For example, let’s observe the arrival of buses at a bus stop. On average we see a bus every 10 minutes. However, the time interval between the arrivals of two buses is exactly 10 minutes. We may see the buses, for example, at 0 min, 13.1 min, 19.7 min, 33.3 min, 40.2 min, …

We divide a time period of concern into $n$ pieces. In each piece of time interval, the probability for occurrence of an event is $p \in [0, 1]$. Then, the probability that this event occurs $k$ times in the whole time period is
$$
\text{Pr}(k, n, p) = \frac{n!}{k! (n-k)!} p^k (1-p)^{n-k}.
$$
This is called binomial distribution. Do the following operations by SymPy.

Define the symbols $n$ and $k$ standing for two nonnegative integers. Define the symbol $p$ standing for a nonnegative real number.

Define the expression for $\text{Pr}(k, n, p)$ and print out this expression by pprint().

Substitute $n = 20$ and $p = 0.5$ into the expression of $\text{Pr}(k, n, p)$. Then, the new expression is only a function of $k$, i.e., $\text{Pr}(k)$

Convert the SymPy expression $\text{Pr}(k)$ to a python function called binomial_prob_k_func() by sympy.lambdify. Note that the expression $\text{Pr}(k)$ may contain the operation sympy.factorial() which is not supported by NumPy but is supported by SciPy. So, we cannot use sympy.lambdify( … , “numpy”); instead, we must use sympy.lambdify( … , “scipy”).

Try three sets of parameters: $n = 20$ and $p = 0.4$, $n = 20$ and $p = 0.7$, $n = 40$ and $p = 0.5$. Similar to Question 3, for each set of parameters, substitute $n$ and $p$ to the expression of $\text{Pr}(k, n, p)$. Then, similar to Question 4, for each set of parameters, define the python function binomial_prob_k_func(). Finally, for each set of parameters, plot this python function, i.e., $\text{Pr}(k)$.

(25 marks)

Q4. An RL circuit is an electrical circuit consisting of a resistor and an inductor, as shown in the following figure.

The voltage source raise the voltage V. Through the resistor, the voltage drops by $IR$ ($I$ is the current and $R$ is the resistance). Through the inductor, the voltage drops by $L \mathrm{d}I/\mathrm{d}t$ ($L$ is the inductance). So, we have the ordinary differential equation:
$$
I(t) R + L \frac{\mathrm{d}I(t)}{\mathrm{d}t} = V,
$$
where we emphasize that $I$ is a function of time $t$. Solve this equation of SymPy.

Define the symbols $I$, $R$, $L$, $t$ and $V$.

Define the above equation.

Solve the equation with the initial condition: $I(t=0) = 0$.

Substitute the parameters: $R = 50$ (unit: $\Omega$), $L = 1$ (unit: H) and $V = 5$ (unit: V) into the expression of the solution. Print out the solution after substitution.

Convert the solution $I(t)$ to a python function by sympy.lambdify().

Plot $I$ vs. $t$ by matplotlib.

Replace the DC voltage source by an AC voltage source such that $V = 5 \sin(200 t)$ (unit: V). Solve the above differential equation for this AC source with parameters given in Question 4 (except $V$). Finally, plot $I$ vs. $t$.

(25 marks)