{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Extra credit opportunity\n",
        "\n",
        "# Extra credit\n",
        "\n",
        "This extra credit is worth 10 test points.\n",
        "\n",
        "You will need to print out your notebook where you did the work to\n",
        "complete this. Your first cell should be the following where your\n",
        "replace the two values marked with `<<>>` with your specific values."
      ],
      "id": "787d5de5-d600-478d-8782-e21446b2a44c"
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {},
      "outputs": [],
      "source": [
        "using Plots\n",
        "using MTH229\n",
        "plotly()\n",
        "nm = \"<<your NAME>>\"              # <--- put your name here, leave quotes\n",
        "a = <<LAST DIGIT OF STUDENT ID>>  # <--- put a number here\n",
        "a = max(a, 1)"
      ],
      "id": "2c2573c3"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We will consider the following function, where `a` has been defined in\n",
        "the first cell:"
      ],
      "id": "62f0f962-2b77-4ed6-86bf-71b0aa3f59b2"
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "f (generic function with 1 method)"
            ]
          }
        }
      ],
      "source": [
        "f(x) = a * log((a + sqrt(a^2 - x^2))/x) - sqrt(a^2 - x^2)"
      ],
      "id": "31d203fc"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The following will plot this function over the square viewing window of\n",
        "size `3a` by `3a`:"
      ],
      "id": "4b2f8a5d-91bb-4ea6-b25c-36adafea2391"
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {},
      "outputs": [],
      "source": [
        "plot(; xlims=(0, 3a), ylims=(0, 3a), legend=false, aspect_ratio=:equal)\n",
        "title!(\"My awesome plot, by $nm\")\n",
        "plot!(f, 0, a)"
      ],
      "id": "3f5b7d4f"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Just copy and paste the above into your notebook.\n",
        "\n",
        "## Task 1:\n",
        "\n",
        "-   For `c=a/2`, draw the tangent line at `(c,f(c))` on the graph:\n",
        "\n",
        "-   Write a function, `d(c)`, to compute the length of the line segment\n",
        "    between `(0, b)` and `(c, f(c))` where the tangent line is of the\n",
        "    form $y=mx + b$."
      ],
      "id": "2521b3d1-3bae-4929-a792-9612c79b4a64"
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {},
      "outputs": [],
      "source": [
        "d(c) = <<FILL THIS IN>>"
      ],
      "id": "ca7165ba"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "(Remember, `b` is the value of the tangent line when $x=0$.)\n",
        "\n",
        "-   Compare the value of `d(a/2)` to `a`.\n",
        "\n",
        "## Task 2:\n",
        "\n",
        "-   For *any* value of `c` in `[0,a]` write a function `pl(x, c)` which\n",
        "    finds the line *perpendicular* to the tangent line at $(c, f(c))$"
      ],
      "id": "1519e4cc-58a0-4e4d-bba0-1957a4e10ad9"
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {},
      "outputs": [],
      "source": [
        "pl(x, c) = <<FILL THIS IN>>"
      ],
      "id": "3024f94f"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "-   For `c=a/2`, again, add a plot of this normal line with the command"
      ],
      "id": "a425cfe3-2f66-48c5-95cf-999f61d259f8"
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {},
      "outputs": [],
      "source": [
        "plot!(x -> pl(x, a/2))"
      ],
      "id": "e24886b4"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "It should look *perpendicular* to the tangent line already drawn at\n",
        "$(c,f(c))$. (The `aspect_ratio=:equal` part of `plot` ensures this.)\n",
        "\n",
        "-   This makes a function to plot this normal line for any given `c`.\n",
        "    Just copy and paste it into your notebook. (The `linecolor` argument\n",
        "    mutes the look.)"
      ],
      "id": "1c27b2e3-cc9d-4f47-b400-2458cf70a8a7"
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "P (generic function with 1 method)"
            ]
          }
        }
      ],
      "source": [
        "P(c) = plot!(x -> pl(x, c); linecolor=RGBA(.5,.5,.5, 0.25))"
      ],
      "id": "6598628b"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "-   These commands will draw many such lines for different values of\n",
        "    `c`. Again, just copy and paste it into your notebook."
      ],
      "id": "18557b5c-2ff6-40cb-b681-2b86ea12e769"
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "metadata": {},
      "outputs": [],
      "source": [
        "cs = range(0, a, length=50)\n",
        "P.(cs)\n",
        "current()"
      ],
      "id": "9de78bdb"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Task 3:\n",
        "\n",
        "### Plotting an inverse function\n",
        "\n",
        "In the projects, we learned that to add a plot of a function `f` using\n",
        "the lower-level interface, we can first create `xs` and then plot the\n",
        "“`ys`” with"
      ],
      "id": "2f8eb0d4-9d72-444b-8fc3-714429e52636"
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "metadata": {},
      "outputs": [],
      "source": [
        "plot!(xs, f.(xs))"
      ],
      "id": "b5bfeb34"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We can use this style to plot an *inverse* function for `f`. The $x$ and\n",
        "$y$ values are reversed.\n",
        "\n",
        "-   We first create the `ys` and then plot the “`xs`” with:"
      ],
      "id": "8b7ede60-e688-4393-9e04-3ba56fcde716"
    },
    {
      "cell_type": "code",
      "execution_count": 14,
      "metadata": {},
      "outputs": [],
      "source": [
        "plot!(f.(ys), ys)"
      ],
      "id": "1bf5f4cc"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "In your graph, to emphasize the line, use a `linewidth` value bigger\n",
        "than the default:"
      ],
      "id": "eeb5f5ad-61f4-4981-a8de-f564f5e45fba"
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "metadata": {},
      "outputs": [],
      "source": [
        "plot!(f.(ys), ys; linewidth=5)"
      ],
      "id": "037a5ae9"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "-   Plot the *inverse* of the function of"
      ],
      "id": "5ae45f44-b96a-4ed5-9c96-13f5f9c34873"
    },
    {
      "cell_type": "code",
      "execution_count": 16,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "g (generic function with 1 method)"
            ]
          }
        }
      ],
      "source": [
        "g(x) = a * cosh(x/a)"
      ],
      "id": "2280d47d"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "using the range of `y` values seen:"
      ],
      "id": "70ed291c-1fc3-4b04-a090-84f4d6384922"
    },
    {
      "cell_type": "code",
      "execution_count": 17,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "0.0:0.09090909090909091:9.0"
            ]
          }
        }
      ],
      "source": [
        "ys = range(0, 3a, length=100)"
      ],
      "id": "37ca6b0c"
    },
    {
      "cell_type": "code",
      "execution_count": 18,
      "metadata": {},
      "outputs": [],
      "source": [
        "<<PLOTTING COMMANDS HERE>>"
      ],
      "id": "e3bbe15a"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "If you did it right, you can see that the graph suggested by the normal\n",
        "lines is the inverse of this parameterized `cosh` function. (See\n",
        "[Tractrix](https://en.wikipedia.org/wiki/Tractrix) for another\n",
        "illustration.)"
      ],
      "id": "2252da7a-7787-4b58-b210-d248ce3835dd"
    }
  ],
  "nbformat": 4,
  "nbformat_minor": 5,
  "metadata": {
    "kernelspec": {
      "name": "julia-1.11",
      "display_name": "Julia 1.11.4",
      "language": "julia",
      "path": "/Users/jverzani/Library/Jupyter/kernels/julia-1.11"
    }
  }
}