{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# \n", "\n", "# Graphing functions with Julia\n", "\n", "Read about this here: [Graphing Functions with\n", "Julia](http://mth229.github.io/graphing.html).\n", "\n", "`Julia` has several packages that allow for graphical presentations, but\n", "nothing “built-in.”\n", "\n", "Several reasonable options exist, but these are the two most common\n", "packages used:\n", "\n", "- `Plots` which offers a unified front end to several different\n", " plotting backends (`pyplot` or `matplotlib`; `GR`; `plotly`;\n", " `unicodeplots`; and others). Plots has a recipe system that makes it\n", " easier to add default plots to many object types. We will utilize\n", " the interface for functions. In the following we call `plotly()` to\n", " set the Plotly backend for web-based graphics. This sometimes fails\n", " due to JavaScript issues. The `gr()` backend produces static\n", " graphics and is available by default.\n", "\n", "- `Makie` which offers the most advanced graphics interface,\n", " especially for 3-d graphics. It will *likely* be the default choice\n", " for `Julia` users at some point.\n", "\n", "We load both a plotting package, its backend, and the `MTH229` package\n", "with this block of commands:" ], "id": "2ad8bb27-233f-4cc9-9819-a618b0e76d61" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "using MTH229\n", "using Plots\n", "plotly()" ], "id": "093aaefe" }, { "cell_type": "markdown", "metadata": {}, "source": [ "------------------------------------------------------------------------\n", "\n", "The `Plots` package brings in a `plot` function that makes plotting\n", "functions as easy as specifying a function object and the $x$ domain to\n", "plot over:" ], "id": "01368a9f-5dd5-45d0-b0ee-ce0dba42c243" }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/html": [ "" ] } } ], "source": [ "f(x) = sin(x^2)\n", "plot(f, 0, 2pi)" ], "id": "fdd4254e" }, { "cell_type": "markdown", "metadata": {}, "source": [ "Often most of the battle is *judiciously* choosing the range of values,\n", "$[a,b]$, so that the graph highlights a feature of interest: for example\n", "a relative maximum or minimum, a zero, a vertical asymptote, a\n", "horizontal asymptote, a slant asymptote…\n", "\n", "The use of a function as an argument is not something done with a\n", "calculator, but is very useful when using `Julia` for calculus, as many\n", "actions may be viewed as operating on the function $f$, not the values\n", "of the function, $f(x)$.\n", "\n", "### Backends\n", "\n", "The `Plots` package has two backends installed with it, and others are\n", "available. The `gr()` backend is loaded by default. To try an\n", "*interactive* backend using web technologies, issue the command\n", "`plotly()`.\n", "\n", "------------------------------------------------------------------------\n", "\n", "More than one function can be plotted on a graph. The `plot!` function\n", "makes this easy: make the first plot with `plot` and any additional ones\n", "with `plot!`. For example:" ], "id": "542f59a8-a4e8-4de4-bbca-8a8e302cb7a7" }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/html": [ "" ] } } ], "source": [ "plot(sin, 0, 2pi)\n", "plot!(cos)\n", "plot!(zero)" ], "id": "0cdd1e27" }, { "cell_type": "markdown", "metadata": {}, "source": [ "The legend can be suppressed by specifying `legend=false` to the first\n", "plot; e.g. `plot(sin,0, 2pi, legend=false)`. There is not need to add\n", "limits to the `plot!` command if the existing viewing window, $[a,b]$,\n", "is desired.\n", "\n", "(A convention in `Julia` is to use a trailing `!` in a function name to\n", "indicate that the function will modify an existing object. In this case\n", "`plot!` modifies the existing plot, which is implicitly known to the\n", "function.)\n", "\n", "------------------------------------------------------------------------\n", "\n", "A plot is nothing more than a connect-the-dot graph of paired $x$ and\n", "$y$ values. It can be useful to know how to do the steps. The above\n", "graph of `sin` could be done with:" ], "id": "097985c4-54eb-4862-9de2-d3e783ed9668" }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/html": [ "" ] } } ], "source": [ "a, b = 0, 2pi\n", "xs = range(a, b, length=50) # 50 points between a and b\n", "ys = sin.(xs) # or ys = [sin(x) for x in xs]\n", "plot(xs, ys)" ], "id": "21917b6a" }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `xs` and `ys` are written as though they are “plural” because these\n", "variables contain 50 values each in a container. The function call\n", "`sin.(xs)` adds a “dot” to the parentheses. This syntax instructs\n", "`Julia` to “broadcast” `sin` to *each* value in the container `xs`.\n", "\n", "The plot “recipe” for plotting functions provided by `Plots` does a bit\n", "more than this, as it adaptively specifies the points to plot.\n", "\n", "The `scatter!` function can be used to add points to a graph. These may\n", "be specified as collections of `x` and `y` values, as above.\n", "\n", "## NaN values.\n", "\n", "The value `NaN` is a floating point value that arises during some\n", "indeterminate operations, such as `0/0`. The `plot` function will stop\n", "connecting the dots when it encounters an `NaN` value. This convention\n", "can be gainfully employed to introduce breaks, or discontinuities, in a\n", "graph.\n", "\n", "## Mapping a function over a collection\n", "\n", "In `Julia` a collection of values can be made by combining them with\n", "square brackets, as in `[1,1,2,3,5]`. The square brackets use a vector\n", "for the enclosing container. Parentheses also combine values using a\n", "“tuple.”\n", "\n", "For reqular patterns the colon operator can be used: `1:5` is\n", "essentially `[1,2,3,4,5]`. If the gap between succesive numbers is not\n", "`1`, but, say, `h`, it can be set with the syntax `a:h:b`.\n", "\n", "The `range` function is also used to specify regular patterns, as in:\n", "`range(a, b, n)`, which specified `n` evenly spaced points from `a` to\n", "`b`. Using the colon syntax or the `range` function only creates a means\n", "to generate values, rather than the values themselves.\n", "\n", "Collections of values are useful for many reasons. We will see later how\n", "they are used to store the zeros of a function.\n", "\n", "`Julia` has a few different ways of applying a function to each value in\n", "a collection: the `map` function, called as `map(f, collection)`, a\n", "comprehension, created as `[f(x) for x in collection]`, but we will\n", "typically use the *broadcasting* syntax where a “dot” is inserted\n", "between the function name and the parentheses for calling the function.\n", "(E.g., `f.(xs)`.)\n", "\n", "For example, here is *one* way to the create values\n", "$1/10, 1/100, \\dots, 1/10^5$:" ], "id": "dcd440d2-4fb6-4b3b-bf83-089198d6bc0d" }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "5-element Vector{Float64}:\n", " 0.1\n", " 0.01\n", " 0.001\n", " 0.0001\n", " 1.0e-5" ] } } ], "source": [ "ns = 1:5\n", "f(n) = 1/10^n\n", "f.(ns)" ], "id": "b0b79893" }, { "cell_type": "markdown", "metadata": {}, "source": [ "------------------------------------------------------------------------" ], "id": "26df25b5-9442-44ca-8a4d-22e7348baf78" }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# your commands go here" ], "id": "f6a9b6e5" } ], "nbformat": 4, "nbformat_minor": 5, "metadata": { "kernelspec": { "name": "julia-1.11", "display_name": "Julia 1.11.2", "language": "julia", "path": "/Users/jverzani/Library/Jupyter/kernels/julia-1.11" }, "language_info": { "name": "julia", "file_extension": ".jl", "mimetype": "application/julia", "version": "1.11.2" } } }