{ "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", "\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.\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": "a554a335-7b73-40a4-a637-3b94f54265f4" }, { "cell_type": "code", "execution_count": 0, "metadata": {}, "outputs": [], "source": [ "using MTH229\n", "using Plots\n", "plotly()" ], "id": "2" }, { "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": "7a5d2f5e-2d66-4dc3-a53e-a0ab647e67b6" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/html": [ "" ] } } ], "source": [ "f(x) = sin(x^2)\n", "plot(f, 0, 2pi)" ], "id": "6" }, { "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": "0e75fa84-af28-4fc0-a317-9c0007abcc5e" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/html": [ "" ] } } ], "source": [ "plot(sin, 0, 2pi)\n", "plot!(cos)\n", "plot!(zero)" ], "id": "8" }, { "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)`.\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": "be454374-075a-44b6-9d00-d4c253e19697" }, { "cell_type": "code", "execution_count": 1, "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": "10" }, { "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 (a vector in this case).\n", "The function call `sin.(xs)` adds a “dot” to the parentheses. This\n", "syntax instructs `Julia` to “broadcast” `sin` to *each* value in the\n", "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 are\n", "specified as collections of `x` and `y` values.\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 in a 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` points from `a` to `b`. Using the\n", "colon syntax or the `range` function only creates a means to generate\n", "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": "a99b2f70-398b-4606-afc1-3d323b903a41" }, { "cell_type": "code", "execution_count": 1, "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": "12" }, { "cell_type": "markdown", "metadata": {}, "source": [ "------------------------------------------------------------------------" ], "id": "6006d0cb-455e-40ca-b7eb-634d4f10197a" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# your commands go here\n" ], "id": "14" } ], "nbformat": 4, "nbformat_minor": 5, "metadata": { "kernel_info": { "name": "julia" }, "kernelspec": { "name": "julia", "display_name": "Julia", "language": "julia" }, "language_info": { "name": "julia", "codemirror_mode": "julia", "version": "1.10.0" } } }