# Part 1: Matrix and vector operations. 

1. Solve the following system: 

$$
\left[\begin{array}{ccccc}
a_1 & b_1 &  & & 0 \\
c_1 & a_2 & b_2 & & \\
& \ddots & \ddots & \ddots & \\
& &  & a_{99} & b_{99} \\
0 & & & c_{99} & a_{100} 
\end{array}\right] 
\left[\begin{array}{c}
x_{1} \\
x_{2} \\
\vdots \\
x_{100}
\end{array}\right]
=
\left[\begin{array}{c}
d_{1} \\
d_{2} \\
\vdots \\
d_{100}
\end{array}\right]
$$
where 
$$
a_j = j, \quad b_j = 1/j, \quad c_j = 1, \quad d_j = \sin(j \pi / 200)
$$
and print $x_1, x_2, \dots, x_5$. 


# Part 2: For loops. 

1. Write a function that uses a `for` loop to calculate the following with a sequence of $m$, and generate a plot for $m$ verses $E_m$. Avoid using a `for` loop, can you complete the same task?  

$$
E_m = 1 + \frac{1}{2} + \dots + \frac{1}{2^m} - \log(2^m)
$$


2. Let's draw a regular polygon of n sides, with a horizontal bottom side, and the corners of the polygon staying on the unit circle. For given $n = 5$ and $r = 0.6$, start the first point $p_1 = (x_1, y_1)$ as a random number in $(-0.5, 0.5) \times (-0.5, 0.5)$, generate $10^4$ points interactively. In the $j$th iteration, we choose one corner $z_*$ of the polygon randomly and let $p_{j+1} = (x_{j+1}, y_{j+1})$ be the point on the line segment between $p_j$ and $z_*$, with the distance from $p_{j+1}$ to $p_j$ being $r$ times the distance from $z_j$ to $p_j$ and then draw all these points as dots in the $xy$ panel. 

Hint: Complex numbers can be used to represent points in the $xy$ plane. The following script works for even or odd $n$, and the polygon always has a flat bottom. 

```{r, fig.width=5}
n <- 8
t <- c(0:n) - 0.5
z <-  exp(2i * pi * (t/n - 0.25))

plot(Re(z), Im(z))
```

