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

assignment_1

MSE3114 Computational Methods for Physicists and Materials Engineers

Assignment 1

Q1. (Variables) Print out the values of the following variables and determine their types.

var0 = 1 + 100.
var1 = 1e5
var2 = 111 // 30
var3 = [[1, 2],
[3, 4]]
var4 = np.array([[1, 2],
[3, 4]])
var5 = {1.2, 2, 35., 87}
var6 = {
“a”: 1.2,
“b”: 2,
“c”: (35, 4),
“d”: “1234”,
}
var7 = var6[“c”]
var8 = var6[“d”][-1]
var9 = 3.0 == 3

(1$\times$10 = 10 marks)

Q2. (Functions)

Evaluate the following expressions by python:
$$
\log_{10} \left|\sin\left(\frac{4\pi}{3}\right)\right|.
$$

Define two matrices as lists:
$$
\mathbf{A}
= \left(\begin{array}{ccc}
1 & 2 & 3 \\
4 & 5 & 6
\end{array}\right),
\quad
\mathbf{B}
= \left(\begin{array}{ccc}
-7 & -8 & -9 \\
-10 & -11 & -12
\end{array}\right).
$$
Evaluate
$$
\arcsin(0.1 \mathbf{A})
+
\arccos(0.02 \mathbf{B}).
$$

Define two matrices as numpy arrays:
$$
\mathbf{A}
= \left(\begin{array}{ccc}
1 & 2 & 3 \\
4 & 5 & 6
\end{array}\right),
\quad
\mathbf{B}
= \left(\begin{array}{ccc}
-7 & -8 & -9 \\
-10 & -11 & -12
\end{array}\right).
$$
Evaluate
$$
\arcsin(0.1 \mathbf{A})
+
\arccos(0.02 \mathbf{B}).
$$

(2 + 4 + 4 = 10 marks)

Q3. (NumPy array and array index) Create and print out the matrix in form of NumPy array:
\begin{equation}
\mathbf{A}
= \left(\begin{array}{cccccccc}
1 & 2 & 3 & 0 & 0 & 0 & 0 & 0 \\
4 & 5 & 6 & 0 & 0 & 10 & 11 & 0 \\
7 & 8 & 9 & 0 & 0 & 12 & 13 & 0 \\
0 & 0 & 0 & 0 & 0 & 14 & 15 & 0 \\
0 & 18 & 19 & 20 & 0 & 16 & 17 & 0 \\
0 & 21 & 22 & 23 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 24 & 25 & 26
\end{array}\right).
\end{equation}

Decompose this matrix to four matrices with non-zero elements:
\begin{equation}
\mathbf{a}
= \left(\begin{array}{ccc}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{array}\right),
\quad
\mathbf{b}
= \left(\begin{array}{cc}
10 & 11 \\
12 & 13 \\
14 & 15 \\
16 & 17
\end{array}\right),
\quad
\mathbf{c}
= \left(\begin{array}{ccc}
18 & 19 & 20 \\
21 & 22 & 23
\end{array}\right),
\quad
\mathbf{d}
= \left(\begin{array}{ccc}
24 & 25 & 26
\end{array}\right).
\end{equation}
$\mathbf{a}$, $\mathbf{b}$, $\mathbf{c}$ and $\mathbf{d}$ must be constructed by slicing of $\mathbf{A}$.
(10 marks)

Q4. (Function definition and condition) The Internal Revenue Service (US) decides to use the following table to determine tax payment. Define a function which takes in the income and returns the tax to pay. Use this function to calculate the tax payments for the following incomes: \$5,000, \\$10,000, \$15,000, \\$22,000, \$30,000, \\$38,000, and \$50,000.

Taxable income Tax payable
\$10,000 or less 10\% of taxable income
\$10,000 to \\$20,000 \$1,000 + 20\% of amount by which taxable income exceeds \\$10,000
\$20,000 to \\$40,000 \$3,000 + 30\% of amount by which taxable income exceeds \\$20,000
More than \$40,000 \$9,000 + 50\% of amount by which taxable income exceeds \\$40,000

(20 marks)

