{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "1ce5d98c-4cc0-421b-9b20-a23f21ea83ab",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"v\"1.8.0-DEV.71\""
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"VERSION"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "176b7c26-356c-4c42-a3dd-2ff2b302cffa",
"metadata": {},
"outputs": [],
"source": [
"using Plots\n",
"using Zygote"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b011345a-dbf5-4faa-b9d3-1e0157eb80c2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3×4 Matrix{Int64}:\n",
" 1 2 3 4\n",
" 1 2 3 4\n",
" 1 2 3 4"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"3×4 Matrix{Int64}:\n",
" 10 10 10 10\n",
" 20 20 20 20\n",
" 30 30 30 30"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"meshgrid(x, y) = reim(complex.(x', y))\n",
"\n",
"x = 1:4\n",
"y = 10(1:3)\n",
"X, Y = meshgrid(x, y)\n",
"display(X)\n",
"display(Y)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "5237ac47-f429-40f2-9887-6c621a2bbfca",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"plotvf (generic function with 1 method)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function plotvf!(x, y, f; scale=1, kwargs...)\n",
" X, Y = meshgrid(x, y)\n",
" u(x, y) = scale * f(x, y)[1]\n",
" v(x, y) = scale * f(x, y)[2]\n",
" U = u.(X, Y)\n",
" V = v.(X, Y)\n",
" X -= U/2\n",
" Y -= V/2\n",
" quiver!(vec(X), vec(Y); quiver = (vec(U), vec(V)), kwargs...)\n",
"end\n",
"\n",
"plotvf(x, y, f; kwargs...) = (plot(); plotvf!(x, y, f; kwargs...))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "b69f5603-e214-4e47-8bee-b37d0a9786af",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g(x, y) = (-y, x)\n",
"x = y = range(-2, 2, length=11)\n",
"plotvf(x, y, g; scale=0.2, size=(400, 400))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "0f1ec452-117f-4ad7-b9af-8f0cf327bf3e",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f(x, y) = x^3 - 3x + y^2\n",
"df(x, y) = gradient(f, x, y)\n",
"xs = ys = range(-2, 2, length=101)\n",
"heatmap(xs, ys, f; color=:rainbow)\n",
"x = y = range(-2, 2, length=21)[2:2:end]\n",
"plotvf!(x, y, df; scale=0.05, color=:white)\n",
"plot!(xlim=extrema(xs), ylim=extrema(ys))"
]
},
{
"cell_type": "markdown",
"id": "23e48b8c-ac4f-445a-a585-ba5dce75b4b9",
"metadata": {},
"source": [
"For Plots.@recipe, see\n",
"\n",
"* https://docs.juliaplots.org/latest/recipes/\n",
"* https://github.com/JuliaPlots/ExamplePlots.jl/blob/master/notebooks/usertype_recipes.ipynb\n",
"* https://github.com/JuliaPlots/ExamplePlots.jl/blob/master/notebooks/type_recipes.ipynb\n",
"* https://github.com/JuliaPlots/ExamplePlots.jl/blob/master/notebooks/series_recipes.ipynb\n",
"* https://github.com/JuliaPlots/Plots.jl/blob/master/src/recipes.jl\n",
"* https://nbviewer.jupyter.org/gist/genkuroki/521c4bf9160caae8f8c6591e78a9f1d1"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "92f102f6-16d2-4d28-9746-2a498d45b945",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Main.O"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"module O\n",
"\n",
"using Plots\n",
"meshgrid(x, y) = reim(complex.(x', y))\n",
"\n",
"struct VectorField{X, Y, F, S} x::X; y::Y; f::F; scale::S end\n",
"VectorField(x, y, f; scale=0.2) = VectorField(x, y, f, scale)\n",
"\n",
"@recipe function F(vf::VectorField)\n",
" x, y, f, scale = vf.x, vf.y, vf.f, vf.scale\n",
" X, Y = meshgrid(x, y)\n",
" u(x, y) = scale * f(x, y)[1]\n",
" v(x, y) = scale * f(x, y)[2]\n",
" U = u.(X, Y)\n",
" V = v.(X, Y)\n",
" X -= U/2\n",
" Y -= V/2\n",
" \n",
" seriestype := :quiver\n",
" quiver --> (vec(U), vec(V))\n",
" (vec(X), vec(Y))\n",
"end\n",
"\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "10f3de9c-5011-4357-b429-5b7f75af892d",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = y = range(-2, 2, length=11)\n",
"g(x, y) = (-y, x)\n",
"plot(O.VectorField(x, y, g); size=(400, 400))"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "42b0c678-d17d-436c-89af-ca7576c633da",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f(x, y) = x^3 - 3x + y^2\n",
"df(x, y) = gradient(f, x, y)\n",
"xs = ys = range(-2, 2, length=101)\n",
"heatmap(xs, ys, f; color=:rainbow)\n",
"x = y = range(-2, 2, length=21)[2:2:end]\n",
"plot!(O.VectorField(x, y, df, 0.05); color=:white)\n",
"plot!(xlim=extrema(xs), ylim=extrema(ys))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c12b9d8f-4e90-458f-84d0-1f364b09f5a9",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"jupytext": {
"formats": "ipynb,jl:hydrogen"
},
"kernelspec": {
"display_name": "Julia 1.8.0-DEV",
"language": "julia",
"name": "julia-1.8"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.8.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}