# CMSC6950 Assignment 3

Due on Tuesday, May 29, 2018

Solve all questions.

*Any two identical submitted assignments from different students will be considered as copies of each other.*

Save your assignment as an ipynb file with a filename ”userid_assignment3.ipynb”. [For userid, I mean your MUN user id/email .e.g. abc123 and not your student number]. Submit your assignment through the D2L dropbox.

### Name :
### Student Number:


## Question 1

Define the following vectors and matrices:

```
vec1 = np.array([ -1., 4., -9.])
mat1 = np.array([[ 1., 3., 5.], [7., -9., 2.], [4., 6., 8. ]]
```
1. You can multiply vectors by constants. Compute `vec2 = (np.pi/4) * vec1`
2. The cosine function can be applied to a vector to yield a vector of cosines.
    Compute `vec2 = np.cos(vec2)`
3. You can add vectors and multiply by scalars. Compute
    `vec3 = vec1 + 2 * vec2`
4. The Euclidean norm of a matrix or a vector is available using `la.norm`. Compute `la.norm(vec3)`
5. You can do row-column matrix multiplication. Compute the product of `mat1` and `vec3` and set `vec4` equal to the result.
6. Compute the transpose of `mat1`.
7. Compute the determinant of `mat1`.
8. Find the smallest element in `vec1`.
9. What function would you use to find the value of `j` so that `vec1[j]` is equal to the smallest element in `vec1`?
10. What expression would you use to find the smallest element of the matrix `mat1`?


```python

```

## Question 2
1. Generate and plot values (50,50) drawn from a normal distribution.  Make a plot as shown in (a).

2. Manipulate the array such that the lowest values are in the top-left corner, the highest in the bottom-right. Make a plot as shown in (b).




```python

```

## Question 3

Using the data available at [http://berkeleyearth.lbl.gov/auto/Regional/TAVG/Text/hawaii-TAVG-Trend.txt](http://berkeleyearth.lbl.gov/auto/Regional/TAVG/Text/hawaii-TAVG-Trend.txt), reproduce the following plot using Numpy, Matplotlib, and/or Scipy.


[High resolution version](http://berkeleyearth.lbl.gov/auto/Regional/TAVG/Figures/hawaii-TAVG-Trend.pdf)

Here's a nice way to ensure the data file is always available locally:


```python
import urllib.request
urllib.request.urlretrieve('http://berkeleyearth.lbl.gov/auto/Regional/TAVG/Text/hawaii-TAVG-Trend.txt', 
                   'hawaii-TAVG-Trend.txt')
```




    ('hawaii-TAVG-Trend.txt', <http.client.HTTPMessage at 0x1035ff0b8>)




```python
!ls -l 'hawaii-TAVG-Trend.txt'
```

    -rw-------@ 1 jmunroe  staff  157295 22 May 11:44 hawaii-TAVG-Trend.txt



```python

```

## Question 4

1 . As you know, a magic square is a matrix all of whose row sums, column
sums and the sums of the two diagonals are the same. (One diagonal of
a matrix goes from the top left to the bottom right, the other diagonal
goes from top right to bottom left.) Show by direct computation that if
the matrix A is given by

```A=np.array([[17, 24, 1, 8, 15],
[23, 5, 7, 14, 16],
[ 4, 6, 13, 20, 22],
[10, 12, 19, 21, 3],
[11, 18, 25, 2, 9]])```

The matrix A has 5 row sums (one for each row), 5 column sums (one for
each column) and two diagonal sums. These 12 sums should all be exactly
the same, and you could verify that they are the same by printing them
and “seeing” that they are the same. It is easy to miss small differences
among so many numbers, though. Instead, verify that A is a magic
square by constructing the 5 column sums and computing the maximum
and minimum values of the column sums. Do the same for the 5 row sums,
and compute the two diagonal sums. Check that these six values are the
same. If the maximum and minimum values are the same, the flyswatter
principle says that all values are the same.

*Hints*: The function `np.diag` extracts the diagonal of a matrix, and the
function `np.fliplr` extracts the other diagonal.


```python

```

2 . From Wikipedia:
> A magic square is a $ n \times n$ square grid (where $n$ is the number of cells on each side) filled with distinct positive integers in the range {$1, 2, \ldots, n^2$)
such that each cell contains a different integer and the sum of the integers in each row, column and diagonal is equal. The sum is called the magic constant or magic sum of the magic square. A square grid with $n$ cells on each side is said to have order $n$.

Define a function that tests if a matrix A is a magic square.  Using this function, write a program that (naively) searches for all possible magic squares of order 3 by explicitly testing all possible combinations. Display all possible $3 \times 3$ magic squares. 



```python

```

## Question 5

1 . Use `matplotlib.pyplot.plot` to produce a plot of the functions 
$f(x) = e^{−x/10} \sin(\pi x)$ and $g(x) = x e^{−x/3}$ over the interval [0, 10]. 
Include labels for the x- and y-axes, and a legend explaining which line is which plot.


```python

```

2 . The shape of a limaçon  can be defined parametrically as

\begin{align}
r &= r_0 + \cos θ \\ 
x &= r \cos θ \\
y &= r \sin θ
\end{align}

When $r_0 = 1$, this curve is called a cardioid. Use this definition to plot
the shape of a limaçon for $r_0 = 0.8$, $r_0 = 1.0$, and $r_0 = 1.2$. Be sure to
use enough points that the curve is closed and appears smooth (except for
the cusp in the cardioid). Use a legend to identify which curve is which.


```python

```

4e629190dbc10644546acf894ccf4152fe109e47
ipynb
Jupyter Notebook
assignments/assignment3.ipynb
cmsc6950-spring-2018/CMSC6950.github.io
9dcbddc30c0df1550f796a836f956aaaa091573a
CC-BY-4.0
assignments/assignment3.ipynb
cmsc6950-spring-2018/CMSC6950.github.io
9dcbddc30c0df1550f796a836f956aaaa091573a
CC-BY-4.0
assignments/assignment3.ipynb
cmsc6950-spring-2018/CMSC6950.github.io
9dcbddc30c0df1550f796a836f956aaaa091573a
CC-BY-4.0
2020-05-23T00:59:08.000Z
2020-05-23T00:59:08.000Z
Qwen/Qwen-72B
1. YES
2. YES