Q5. (Looping) Powder x-ray diffraction is a technique used to characterize the structure of crystals. The idea is illustrated in the figure below. The crystalline grains in the powder have all possible orientations. For the incident beam of a particular direction, diffraction can occur only for a discrete set of angles $2\theta$. So, in the plot of the intensity vs. $2\theta$ (see the figure), we see a set of peaks corresponding to the $2\theta$ values where diffraction occurs. For simple cubic crystals, the angle $2\theta$ for which diffraction occurs should satisfy the following condition:
$$
\sin \theta = \frac{\lambda}{2a} \sqrt{h^2 + k^2 + l^2},
$$
where $\lambda$ is the wavelength of the x-ray, $a$ is the lattice parameter, and $h$, $k$ and $l$ are integers representing the crystallographic indices.

The wavelength of x-ray typically used in experiments is $1.54$ angstrom. Assume that the lattice parameter is $a = 4.0$ angstrom. Write a python code to calculate the $2\theta$ values corresponding to the first 10 peaks in the plot of intensity vs. $2\theta$. (You can check your answer with the experimental measurement for BaTiO$_3$).

Hint: You may loop the values of $h$, $k$ amd $l$ from $-N$ to $N$, calculate $2\theta$ according to the formula, and store the $2\theta$ values to a list. Since some $(h,k,l)$ values result in the same value of $2\theta$, we need to delete the repeated items in the list. A possible way to delete the repeated items in a list is converting the list to set (recall that no repeated item is allowed in a set) and then converting the set back to list. Finally, sort the list and print out the first 10 items of the list.
(30 marks)

Q6. (File handling) Download the file “cnt8x3.cfg” from Canvas. This file contains the information of an atomic configuration.

Read the file “cnt8x3.cfg”. Firstly, read in the box size in Lines 6, 12 and 18 (Note: the first line of the file is defined as “Line 0”):


H0(1,1) = 23.457426737892 A

H0(2,2) = 23.457426737892 A

H0(3,3) = 42.5470657037592 A

The box size along the x-axis is the number in Line 6: “23.457426737892”. The box size along the y-axis is the number in Line 12: “23.457426737892”. The box size along the z-axis is the number in Line 18: “42.5470657037592”. Store these three values in the list “box”.

Read in the names of elements and the coordinates from Line 44 to 431:


12.011 C .666666666666667 .5 0 0 0 0
12.011 C .656201117501057 .55812906905498 .991408934707904 0 0 0

12.011 C .658001529992712 .553040496771241 .780068728522336 0 0 0

Column 1 is the names of elements (again, the first column is defined as “Column 0”). Here, all the names of elements are “C” (carbon). Store the elements in the list “elem”. Columns 2, 3 and 4 are the fractional x-, fractional y- and fractional z-coordinates. Calculate the x-coordinates by multiplying the fractional x-coordinates by the box size along the x-axis and store the results in the list “x_coor” (box size has already been obtained in Problem 1). Calculate the y-coordinates by multiplying the fractional y-coordinates by the box size along the y-axis and store the results in the list “y_coor”. Calculate the z-coordinates by multiplying the fractional z-coordinates by the box size along the z-axis and store the results in the list “z_coor”.

Write the file “cnt8x3.xyz”. The first line is the number of atoms, i.e., the length of the list “x_coor”. The second line is a description of this configuration; you can give an arbitrary string. But, you are required to write your name in this line. Starting from the third line, write the atomic configuration in this format:

Column 0 Column 1 Column 2 Column 3
(name of element) (x coordinate) (y coordinate) (z coordinate)

The first four lines look like:

388
your name
C 15.638284491928006 11.728713368946 0.0
C 15.392789639103905 13.092271747645059 42.18154108431111

Submit the created file “cnt8x3.xyz”.

(Bonus) Install a python library “mogli” by running

pip install mogli

in the command prompt or terminal. Run the following code (the file created in the last question “cnt8x3.xyz” is placed in the same folder):

import mogli
atom_struct = mogli.read(‘cnt8x3.xyz’)
mogli.show(atom_struct[0], bonds_param=1.8)

A window will pop up showing an atomic structure. Submit the snapshot of this structure. What is this structure?

(20 + bonus 10 marks)