assignment_2
MSE3114 Computational Methods for Physicists and Materials Engineers
Assignment 2
Q1. Write down the matrix representation of the second derivative operator. (As an example, the matrix representation of the first derivative operator is given on Page 31 of the lecture note.) (20 marks)
Q2. Write a python code to do the following operations.
Create three vectors in 3D space in form of NumPy arrays:
\begin{equation}
\mathbf{a}_1 = \left(\begin{array}{c}
1.0 \\ 2.1 \\ 3.2
\end{array}\right),
\quad
\mathbf{a}_2 = \left(\begin{array}{c}
-3.0 \\ 0.1 \\ -2.2
\end{array}\right),
\quad
\mathbf{a}_3 = \left(\begin{array}{c}
-0.01 \\ 10 \\ 5.2
\end{array}\right).
\end{equation}
Define a function vec_proj() which takes in two vectors (NumPy arrays) and returns the projection of the first vector on the second vector. Then, apply this function to calculate the projection of $\mathbf{a}_2$ on $\mathbf{a}_1$ and the projection of $\mathbf{a}_1$ on $\mathbf{a}_2$.
Define a function vec_angle() which takes in two vectors (NumPy arrays) and returns the angle (in degree) between these two vectors. Then, apply this function to calculate the angle between $\mathbf{a}_1$ and $\mathbf{a}_2$, the angle between $\mathbf{a}_2$ and $\mathbf{a}_3$, and the angle between $\mathbf{a}_3$ and $\mathbf{a}_1$.
Define a function vec_vol() which takes in three vectors (NumPy arrays) and returns the area of the parallelepiped spanned by these three vectors. Then, apply this function to calculate the area of the parallelepiped spanned by $\mathbf{a}_1$, $\mathbf{a}_2$ and $\mathbf{a}_3$.
(4$\times$5 = 20 marks)
Q3. Write a python code to do the following operations.
Create a stress tensor and a strain tensor in form of NumPy arrays:
\begin{equation}
\boldsymbol{\sigma} = \left(\begin{array}{c}
1.0 & -0.2 & 0.5 \\
-0.2 & 4.3 & 0.0 \\
0.5 & 0.0 & 5.2
\end{array}\right),
\quad
\boldsymbol{\epsilon} = \left(\begin{array}{c}
0.02 & -0.01 & 0.03 \\
-0.01 & 0.2 & -0.05 \\
0.03 & -0.05 & 0.4
\end{array}\right).
\end{equation}
The hydrostatic stress is
\begin{equation}
\boldsymbol{\sigma}_{\text{hyd}} = \left(\frac{1}{3}\text{tr}\,\boldsymbol{\sigma}\right) \mathbf{I},
\end{equation}
and the deviatoric stress is
\begin{equation}
\boldsymbol{\sigma}_{\text{dev}} = \boldsymbol{\sigma} – \boldsymbol{\sigma}_{\text{hyd}}.
\end{equation}
Define a function stress_hyd_dev() which takes in a stress tensor and returns the hydrostatic stress and the deviatoric stress. Then, apply this function to calculate the hydrostatic stress and the deviatoric stress of the stress defined in Question 1.
If we rotate the material, the representation of the stress tensor will change. Suppose that the coordinate system is $(\mathbf{e}_1, \mathbf{e}_2, \mathbf{e}_3)$. If we rotate the material about an axis $\mathbf{u} = (u_1, u_2, u_3)$ (a unit vector) by the angle $\theta$, the representation of the stress tensor will become
\begin{equation}
\boldsymbol{\sigma}’ = \mathbf{R} \boldsymbol{\sigma},
\end{equation}
where $\mathbf{R}$ is the rotation matrix:
\begin{equation}
\mathbf{R} = \left(\begin{array}{ccc}
\cos\theta + u_1^2 (1 – \cos\theta) &
u_1 u_2 (1 – \cos\theta) – u_3 \sin\theta &
u_1 u_3 (1 – \cos\theta) + u_2 \sin\theta \\
u_2 u_1 (1 – \cos\theta) + u_3 \sin\theta &
\cos\theta + u_2^2 (1 – \cos\theta) &
u_2 u_3 (1 – \cos\theta) – u_1 \sin\theta \\
u_3 u_1 (1 – \cos\theta) – u_2 \sin\theta &
u_3 u_2 (1 – \cos\theta) + u_1 \sin\theta &
\cos\theta + u_3^2 (1 – \cos\theta)
\end{array}\right).
\end{equation}
Define a function stress_rot() which takes in a stress tensor, rotation axis and rotation angle and returns the stress tensor after rotation. Then, apply this function to calculate the stress tensor after rotating the material about $\mathbf{u} = (1, 1, 1)$ by the angle $\pi/6$.
(6+6+8 = 20 marks)
Q4. Write a python code to do the following operations.
Create a $1000 \times 1000$ matrix $\mathbf{A}$. Each element of the matrix is a random integer in the range $[1, 100]$.
Convert the type of the matrix elements from integer to floating point.
Create the matrix $\mathbf{B}$ by doing the following element-wise operation on $\mathbf{A}$:
$$
B_{ij} = \dfrac{A_{ij}^{10}\sqrt{A_{ij}}}{\sin A_{ij}},
$$
where $B_{ij}$ and $A_{ij}$ denote the elements of $\mathbf{B}$ and $\mathbf{A}$. You are required to do this element-wise operation explicitly by loop over the elements of $\mathbf{B}$ and $\mathbf{A}$. Print the first element of $\mathbf{B}$ and get the time for creating this $\mathbf{B}$ in unit of nanosecond.
Create the same matrix $\mathbf{B}$ by NumPy element-wise operation. Print the first element of $\mathbf{B}$ and get the time for creating this $\mathbf{B}$ in unit of nanosecond.
Let’s call the matrix $\mathbf{B}$ created in Q4.3 $\mathbf{B}_1$ and the matrix $\mathbf{B}$ created in Q4.4 $\mathbf{B}_2$. Although $\mathbf{B}_1$ and $\mathbf{B}_2$ are created in different ways, they should be identical in principle. In order to check if they are really identical, we can look at the Frobenius norm of the relative error:
$$
\left\| \frac{\mathbf{B}_2 – \mathbf{B}_1}{\mathbf{B}_1} \right\|_\text{F}.
$$
In the above expression, all the operations inside $\| \cdot \|$ are element-wise. Evaluate this Frobenius norm and check if the relative error is small.
(5$\times$4 = 20 marks)
Q5. Plot four hyperbolic functions by matplotlib: $y = \sinh(x)$, $y = \cosh(x)$, $y = \tanh(x)$ and $y = \text{sech}(x)$. The final result may look like (NOT exactly same as) the following figure:
The requirements are:
The argument of all the functions is $\mathbf{x} = [-2.0, -1.9, -1.8, \cdots, 1.8, 1.9, 2.0]$. Create $\mathbf{x}$ as a Numpy array.
The four figures are arranged as the above figure.
Set the color of the points and curves to “blue”.
Set the shape of the points to “square”.
Set the domain of the $x$-axis to $[-2, 2]$. Set the domain of the $y$-axis to $[-2, 2]$.
Draw grid lines.
Add a title to the figure: “Hypberbolic function ($\Delta x = 0.1$)”.
(20 marks)