# Homework 01

Congratulations! You've managed to open this Juypter notebook on either Github or on your local machine.

Help for Jupyter Notebooks can be found in the Jupyter Lab by going to `Help > Notebook Reference`. You can also go to the [Notebook basics](https://jupyter-notebook.readthedocs.io/en/latest/examples/Notebook/Notebook%20Basics.html) documentation.

The basics are that you can write *markdown* cells that have math, words, and other markdown (like Rmarkdown!)... and *code* cells that, well, have code and display the output below (as in Rmarkdown). Switch *mode* to change between the two (sidebar will change colors).

If you want to export your document to .pdf (you don't have to!), you an go to `File > Export Notebook As... > PDF`. To do this, I had to install the *Inkscape* application. (I did this with the [Chocolatey](https://chocolatey.org/) package manager on Windows. You can probably do this with [Homewbrew](https://brew.sh/) on a Mac)

# Instructions

Your homework is to generate 3d plots of a quadratic function in $\mathbb R^2$ and to examine the relationship between eigenvalues of the Hessian matrices, shapes of the functions, and the (possible) existence of minima and maxima.

You can find the documentation for `Plots.jl` at <http://docs.juliaplots.org/latest/>

For the following functions

\begin{align}
    f^a(x,y) &= -x^2       - y^2 \\
    f^b(x,y) &= -x^2 +  xy - y^2 \\
    f^c(x,y) &= -x^2 + 2xy - y^2 \\
    f^d(x,y) &= -x^2 + 3xy - y^2 
\end{align}

1. Write the Hessian matrix in \LaTeX

2. Compute the determinants by hand. Are the Hessians PD, PSD, NSD, or ND? What does this imply about convexity / concavity of the function? What about the existence of a minimum or maximum over the domain $\mathbb R^2$?

3. `@assert` statements are wonderful to include in your functions because they make sure that the inputs meet certain assumptions... such as that the length of two vectors is the same. Using them regularly can help you avoid errors

    Use an `@assert` statement to check that your determinants computed by hand are correct. See what Julia does when you put the wrong determinanmt in. See [`LinearAlgebra.det`](https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/index.html#LinearAlgebra.det) docs

    ```julia
    @assert det(Ha) == ???
    ```

4. Compute the eigenvalues of your matrix using [`LinearAlgebra.eigvals`](https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/index.html#LinearAlgebra.eigvals)

5. Create a function in Julia to compute $f^a, f^b, f^c, f^d$ as above. Plot them!


To submit your homework, commit this notebook to your personal homework repo, push it, and issue a pull request to turn it in to me.

# Top of your Julia Code


```julia
# Load libraries first of all

using LinearAlgebra  # load LinearAlgebra standard library
using Plots          # loads the Plots module
```

    ┌ Info: Precompiling Plots [91a5bcdd-55d7-5caf-9e0b-520d859cae80]
    └ @ Base loading.jl:1242



```julia
# tells Plots to use the GR() backend.
#    Note: for interactive 3d plots, you can also install 
#    PyPlot or PlotlyJS and try using those. You might 
#    need to use Atom or the REPL to get the interactivity
gr()
```




    Plots.GRBackend()




```julia
# define a range we can iterate over
xrange = -3.0 : 0.1 : 3.0
```




    -3.0:0.1:3.0



# Question 1

$$
f^a(x,y) = -x^2 - y^2
$$   

## Part 1a 

Hessian is $H^a = \begin{bmatrix} -2 & 0 \\ 0 & -2 \end{bmatrix}$

## Part 1b

The determinant is $|H^a| = 4$, so the matrix is negative definite, and it has a global maximum at [0,0]


```julia
# define the Hessian for H^a. 
# Note:
#     Julia in Jupyter Notebooks and Atom can handle latexy characters
#     I got fancy by typing H\^a [tab] and getting a superscript
#     We could have also gotten greek letters with \beta [tab]
#     or (very important) approximately equals with \approx [tab]

Hᵃ = [-2 0 ; 0 -2]
```




    2×2 Array{Int64,2}:
     -2   0
      0  -2



## Part 1c


```julia
@assert det(Hᵃ) == 4
```

## Part 1d


```julia
eigvals(Hᵃ)
```




    2-element Array{Float64,1}:
     -2.0
     -2.0




```julia
# functions to plot
fa(x,y) = -x^2 - y^2
```




    fa (generic function with 1 method)




```julia
plot(xrange, xrange, fa, st = :surface, title = "\$a=0\$")
```




    

    



# Question 2

$$
f^b(x,y) = -x^2 +xy - y^2
$$   

## Part 2a 

Hessian is $H^b = \begin{bmatrix} -2 & 1 \\ 1 & -2 \end{bmatrix}$

## Part 2b

Determinant is $|H^b| = 2$, so the matrix is negative definite, and it has a global  maximum at [0,0]


```julia
Hᵇ= [-2 1; 1 -2] 
```




    2×2 Array{Int64,2}:
     -2   1
      1  -2




```julia
@assert det(Hᵇ)== 3
```


```julia
eigvals(Hᵇ)
```




    2-element Array{Float64,1}:
     -3.0
     -1.0




```julia
# functions to plot
fb(x,y) = -x^2 + x*y - y^2

```




    fb (generic function with 1 method)




```julia
plot(xrange, xrange, fb, st = :surface, title = "\$a=0\$")
```




    

    



plot(xrange, xrange, fa, st = :surface, title = "\$a=0\$")

# Question 3

$$
f^c(x,y) = -x^2 +2xy -y^2
$$   

## Part 3a 

Hessian is $H^c = \begin{bmatrix} -2 & 2 \\ 2 & -2 \end{bmatrix}$

## Part 3b

Determinant is $|H^c| = 0$, so the matrix is negative semi definite, and it has a non unique global  maximum at [0,0]


```julia
Hᶜ= [-2 2; 2 -2] 
```




    2×2 Array{Int64,2}:
     -2   2
      2  -2




```julia
@assert det(Hᶜ)== 0
```


```julia
eigvals(Hᶜ)
```




    2-element Array{Float64,1}:
     -4.0
      0.0




```julia
# functions to plot
fᶜ(x,y) = -x^2 + 2*x*y - y^2
```




    fᶜ (generic function with 1 method)




```julia
plot(xrange, xrange, fᶜ, st = :surface, title = "\$a=0\$")
```




    

    



# Question 4

$$
f^c(x,y) = -x^2 +3xy- y^2
$$   

## Part 4a 

Hessian is $H^d = \begin{bmatrix} -2 & 3 \\ 3 & -2 \end{bmatrix}$

## Part 4b

Determinant is $|H^d| = -5$, so the matrix is indefinite, and $f^d$ has no global maximum.


```julia
Hᵈ= [2 3; 3 2]
```




    2×2 Array{Int64,2}:
     2  3
     3  2




```julia
@assert det(Hᵈ) == -5
```


```julia
eigvals(Hᵈ)
```




    2-element Array{Float64,1}:
     -0.9999999999999998
      5.0               




```julia
fᵈ(x,y) = -x^2 + 3*x*y - y^2
```




    fᵈ (generic function with 1 method)




```julia

plot(xrange, xrange, fᵈ, st = :surface, title = "\$a=0\$")
```




    

    




```julia

```

069f3acc64e8ebf5d4894d3de3ba1b21b2dae102
ipynb
Jupyter Notebook
Hwk01.ipynb
jhbuckner/Hwk01
8077ad222921b4825c7c99739fe72587c75ee9c3
MIT
Hwk01.ipynb
jhbuckner/Hwk01
8077ad222921b4825c7c99739fe72587c75ee9c3
MIT
Hwk01.ipynb
jhbuckner/Hwk01
8077ad222921b4825c7c99739fe72587c75ee9c3
MIT
Qwen/Qwen-72B
1. YES
2. YES