├── .gitignore ├── Lecture 1 ├── Intro.pdf └── Lecture 1.pdf ├── Lecture 10 ├── Lecture 10.pdf ├── Manifest.toml ├── Project.toml ├── minimization.ipynb └── root-finding.ipynb ├── Lecture 11 ├── Lecture 11.pdf ├── Manifest.toml ├── Project.toml └── equality-constraints.ipynb ├── Lecture 12 └── Lecture 12.pdf ├── Lecture 13 ├── Lecture 13.pdf ├── Manifest.toml ├── Project.toml └── cable.ipynb ├── Lecture 14 └── Lecture 14.pdf ├── Lecture 15 ├── Lecture 15.pdf ├── Manifest.toml ├── Project.toml └── pendulum-constraints.ipynb ├── Lecture 16 ├── Lecture 16.pdf ├── Manifest.toml ├── Project.toml └── pendulum-variational.ipynb ├── Lecture 17 ├── Lecture 17.pdf ├── Manifest.toml ├── Project.toml ├── pendulum-damped.ipynb ├── pendulum-legendre.ipynb └── variational-constraints.ipynb ├── Lecture 18 ├── Lecture 18.pdf ├── Manifest.toml ├── Project.toml └── falling-particle.ipynb ├── Lecture 19 ├── Lecture 19.pdf ├── Manifest.toml ├── Project.toml └── falling-particle-friction.ipynb ├── Lecture 2 ├── Lecture 2.pdf ├── Manifest.toml ├── Project.toml └── pendulum.ipynb ├── Lecture 20 ├── Lecture 20.pdf ├── Manifest.toml ├── Project.toml └── double-pendulum.ipynb ├── Lecture 21 ├── Lecture 21.pdf ├── Manifest.toml ├── Project.toml └── floating-2bar.ipynb ├── Lecture 22 ├── Lecture 22.pdf ├── Manifest.toml ├── Project.toml └── floating-2bar-max.ipynb ├── Lecture 23 └── Lecture 23.pdf ├── Lecture 3 ├── Lecture 3.pdf ├── Manifest.toml ├── Project.toml └── pendulum-rk2.ipynb ├── Lecture 4 ├── Lecture 4.pdf ├── Manifest.toml ├── Project.toml └── damped-pendulum.ipynb ├── Lecture 5 └── Lecture 5.pdf ├── Lecture 6 └── Lecture 6.pdf ├── Lecture 7 └── Lecture 7.pdf ├── Lecture 8 └── Lecture 8.pdf ├── Lecture 9 └── Lecture 9.pdf └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | .DS_Store 3 | 4 | Lecture 2/.ipynb_checkpoints/ 5 | 6 | Lecture 3/.ipynb_checkpoints/ 7 | 8 | Lecture 4/.ipynb_checkpoints/damped-pendulum-checkpoint.ipynb 9 | 10 | Lecture 10/.ipynb_checkpoints/ 11 | 12 | Lecture 13/.ipynb_checkpoints/ 13 | 14 | Lecture 17/.ipynb_checkpoints/ 15 | 16 | Lecture 18/.ipynb_checkpoints/ 17 | 18 | Lecture 19/.ipynb_checkpoints/ 19 | 20 | Lecture 20/.ipynb_checkpoints/ 21 | 22 | Lecture 21/.ipynb_checkpoints/ 23 | 24 | Lecture 22/.ipynb_checkpoints/ 25 | -------------------------------------------------------------------------------- /Lecture 1/Intro.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamics-simulation-16-715/lecture-notebooks/a2f7642b3ba31629fc15ca77b738e3f21a5e6c8c/Lecture 1/Intro.pdf -------------------------------------------------------------------------------- /Lecture 1/Lecture 1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamics-simulation-16-715/lecture-notebooks/a2f7642b3ba31629fc15ca77b738e3f21a5e6c8c/Lecture 1/Lecture 1.pdf -------------------------------------------------------------------------------- /Lecture 10/Lecture 10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamics-simulation-16-715/lecture-notebooks/a2f7642b3ba31629fc15ca77b738e3f21a5e6c8c/Lecture 10/Lecture 10.pdf -------------------------------------------------------------------------------- /Lecture 10/Manifest.toml: -------------------------------------------------------------------------------- 1 | # This file is machine-generated - editing it directly is not advised 2 | 3 | -------------------------------------------------------------------------------- /Lecture 10/Project.toml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamics-simulation-16-715/lecture-notebooks/a2f7642b3ba31629fc15ca77b738e3f21a5e6c8c/Lecture 10/Project.toml -------------------------------------------------------------------------------- /Lecture 10/minimization.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import Pkg; Pkg.activate(@__DIR__); Pkg.instantiate()" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "using LinearAlgebra\n", 19 | "using ForwardDiff\n", 20 | "using PyPlot" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": null, 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "function f(x)\n", 30 | " return x.^4 + x.^3 - x.^2 - x\n", 31 | "end" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": null, 37 | "metadata": {}, 38 | "outputs": [], 39 | "source": [ 40 | "function ∇f(x)\n", 41 | " return 4.0*x.^3 + 3.0*x.^2 - 2.0*x - 1.0\n", 42 | "end" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "function ∇2f(x)\n", 52 | " return 12.0*x.^2 + 6.0*x - 2.0\n", 53 | "end" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": null, 59 | "metadata": {}, 60 | "outputs": [], 61 | "source": [ 62 | "x = LinRange(-1.75,1.25,1000)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "p = plot(x,f(x))" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": null, 77 | "metadata": {}, 78 | "outputs": [], 79 | "source": [ 80 | "function newton_step(x0)\n", 81 | " xn = x0 - ∇2f(x0)\\∇f(x0)\n", 82 | "end" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": null, 88 | "metadata": {}, 89 | "outputs": [], 90 | "source": [ 91 | "xguess = 0.0\n", 92 | "plot(x, f(x))\n", 93 | "plot(xguess, f(xguess), \"rx\")" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": null, 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "xnew = newton_step(xguess[end])\n", 103 | "xguess = [xguess xnew]\n", 104 | "plot(x, f(x))\n", 105 | "plot(xguess, f(xguess), \"rx\")" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": null, 111 | "metadata": {}, 112 | "outputs": [], 113 | "source": [] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": null, 118 | "metadata": {}, 119 | "outputs": [], 120 | "source": [ 121 | "∇2f(0.0)" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": null, 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [ 130 | "function regularized_newton_step(x0)\n", 131 | " β = 1.0\n", 132 | " H = ∇2f(x0)\n", 133 | " while !isposdef(H)\n", 134 | " H = H + β*I\n", 135 | " end\n", 136 | " xn = x0 - H\\∇f(x0)\n", 137 | "end" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": null, 143 | "metadata": {}, 144 | "outputs": [], 145 | "source": [ 146 | "xguess = 0.0\n", 147 | "plot(x, f(x))\n", 148 | "plot(xguess, f(xguess), \"rx\")" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": null, 154 | "metadata": {}, 155 | "outputs": [], 156 | "source": [ 157 | "xnew = regularized_newton_step(xguess[end])\n", 158 | "xguess = [xguess xnew]\n", 159 | "plot(x, f(x))\n", 160 | "plot(xguess, f(xguess), \"rx\")" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": null, 166 | "metadata": {}, 167 | "outputs": [], 168 | "source": [] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": null, 173 | "metadata": {}, 174 | "outputs": [], 175 | "source": [ 176 | "function backtracking_regularized_newton_step(x0)\n", 177 | " b = 0.1\n", 178 | " c = 0.5\n", 179 | " β = 1.0\n", 180 | " H = ∇2f(x0)\n", 181 | " while !isposdef(H)\n", 182 | " H = H + β*I\n", 183 | " end\n", 184 | " Δx = -H\\∇f(x0)\n", 185 | " \n", 186 | " α = 1.0\n", 187 | " while f(x0 + α*Δx) > f(x0) + b*α*∇f(x0)\n", 188 | " α = c*α\n", 189 | " end\n", 190 | " \n", 191 | " xn = x0 + α*Δx\n", 192 | "end" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": null, 198 | "metadata": {}, 199 | "outputs": [], 200 | "source": [ 201 | "xguess = 0.0\n", 202 | "plot(x, f(x))\n", 203 | "plot(xguess, f(xguess), \"rx\")" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": null, 209 | "metadata": {}, 210 | "outputs": [], 211 | "source": [ 212 | "xnew = backtracking_regularized_newton_step(xguess[end])\n", 213 | "xguess = [xguess xnew]\n", 214 | "plot(x, f(x))\n", 215 | "plot(xguess, f(xguess), \"rx\")" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": null, 221 | "metadata": {}, 222 | "outputs": [], 223 | "source": [] 224 | } 225 | ], 226 | "metadata": { 227 | "kernelspec": { 228 | "display_name": "Julia 1.6.7", 229 | "language": "julia", 230 | "name": "julia-1.6" 231 | }, 232 | "language_info": { 233 | "file_extension": ".jl", 234 | "mimetype": "application/julia", 235 | "name": "julia", 236 | "version": "1.6.7" 237 | } 238 | }, 239 | "nbformat": 4, 240 | "nbformat_minor": 4 241 | } 242 | -------------------------------------------------------------------------------- /Lecture 10/root-finding.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import Pkg; Pkg.activate(@__DIR__); Pkg.instantiate()" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "using LinearAlgebra\n", 19 | "using ForwardDiff\n", 20 | "using PyPlot" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": null, 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "function pendulum_dynamics(x)\n", 30 | " l = 1.0\n", 31 | " g = 9.81\n", 32 | " \n", 33 | " θ = x[1]\n", 34 | " θ̇ = x[2]\n", 35 | " \n", 36 | " θ̈ = -(g/l)*sin(θ)\n", 37 | " \n", 38 | " return [θ̇; θ̈]\n", 39 | "end" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": null, 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "function backward_euler_step_fixed_point(fun, x0, h)\n", 49 | " xn = x0\n", 50 | " e = [norm(x0 + h.*fun(xn) - xn)]\n", 51 | " while e[end] > 1e-8\n", 52 | " xn = x0 + h.*fun(xn)\n", 53 | " e = [e; norm(x0 + h.*fun(xn) - xn)]\n", 54 | " end\n", 55 | " \n", 56 | " return xn, e\n", 57 | "end" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": null, 63 | "metadata": {}, 64 | "outputs": [], 65 | "source": [ 66 | "function backward_euler_step_newton(fun, x0, h)\n", 67 | " xn = x0\n", 68 | " r = x0 + h.*fun(xn) - xn\n", 69 | " e = [norm(r)]\n", 70 | " while e[end] > 1e-8\n", 71 | " ∂r = ForwardDiff.jacobian(x -> x0 + h.*fun(x) - x, xn)\n", 72 | " xn = xn - ∂r\\r\n", 73 | " r = x0 + h.*fun(xn) - xn\n", 74 | " e = [e; norm(r)]\n", 75 | " end\n", 76 | " \n", 77 | " return xn, e\n", 78 | "end" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": null, 84 | "metadata": {}, 85 | "outputs": [], 86 | "source": [ 87 | "function backward_euler_fixed_point(fun, x0, Tf, h)\n", 88 | " t = Array(range(0,Tf,step=h))\n", 89 | " \n", 90 | " x_hist = zeros(length(x0),length(t))\n", 91 | " x_hist[:,1] .= x0\n", 92 | " \n", 93 | " for k = 1:(length(t)-1)\n", 94 | " x_hist[:,k+1], e = backward_euler_step_fixed_point(fun, x_hist[:,k], h)\n", 95 | " end\n", 96 | " \n", 97 | " return x_hist, t\n", 98 | "end" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": null, 104 | "metadata": {}, 105 | "outputs": [], 106 | "source": [ 107 | "function backward_euler_newton(fun, x0, Tf, h)\n", 108 | " t = Array(range(0,Tf,step=h))\n", 109 | " \n", 110 | " x_hist = zeros(length(x0),length(t))\n", 111 | " x_hist[:,1] .= x0\n", 112 | " \n", 113 | " for k = 1:(length(t)-1)\n", 114 | " x_hist[:,k+1], e = backward_euler_step_newton(fun, x_hist[:,k], h)\n", 115 | " end\n", 116 | " \n", 117 | " return x_hist, t\n", 118 | "end" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": null, 124 | "metadata": {}, 125 | "outputs": [], 126 | "source": [ 127 | "x0 = [.1; 0]\n", 128 | "x_hist1, t_hist1 = backward_euler_fixed_point(pendulum_dynamics, x0, 10, 0.01)\n", 129 | "x_hist2, t_hist2 = backward_euler_newton(pendulum_dynamics, x0, 10, 0.01)\n", 130 | "plot(t_hist1, x_hist1[1,:])\n", 131 | "plot(t_hist2, x_hist2[1,:])" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": null, 137 | "metadata": {}, 138 | "outputs": [], 139 | "source": [ 140 | "max(abs.(x_hist1-x_hist2)...)" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": null, 146 | "metadata": {}, 147 | "outputs": [], 148 | "source": [ 149 | "xn, e1 = backward_euler_step_fixed_point(pendulum_dynamics, x0, 0.1)\n", 150 | "e1" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": null, 156 | "metadata": {}, 157 | "outputs": [], 158 | "source": [ 159 | "xn, e2 = backward_euler_step_newton(pendulum_dynamics, x0, 0.1)\n", 160 | "e2" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": null, 166 | "metadata": {}, 167 | "outputs": [], 168 | "source": [ 169 | "semilogy(e1)\n", 170 | "semilogy(e2)" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": null, 176 | "metadata": {}, 177 | "outputs": [], 178 | "source": [] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": null, 183 | "metadata": {}, 184 | "outputs": [], 185 | "source": [] 186 | } 187 | ], 188 | "metadata": { 189 | "kernelspec": { 190 | "display_name": "Julia 1.6.7", 191 | "language": "julia", 192 | "name": "julia-1.6" 193 | }, 194 | "language_info": { 195 | "file_extension": ".jl", 196 | "mimetype": "application/julia", 197 | "name": "julia", 198 | "version": "1.6.7" 199 | } 200 | }, 201 | "nbformat": 4, 202 | "nbformat_minor": 4 203 | } 204 | -------------------------------------------------------------------------------- /Lecture 11/Lecture 11.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamics-simulation-16-715/lecture-notebooks/a2f7642b3ba31629fc15ca77b738e3f21a5e6c8c/Lecture 11/Lecture 11.pdf -------------------------------------------------------------------------------- /Lecture 11/Manifest.toml: -------------------------------------------------------------------------------- 1 | # This file is machine-generated - editing it directly is not advised 2 | 3 | [[ArgTools]] 4 | uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f" 5 | 6 | [[Artifacts]] 7 | uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33" 8 | 9 | [[Base64]] 10 | uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" 11 | 12 | [[ChainRulesCore]] 13 | deps = ["Compat", "LinearAlgebra", "SparseArrays"] 14 | git-tree-sha1 = "54fc4400de6e5c3e27be6047da2ef6ba355511f8" 15 | uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" 16 | version = "1.11.6" 17 | 18 | [[ChangesOfVariables]] 19 | deps = ["ChainRulesCore", "LinearAlgebra", "Test"] 20 | git-tree-sha1 = "bf98fa45a0a4cee295de98d4c1462be26345b9a1" 21 | uuid = "9e997f8a-9a97-42d5-a9f1-ce6bfc15e2c0" 22 | version = "0.1.2" 23 | 24 | [[CommonSubexpressions]] 25 | deps = ["MacroTools", "Test"] 26 | git-tree-sha1 = "7b8a93dba8af7e3b42fecabf646260105ac373f7" 27 | uuid = "bbf7d656-a473-5ed7-a52c-81e309532950" 28 | version = "0.3.0" 29 | 30 | [[Compat]] 31 | deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"] 32 | git-tree-sha1 = "44c37b4636bc54afac5c574d2d02b625349d6582" 33 | uuid = "34da2185-b29b-5c13-b0c7-acf172513d20" 34 | version = "3.41.0" 35 | 36 | [[CompilerSupportLibraries_jll]] 37 | deps = ["Artifacts", "Libdl"] 38 | uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae" 39 | 40 | [[Dates]] 41 | deps = ["Printf"] 42 | uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" 43 | 44 | [[DelimitedFiles]] 45 | deps = ["Mmap"] 46 | uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab" 47 | 48 | [[DiffResults]] 49 | deps = ["StaticArrays"] 50 | git-tree-sha1 = "c18e98cba888c6c25d1c3b048e4b3380ca956805" 51 | uuid = "163ba53b-c6d8-5494-b064-1a9d43ac40c5" 52 | version = "1.0.3" 53 | 54 | [[DiffRules]] 55 | deps = ["LogExpFunctions", "NaNMath", "Random", "SpecialFunctions"] 56 | git-tree-sha1 = "9bc5dac3c8b6706b58ad5ce24cffd9861f07c94f" 57 | uuid = "b552c78f-8df3-52c6-915a-8e097449b14b" 58 | version = "1.9.0" 59 | 60 | [[Distributed]] 61 | deps = ["Random", "Serialization", "Sockets"] 62 | uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b" 63 | 64 | [[DocStringExtensions]] 65 | deps = ["LibGit2"] 66 | git-tree-sha1 = "b19534d1895d702889b219c382a6e18010797f0b" 67 | uuid = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" 68 | version = "0.8.6" 69 | 70 | [[Downloads]] 71 | deps = ["ArgTools", "LibCURL", "NetworkOptions"] 72 | uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6" 73 | 74 | [[ForwardDiff]] 75 | deps = ["CommonSubexpressions", "DiffResults", "DiffRules", "LinearAlgebra", "LogExpFunctions", "NaNMath", "Preferences", "Printf", "Random", "SpecialFunctions", "StaticArrays"] 76 | git-tree-sha1 = "1bd6fc0c344fc0cbee1f42f8d2e7ec8253dda2d2" 77 | uuid = "f6369f11-7733-5829-9624-2563aa707210" 78 | version = "0.10.25" 79 | 80 | [[InteractiveUtils]] 81 | deps = ["Markdown"] 82 | uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" 83 | 84 | [[InverseFunctions]] 85 | deps = ["Test"] 86 | git-tree-sha1 = "a7254c0acd8e62f1ac75ad24d5db43f5f19f3c65" 87 | uuid = "3587e190-3f89-42d0-90ee-14403ec27112" 88 | version = "0.1.2" 89 | 90 | [[IrrationalConstants]] 91 | git-tree-sha1 = "7fd44fd4ff43fc60815f8e764c0f352b83c49151" 92 | uuid = "92d709cd-6900-40b7-9082-c6be49f344b6" 93 | version = "0.1.1" 94 | 95 | [[JLLWrappers]] 96 | deps = ["Preferences"] 97 | git-tree-sha1 = "22df5b96feef82434b07327e2d3c770a9b21e023" 98 | uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210" 99 | version = "1.4.0" 100 | 101 | [[LibCURL]] 102 | deps = ["LibCURL_jll", "MozillaCACerts_jll"] 103 | uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21" 104 | 105 | [[LibCURL_jll]] 106 | deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"] 107 | uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0" 108 | 109 | [[LibGit2]] 110 | deps = ["Base64", "NetworkOptions", "Printf", "SHA"] 111 | uuid = "76f85450-5226-5b5a-8eaa-529ad045b433" 112 | 113 | [[LibSSH2_jll]] 114 | deps = ["Artifacts", "Libdl", "MbedTLS_jll"] 115 | uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8" 116 | 117 | [[Libdl]] 118 | uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" 119 | 120 | [[LinearAlgebra]] 121 | deps = ["Libdl"] 122 | uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" 123 | 124 | [[LogExpFunctions]] 125 | deps = ["ChainRulesCore", "ChangesOfVariables", "DocStringExtensions", "InverseFunctions", "IrrationalConstants", "LinearAlgebra"] 126 | git-tree-sha1 = "e5718a00af0ab9756305a0392832c8952c7426c1" 127 | uuid = "2ab3a3ac-af41-5b50-aa03-7779005ae688" 128 | version = "0.3.6" 129 | 130 | [[Logging]] 131 | uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" 132 | 133 | [[MacroTools]] 134 | deps = ["Markdown", "Random"] 135 | git-tree-sha1 = "3d3e902b31198a27340d0bf00d6ac452866021cf" 136 | uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" 137 | version = "0.5.9" 138 | 139 | [[Markdown]] 140 | deps = ["Base64"] 141 | uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" 142 | 143 | [[MbedTLS_jll]] 144 | deps = ["Artifacts", "Libdl"] 145 | uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1" 146 | 147 | [[Mmap]] 148 | uuid = "a63ad114-7e13-5084-954f-fe012c677804" 149 | 150 | [[MozillaCACerts_jll]] 151 | uuid = "14a3606d-f60d-562e-9121-12d972cd8159" 152 | 153 | [[NaNMath]] 154 | git-tree-sha1 = "f755f36b19a5116bb580de457cda0c140153f283" 155 | uuid = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3" 156 | version = "0.3.6" 157 | 158 | [[NetworkOptions]] 159 | uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908" 160 | 161 | [[OpenLibm_jll]] 162 | deps = ["Artifacts", "Libdl"] 163 | uuid = "05823500-19ac-5b8b-9628-191a04bc5112" 164 | 165 | [[OpenSpecFun_jll]] 166 | deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "Pkg"] 167 | git-tree-sha1 = "13652491f6856acfd2db29360e1bbcd4565d04f1" 168 | uuid = "efe28fd5-8261-553b-a9e1-b2916fc3738e" 169 | version = "0.5.5+0" 170 | 171 | [[Pkg]] 172 | deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"] 173 | uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" 174 | 175 | [[Preferences]] 176 | deps = ["TOML"] 177 | git-tree-sha1 = "2cf929d64681236a2e074ffafb8d568733d2e6af" 178 | uuid = "21216c6a-2e73-6563-6e65-726566657250" 179 | version = "1.2.3" 180 | 181 | [[Printf]] 182 | deps = ["Unicode"] 183 | uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" 184 | 185 | [[REPL]] 186 | deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"] 187 | uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" 188 | 189 | [[Random]] 190 | deps = ["Serialization"] 191 | uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" 192 | 193 | [[SHA]] 194 | uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" 195 | 196 | [[Serialization]] 197 | uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" 198 | 199 | [[SharedArrays]] 200 | deps = ["Distributed", "Mmap", "Random", "Serialization"] 201 | uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383" 202 | 203 | [[Sockets]] 204 | uuid = "6462fe0b-24de-5631-8697-dd941f90decc" 205 | 206 | [[SparseArrays]] 207 | deps = ["LinearAlgebra", "Random"] 208 | uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" 209 | 210 | [[SpecialFunctions]] 211 | deps = ["ChainRulesCore", "IrrationalConstants", "LogExpFunctions", "OpenLibm_jll", "OpenSpecFun_jll"] 212 | git-tree-sha1 = "e6bf188613555c78062842777b116905a9f9dd49" 213 | uuid = "276daf66-3868-5448-9aa4-cd146d93841b" 214 | version = "2.1.0" 215 | 216 | [[StaticArrays]] 217 | deps = ["LinearAlgebra", "Random", "Statistics"] 218 | git-tree-sha1 = "2884859916598f974858ff01df7dfc6c708dd895" 219 | uuid = "90137ffa-7385-5640-81b9-e52037218182" 220 | version = "1.3.3" 221 | 222 | [[Statistics]] 223 | deps = ["LinearAlgebra", "SparseArrays"] 224 | uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" 225 | 226 | [[TOML]] 227 | deps = ["Dates"] 228 | uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76" 229 | 230 | [[Tar]] 231 | deps = ["ArgTools", "SHA"] 232 | uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e" 233 | 234 | [[Test]] 235 | deps = ["InteractiveUtils", "Logging", "Random", "Serialization"] 236 | uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" 237 | 238 | [[UUIDs]] 239 | deps = ["Random", "SHA"] 240 | uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" 241 | 242 | [[Unicode]] 243 | uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" 244 | 245 | [[Zlib_jll]] 246 | deps = ["Libdl"] 247 | uuid = "83775a58-1f1d-513f-b197-d71354ab007a" 248 | 249 | [[nghttp2_jll]] 250 | deps = ["Artifacts", "Libdl"] 251 | uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d" 252 | 253 | [[p7zip_jll]] 254 | deps = ["Artifacts", "Libdl"] 255 | uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0" 256 | -------------------------------------------------------------------------------- /Lecture 11/Project.toml: -------------------------------------------------------------------------------- 1 | [deps] 2 | ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" 3 | -------------------------------------------------------------------------------- /Lecture 11/equality-constraints.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import Pkg; Pkg.activate(@__DIR__); Pkg.instantiate()" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "using LinearAlgebra\n", 19 | "using ForwardDiff\n", 20 | "using PyPlot" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": null, 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "Q = Diagonal([0.5; 1])\n", 30 | "function f(x)\n", 31 | " return 0.5*(x-[1; 0])'*Q*(x-[1; 0])\n", 32 | "end\n", 33 | "function ∇f(x)\n", 34 | " return Q*(x-[1; 0])\n", 35 | "end\n", 36 | "function ∇2f(x)\n", 37 | " return Q\n", 38 | "end" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": null, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "function c(x)\n", 48 | " return x[1]^2 + 2*x[1] - x[2]\n", 49 | "end\n", 50 | "function ∂c(x)\n", 51 | " return [2*x[1]+2 -1]\n", 52 | "end" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": null, 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [ 61 | "function plot_landscape()\n", 62 | " Nsamp = 20\n", 63 | " Xsamp = kron(ones(Nsamp),LinRange(-4,4,Nsamp)')\n", 64 | " Ysamp = kron(ones(Nsamp)',LinRange(-4,4,Nsamp))\n", 65 | " Zsamp = zeros(Nsamp,Nsamp)\n", 66 | " for j = 1:Nsamp\n", 67 | " for k = 1:Nsamp\n", 68 | " Zsamp[j,k] = f([Xsamp[j,k]; Ysamp[j,k]])\n", 69 | " end\n", 70 | " end\n", 71 | " contour(Xsamp,Ysamp,Zsamp)\n", 72 | "\n", 73 | " xc = LinRange(-3.2,1.2,Nsamp)\n", 74 | " plot(xc,xc.^2+2.0.*xc,\"y\")\n", 75 | "end\n", 76 | "\n", 77 | "plot_landscape()" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "function newton_step(x0,λ0)\n", 87 | " H = ∇2f(x0) + ForwardDiff.jacobian(x -> ∂c(x)'*λ0, x0)\n", 88 | " C = ∂c(x0)\n", 89 | " Δz = [H C'; C 0]\\[-∇f(x0)-C'*λ0; -c(x0)]\n", 90 | " Δx = Δz[1:2]\n", 91 | " Δλ = Δz[3]\n", 92 | " return x0+Δx, λ0+Δλ\n", 93 | "end" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": null, 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "xguess = [-3; 2]\n", 103 | "λguess = [0.0]\n", 104 | "plot_landscape()\n", 105 | "plot(xguess[1], xguess[2], \"rx\")" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": null, 111 | "metadata": {}, 112 | "outputs": [], 113 | "source": [ 114 | "xnew, λnew = newton_step(xguess[:,end],λguess[end])\n", 115 | "xguess = [xguess xnew]\n", 116 | "λguess = [λguess λnew]\n", 117 | "plot_landscape()\n", 118 | "plot(xguess[1,:], xguess[2,:],\n", 119 | " \"rx\")" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": null, 125 | "metadata": {}, 126 | "outputs": [], 127 | "source": [ 128 | "H = ∇2f(xguess[:,end]) + ForwardDiff.jacobian(x -> ∂c(x)'*λguess[end], xguess[:,end])" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": null, 134 | "metadata": {}, 135 | "outputs": [], 136 | "source": [] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": null, 141 | "metadata": {}, 142 | "outputs": [], 143 | "source": [ 144 | "function gauss_newton_step(x0,λ0)\n", 145 | " H = ∇2f(x0)\n", 146 | " C = ∂c(x0)\n", 147 | " Δz = [H C'; C 0]\\[-∇f(x0)-C'*λ0; -c(x0)]\n", 148 | " Δx = Δz[1:2]\n", 149 | " Δλ = Δz[3]\n", 150 | " return x0+Δx, λ0+Δλ\n", 151 | "end" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": null, 157 | "metadata": {}, 158 | "outputs": [], 159 | "source": [ 160 | "xguess = [-3; 2]\n", 161 | "λguess = [0.0]\n", 162 | "plot_landscape()\n", 163 | "plot(xguess[1], xguess[2], \"rx\")" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": null, 169 | "metadata": {}, 170 | "outputs": [], 171 | "source": [ 172 | "xnew, λnew = gauss_newton_step(xguess[:,end],λguess[end])\n", 173 | "xguess = [xguess xnew]\n", 174 | "λguess = [λguess λnew]\n", 175 | "plot_landscape()\n", 176 | "plot(xguess[1,:], xguess[2,:], \"rx\")" 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": null, 182 | "metadata": {}, 183 | "outputs": [], 184 | "source": [] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": null, 189 | "metadata": {}, 190 | "outputs": [], 191 | "source": [] 192 | } 193 | ], 194 | "metadata": { 195 | "kernelspec": { 196 | "display_name": "Julia 1.6.7", 197 | "language": "julia", 198 | "name": "julia-1.6" 199 | }, 200 | "language_info": { 201 | "file_extension": ".jl", 202 | "mimetype": "application/julia", 203 | "name": "julia", 204 | "version": "1.6.7" 205 | } 206 | }, 207 | "nbformat": 4, 208 | "nbformat_minor": 4 209 | } 210 | -------------------------------------------------------------------------------- /Lecture 12/Lecture 12.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamics-simulation-16-715/lecture-notebooks/a2f7642b3ba31629fc15ca77b738e3f21a5e6c8c/Lecture 12/Lecture 12.pdf -------------------------------------------------------------------------------- /Lecture 13/Lecture 13.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamics-simulation-16-715/lecture-notebooks/a2f7642b3ba31629fc15ca77b738e3f21a5e6c8c/Lecture 13/Lecture 13.pdf -------------------------------------------------------------------------------- /Lecture 13/Manifest.toml: -------------------------------------------------------------------------------- 1 | # This file is machine-generated - editing it directly is not advised 2 | 3 | [[ASL_jll]] 4 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 5 | git-tree-sha1 = "370cafc70604b2522f2c7cf9915ebcd17b4cd38b" 6 | uuid = "ae81ac8f-d209-56e5-92de-9978fef736f9" 7 | version = "0.1.2+0" 8 | 9 | [[ArgTools]] 10 | uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f" 11 | 12 | [[Artifacts]] 13 | uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33" 14 | 15 | [[AssetRegistry]] 16 | deps = ["Distributed", "JSON", "Pidfile", "SHA", "Test"] 17 | git-tree-sha1 = "b25e88db7944f98789130d7b503276bc34bc098e" 18 | uuid = "bf4720bc-e11a-5d0c-854e-bdca1663c893" 19 | version = "0.1.0" 20 | 21 | [[Base64]] 22 | uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" 23 | 24 | [[BenchmarkTools]] 25 | deps = ["JSON", "Logging", "Printf", "Profile", "Statistics", "UUIDs"] 26 | git-tree-sha1 = "61adeb0823084487000600ef8b1c00cc2474cd47" 27 | uuid = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" 28 | version = "1.2.0" 29 | 30 | [[BinDeps]] 31 | deps = ["Libdl", "Pkg", "SHA", "URIParser", "Unicode"] 32 | git-tree-sha1 = "1289b57e8cf019aede076edab0587eb9644175bd" 33 | uuid = "9e28174c-4ba2-5203-b857-d8d62c4213ee" 34 | version = "1.0.2" 35 | 36 | [[BinaryProvider]] 37 | deps = ["Libdl", "Logging", "SHA"] 38 | git-tree-sha1 = "ecdec412a9abc8db54c0efc5548c64dfce072058" 39 | uuid = "b99e7846-7c00-51b0-8f62-c81ae34c0232" 40 | version = "0.5.10" 41 | 42 | [[Blink]] 43 | deps = ["Base64", "BinDeps", "Distributed", "JSExpr", "JSON", "Lazy", "Logging", "MacroTools", "Mustache", "Mux", "Reexport", "Sockets", "WebIO", "WebSockets"] 44 | git-tree-sha1 = "08d0b679fd7caa49e2bca9214b131289e19808c0" 45 | uuid = "ad839575-38b3-5650-b840-f874b8c74a25" 46 | version = "0.12.5" 47 | 48 | [[Bzip2_jll]] 49 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 50 | git-tree-sha1 = "19a35467a82e236ff51bc17a3a44b69ef35185a2" 51 | uuid = "6e34b625-4abd-537c-b88f-471c36dfa7a0" 52 | version = "1.0.8+0" 53 | 54 | [[CodecBzip2]] 55 | deps = ["Bzip2_jll", "Libdl", "TranscodingStreams"] 56 | git-tree-sha1 = "2e62a725210ce3c3c2e1a3080190e7ca491f18d7" 57 | uuid = "523fee87-0ab8-5b00-afb7-3ecf72e48cfd" 58 | version = "0.7.2" 59 | 60 | [[CodecZlib]] 61 | deps = ["TranscodingStreams", "Zlib_jll"] 62 | git-tree-sha1 = "ded953804d019afa9a3f98981d99b33e3db7b6da" 63 | uuid = "944b1d66-785c-5afd-91f1-9de20f533193" 64 | version = "0.7.0" 65 | 66 | [[ColorSchemes]] 67 | deps = ["ColorTypes", "Colors", "FixedPointNumbers", "Random"] 68 | git-tree-sha1 = "a851fec56cb73cfdf43762999ec72eff5b86882a" 69 | uuid = "35d6a980-a343-548e-a6ea-1d62b119f2f4" 70 | version = "3.15.0" 71 | 72 | [[ColorTypes]] 73 | deps = ["FixedPointNumbers", "Random"] 74 | git-tree-sha1 = "024fe24d83e4a5bf5fc80501a314ce0d1aa35597" 75 | uuid = "3da002f7-5984-5a60-b8a6-cbb66c0b333f" 76 | version = "0.11.0" 77 | 78 | [[Colors]] 79 | deps = ["ColorTypes", "FixedPointNumbers", "Reexport"] 80 | git-tree-sha1 = "417b0ed7b8b838aa6ca0a87aadf1bb9eb111ce40" 81 | uuid = "5ae59095-9a9b-59fe-a467-6f913c188581" 82 | version = "0.12.8" 83 | 84 | [[CompilerSupportLibraries_jll]] 85 | deps = ["Artifacts", "Libdl"] 86 | uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae" 87 | 88 | [[DataAPI]] 89 | git-tree-sha1 = "cc70b17275652eb47bc9e5f81635981f13cea5c8" 90 | uuid = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a" 91 | version = "1.9.0" 92 | 93 | [[DataValueInterfaces]] 94 | git-tree-sha1 = "bfc1187b79289637fa0ef6d4436ebdfe6905cbd6" 95 | uuid = "e2d170a0-9d28-54be-80f0-106bbe20a464" 96 | version = "1.0.0" 97 | 98 | [[Dates]] 99 | deps = ["Printf"] 100 | uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" 101 | 102 | [[DelimitedFiles]] 103 | deps = ["Mmap"] 104 | uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab" 105 | 106 | [[Distributed]] 107 | deps = ["Random", "Serialization", "Sockets"] 108 | uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b" 109 | 110 | [[DocStringExtensions]] 111 | deps = ["LibGit2"] 112 | git-tree-sha1 = "a32185f5428d3986f47c2ab78b1f216d5e6cc96f" 113 | uuid = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" 114 | version = "0.8.5" 115 | 116 | [[Downloads]] 117 | deps = ["ArgTools", "LibCURL", "NetworkOptions"] 118 | uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6" 119 | 120 | [[FileWatching]] 121 | uuid = "7b1f6079-737a-58dc-b8bc-7a2ca5c1b5ee" 122 | 123 | [[FixedPointNumbers]] 124 | deps = ["Statistics"] 125 | git-tree-sha1 = "335bfdceacc84c5cdf16aadc768aa5ddfc5383cc" 126 | uuid = "53c48c17-4a7d-5ca2-90c5-79b7896eea93" 127 | version = "0.8.4" 128 | 129 | [[FunctionalCollections]] 130 | deps = ["Test"] 131 | git-tree-sha1 = "04cb9cfaa6ba5311973994fe3496ddec19b6292a" 132 | uuid = "de31a74c-ac4f-5751-b3fd-e18cd04993ca" 133 | version = "0.5.0" 134 | 135 | [[HTTP]] 136 | deps = ["Base64", "Dates", "IniFile", "Logging", "MbedTLS", "NetworkOptions", "Sockets", "URIs"] 137 | git-tree-sha1 = "14eece7a3308b4d8be910e265c724a6ba51a9798" 138 | uuid = "cd3eb016-35fb-5094-929b-558a96fad6f3" 139 | version = "0.9.16" 140 | 141 | [[Hiccup]] 142 | deps = ["MacroTools", "Test"] 143 | git-tree-sha1 = "6187bb2d5fcbb2007c39e7ac53308b0d371124bd" 144 | uuid = "9fb69e20-1954-56bb-a84f-559cc56a8ff7" 145 | version = "0.2.2" 146 | 147 | [[IniFile]] 148 | deps = ["Test"] 149 | git-tree-sha1 = "098e4d2c533924c921f9f9847274f2ad89e018b8" 150 | uuid = "83e8ac13-25f8-5344-8a64-a9f2b223428f" 151 | version = "0.5.0" 152 | 153 | [[InteractiveUtils]] 154 | deps = ["Markdown"] 155 | uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" 156 | 157 | [[Ipopt]] 158 | deps = ["BinaryProvider", "Ipopt_jll", "Libdl", "LinearAlgebra", "MathOptInterface", "MathProgBase"] 159 | git-tree-sha1 = "539b23ab8fb86c6cc3e8cacaeb1f784415951be5" 160 | uuid = "b6b21f68-93f8-5de0-b562-5493be1d77c9" 161 | version = "0.8.0" 162 | 163 | [[Ipopt_jll]] 164 | deps = ["ASL_jll", "Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "MUMPS_seq_jll", "OpenBLAS32_jll", "Pkg"] 165 | git-tree-sha1 = "82124f27743f2802c23fcb05febc517d0b15d86e" 166 | uuid = "9cc047cb-c261-5740-88fc-0cf96f7bdcc7" 167 | version = "3.13.4+2" 168 | 169 | [[IteratorInterfaceExtensions]] 170 | git-tree-sha1 = "a3f24677c21f5bbe9d2a714f95dcd58337fb2856" 171 | uuid = "82899510-4779-5014-852e-03e436cf321d" 172 | version = "1.0.0" 173 | 174 | [[JLLWrappers]] 175 | deps = ["Preferences"] 176 | git-tree-sha1 = "642a199af8b68253517b80bd3bfd17eb4e84df6e" 177 | uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210" 178 | version = "1.3.0" 179 | 180 | [[JSExpr]] 181 | deps = ["JSON", "MacroTools", "Observables", "WebIO"] 182 | git-tree-sha1 = "bd6c034156b1e7295450a219c4340e32e50b08b1" 183 | uuid = "97c1335a-c9c5-57fe-bc5d-ec35cebe8660" 184 | version = "0.5.3" 185 | 186 | [[JSON]] 187 | deps = ["Dates", "Mmap", "Parsers", "Unicode"] 188 | git-tree-sha1 = "8076680b162ada2a031f707ac7b4953e30667a37" 189 | uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" 190 | version = "0.21.2" 191 | 192 | [[Kaleido_jll]] 193 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 194 | git-tree-sha1 = "2ef87eeaa28713cb010f9fb0be288b6c1a4ecd53" 195 | uuid = "f7e6163d-2fa5-5f23-b69c-1db539e41963" 196 | version = "0.1.0+0" 197 | 198 | [[LaTeXStrings]] 199 | git-tree-sha1 = "c7f1c695e06c01b95a67f0cd1d34994f3e7db104" 200 | uuid = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f" 201 | version = "1.2.1" 202 | 203 | [[Lazy]] 204 | deps = ["MacroTools"] 205 | git-tree-sha1 = "1370f8202dac30758f3c345f9909b97f53d87d3f" 206 | uuid = "50d2b5c4-7a5e-59d5-8109-a42b560f39c0" 207 | version = "0.15.1" 208 | 209 | [[LibCURL]] 210 | deps = ["LibCURL_jll", "MozillaCACerts_jll"] 211 | uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21" 212 | 213 | [[LibCURL_jll]] 214 | deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"] 215 | uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0" 216 | 217 | [[LibGit2]] 218 | deps = ["Base64", "NetworkOptions", "Printf", "SHA"] 219 | uuid = "76f85450-5226-5b5a-8eaa-529ad045b433" 220 | 221 | [[LibSSH2_jll]] 222 | deps = ["Artifacts", "Libdl", "MbedTLS_jll"] 223 | uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8" 224 | 225 | [[Libdl]] 226 | uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" 227 | 228 | [[LinearAlgebra]] 229 | deps = ["Libdl"] 230 | uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" 231 | 232 | [[Logging]] 233 | uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" 234 | 235 | [[METIS_jll]] 236 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 237 | git-tree-sha1 = "2dc1a9fc87e57e32b1fc186db78811157b30c118" 238 | uuid = "d00139f3-1899-568f-a2f0-47f597d42d70" 239 | version = "5.1.0+5" 240 | 241 | [[MUMPS_seq_jll]] 242 | deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "METIS_jll", "OpenBLAS32_jll", "Pkg"] 243 | git-tree-sha1 = "1a11a84b2af5feb5a62a820574804056cdc59c39" 244 | uuid = "d7ed1dd3-d0ae-5e8e-bfb4-87a502085b8d" 245 | version = "5.2.1+4" 246 | 247 | [[MacroTools]] 248 | deps = ["Markdown", "Random"] 249 | git-tree-sha1 = "5a5bc6bf062f0f95e62d0fe0a2d99699fed82dd9" 250 | uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" 251 | version = "0.5.8" 252 | 253 | [[Markdown]] 254 | deps = ["Base64"] 255 | uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" 256 | 257 | [[MathOptInterface]] 258 | deps = ["BenchmarkTools", "CodecBzip2", "CodecZlib", "JSON", "LinearAlgebra", "MutableArithmetics", "OrderedCollections", "Printf", "SparseArrays", "Test", "Unicode"] 259 | git-tree-sha1 = "4819019da9f42746851ddc39d90222d1dbe953b7" 260 | uuid = "b8f27783-ece8-5eb3-8dc8-9495eed66fee" 261 | version = "0.10.3" 262 | 263 | [[MathProgBase]] 264 | deps = ["LinearAlgebra", "SparseArrays"] 265 | git-tree-sha1 = "9abbe463a1e9fc507f12a69e7f29346c2cdc472c" 266 | uuid = "fdba3010-5040-5b88-9595-932c9decdf73" 267 | version = "0.7.8" 268 | 269 | [[MbedTLS]] 270 | deps = ["Dates", "MbedTLS_jll", "Random", "Sockets"] 271 | git-tree-sha1 = "1c38e51c3d08ef2278062ebceade0e46cefc96fe" 272 | uuid = "739be429-bea8-5141-9913-cc70e7f3736d" 273 | version = "1.0.3" 274 | 275 | [[MbedTLS_jll]] 276 | deps = ["Artifacts", "Libdl"] 277 | uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1" 278 | 279 | [[Mmap]] 280 | uuid = "a63ad114-7e13-5084-954f-fe012c677804" 281 | 282 | [[MozillaCACerts_jll]] 283 | uuid = "14a3606d-f60d-562e-9121-12d972cd8159" 284 | 285 | [[Mustache]] 286 | deps = ["Printf", "Tables"] 287 | git-tree-sha1 = "36995ef0d532fe08119d70b2365b7b03d4e00f48" 288 | uuid = "ffc61752-8dc7-55ee-8c37-f3e9cdd09e70" 289 | version = "1.0.10" 290 | 291 | [[MutableArithmetics]] 292 | deps = ["LinearAlgebra", "SparseArrays", "Test"] 293 | git-tree-sha1 = "372e3a76d969e651ca70eb647bf0e303bc95d615" 294 | uuid = "d8a4904e-b15c-11e9-3269-09a3773c0cb0" 295 | version = "0.2.21" 296 | 297 | [[Mux]] 298 | deps = ["AssetRegistry", "Base64", "HTTP", "Hiccup", "Pkg", "Sockets", "WebSockets"] 299 | git-tree-sha1 = "82dfb2cead9895e10ee1b0ca37a01088456c4364" 300 | uuid = "a975b10e-0019-58db-a62f-e48ff68538c9" 301 | version = "0.7.6" 302 | 303 | [[NetworkOptions]] 304 | uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908" 305 | 306 | [[Observables]] 307 | git-tree-sha1 = "fe29afdef3d0c4a8286128d4e45cc50621b1e43d" 308 | uuid = "510215fc-4207-5dde-b226-833fc4488ee2" 309 | version = "0.4.0" 310 | 311 | [[OpenBLAS32_jll]] 312 | deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "Pkg"] 313 | git-tree-sha1 = "ba4a8f683303c9082e84afba96f25af3c7fb2436" 314 | uuid = "656ef2d0-ae68-5445-9ca0-591084a874a2" 315 | version = "0.3.12+1" 316 | 317 | [[OrderedCollections]] 318 | git-tree-sha1 = "85f8e6578bf1f9ee0d11e7bb1b1456435479d47c" 319 | uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" 320 | version = "1.4.1" 321 | 322 | [[Parameters]] 323 | deps = ["OrderedCollections", "UnPack"] 324 | git-tree-sha1 = "34c0e9ad262e5f7fc75b10a9952ca7692cfc5fbe" 325 | uuid = "d96e819e-fc66-5662-9728-84c9c7592b0a" 326 | version = "0.12.3" 327 | 328 | [[Parsers]] 329 | deps = ["Dates"] 330 | git-tree-sha1 = "a8709b968a1ea6abc2dc1967cb1db6ac9a00dfb6" 331 | uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0" 332 | version = "2.0.5" 333 | 334 | [[Pidfile]] 335 | deps = ["FileWatching", "Test"] 336 | git-tree-sha1 = "1be8660b2064893cd2dae4bd004b589278e4440d" 337 | uuid = "fa939f87-e72e-5be4-a000-7fc836dbe307" 338 | version = "1.2.0" 339 | 340 | [[Pkg]] 341 | deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"] 342 | uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" 343 | 344 | [[PlotlyBase]] 345 | deps = ["ColorSchemes", "Dates", "DelimitedFiles", "DocStringExtensions", "JSON", "LaTeXStrings", "Logging", "Parameters", "Pkg", "REPL", "Requires", "Statistics", "UUIDs"] 346 | git-tree-sha1 = "180d744848ba316a3d0fdf4dbd34b77c7242963a" 347 | uuid = "a03496cd-edff-5a9b-9e67-9cda94a718b5" 348 | version = "0.8.18" 349 | 350 | [[PlotlyJS]] 351 | deps = ["Base64", "Blink", "DelimitedFiles", "JSExpr", "JSON", "Kaleido_jll", "Markdown", "Pkg", "PlotlyBase", "REPL", "Reexport", "Requires", "WebIO"] 352 | git-tree-sha1 = "53d6325e14d3bdb85fd387a085075f36082f35a3" 353 | uuid = "f0f68f2c-4968-5e81-91da-67840de0976a" 354 | version = "0.18.8" 355 | 356 | [[Preferences]] 357 | deps = ["TOML"] 358 | git-tree-sha1 = "00cfd92944ca9c760982747e9a1d0d5d86ab1e5a" 359 | uuid = "21216c6a-2e73-6563-6e65-726566657250" 360 | version = "1.2.2" 361 | 362 | [[Printf]] 363 | deps = ["Unicode"] 364 | uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" 365 | 366 | [[Profile]] 367 | deps = ["Printf"] 368 | uuid = "9abbd945-dff8-562f-b5e8-e1ebf5ef1b79" 369 | 370 | [[REPL]] 371 | deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"] 372 | uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" 373 | 374 | [[Random]] 375 | deps = ["Serialization"] 376 | uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" 377 | 378 | [[Reexport]] 379 | git-tree-sha1 = "45e428421666073eab6f2da5c9d310d99bb12f9b" 380 | uuid = "189a3867-3050-52da-a836-e630ba90ab69" 381 | version = "1.2.2" 382 | 383 | [[Requires]] 384 | deps = ["UUIDs"] 385 | git-tree-sha1 = "4036a3bd08ac7e968e27c203d45f5fff15020621" 386 | uuid = "ae029012-a4dd-5104-9daa-d747884805df" 387 | version = "1.1.3" 388 | 389 | [[SHA]] 390 | uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" 391 | 392 | [[Serialization]] 393 | uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" 394 | 395 | [[Sockets]] 396 | uuid = "6462fe0b-24de-5631-8697-dd941f90decc" 397 | 398 | [[SparseArrays]] 399 | deps = ["LinearAlgebra", "Random"] 400 | uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" 401 | 402 | [[Statistics]] 403 | deps = ["LinearAlgebra", "SparseArrays"] 404 | uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" 405 | 406 | [[TOML]] 407 | deps = ["Dates"] 408 | uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76" 409 | 410 | [[TableTraits]] 411 | deps = ["IteratorInterfaceExtensions"] 412 | git-tree-sha1 = "c06b2f539df1c6efa794486abfb6ed2022561a39" 413 | uuid = "3783bdb8-4a98-5b6b-af9a-565f29a5fe9c" 414 | version = "1.0.1" 415 | 416 | [[Tables]] 417 | deps = ["DataAPI", "DataValueInterfaces", "IteratorInterfaceExtensions", "LinearAlgebra", "TableTraits", "Test"] 418 | git-tree-sha1 = "fed34d0e71b91734bf0a7e10eb1bb05296ddbcd0" 419 | uuid = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" 420 | version = "1.6.0" 421 | 422 | [[Tar]] 423 | deps = ["ArgTools", "SHA"] 424 | uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e" 425 | 426 | [[Test]] 427 | deps = ["InteractiveUtils", "Logging", "Random", "Serialization"] 428 | uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" 429 | 430 | [[TranscodingStreams]] 431 | deps = ["Random", "Test"] 432 | git-tree-sha1 = "216b95ea110b5972db65aa90f88d8d89dcb8851c" 433 | uuid = "3bb67fe8-82b1-5028-8e26-92a6c54297fa" 434 | version = "0.9.6" 435 | 436 | [[URIParser]] 437 | deps = ["Unicode"] 438 | git-tree-sha1 = "53a9f49546b8d2dd2e688d216421d050c9a31d0d" 439 | uuid = "30578b45-9adc-5946-b283-645ec420af67" 440 | version = "0.4.1" 441 | 442 | [[URIs]] 443 | git-tree-sha1 = "97bbe755a53fe859669cd907f2d96aee8d2c1355" 444 | uuid = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4" 445 | version = "1.3.0" 446 | 447 | [[UUIDs]] 448 | deps = ["Random", "SHA"] 449 | uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" 450 | 451 | [[UnPack]] 452 | git-tree-sha1 = "387c1f73762231e86e0c9c5443ce3b4a0a9a0c2b" 453 | uuid = "3a884ed6-31ef-47d7-9d2a-63182c4928ed" 454 | version = "1.0.2" 455 | 456 | [[Unicode]] 457 | uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" 458 | 459 | [[WebIO]] 460 | deps = ["AssetRegistry", "Base64", "Distributed", "FunctionalCollections", "JSON", "Logging", "Observables", "Pkg", "Random", "Requires", "Sockets", "UUIDs", "WebSockets", "Widgets"] 461 | git-tree-sha1 = "5fe32e4086d49f7ab9b087296742859f3ae6d62a" 462 | uuid = "0f1e0344-ec1d-5b48-a673-e5cf874b6c29" 463 | version = "0.8.16" 464 | 465 | [[WebSockets]] 466 | deps = ["Base64", "Dates", "HTTP", "Logging", "Sockets"] 467 | git-tree-sha1 = "f91a602e25fe6b89afc93cf02a4ae18ee9384ce3" 468 | uuid = "104b5d7c-a370-577a-8038-80a2059c5097" 469 | version = "1.5.9" 470 | 471 | [[Widgets]] 472 | deps = ["Colors", "Dates", "Observables", "OrderedCollections"] 473 | git-tree-sha1 = "80661f59d28714632132c73779f8becc19a113f2" 474 | uuid = "cc8bc4a8-27d6-5769-a93b-9d913e69aa62" 475 | version = "0.6.4" 476 | 477 | [[Zlib_jll]] 478 | deps = ["Libdl"] 479 | uuid = "83775a58-1f1d-513f-b197-d71354ab007a" 480 | 481 | [[nghttp2_jll]] 482 | deps = ["Artifacts", "Libdl"] 483 | uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d" 484 | 485 | [[p7zip_jll]] 486 | deps = ["Artifacts", "Libdl"] 487 | uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0" 488 | -------------------------------------------------------------------------------- /Lecture 13/Project.toml: -------------------------------------------------------------------------------- 1 | [deps] 2 | Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" 3 | MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee" 4 | PlotlyJS = "f0f68f2c-4968-5e81-91da-67840de0976a" 5 | -------------------------------------------------------------------------------- /Lecture 13/cable.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "d1ce915b", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "import Pkg; Pkg.activate(@__DIR__); Pkg.instantiate()" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": null, 16 | "id": "7853001c", 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "using LinearAlgebra\n", 21 | "using ForwardDiff\n", 22 | "using Ipopt\n", 23 | "using MathOptInterface\n", 24 | "const MOI = MathOptInterface\n", 25 | "using PyPlot" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": null, 31 | "id": "2478374c", 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "g = -1\n", 36 | "w = 10 #width of gap\n", 37 | "ℓ = 12 #length of cable\n", 38 | "m = 10 #mass of cable\n", 39 | "n = 25 #number of discrete segments\n", 40 | "\n", 41 | "Δx = w/n #width of each discrete segment\n", 42 | "ρ = m/ℓ #mass density" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "id": "e294feb8", 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [ 52 | "#Total cable length\n", 53 | "function S(y,ẏ)\n", 54 | " s = 0.0\n", 55 | " for k = 1:(n-1)\n", 56 | " s += sqrt(1 + ẏ[k]^2)*Δx\n", 57 | " end\n", 58 | " return s\n", 59 | "end" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "id": "51c84cd5", 66 | "metadata": {}, 67 | "outputs": [], 68 | "source": [ 69 | "#Potential energy\n", 70 | "function U(y,ẏ)\n", 71 | " u = 0.0\n", 72 | " for k = 1:(n-1)\n", 73 | " ym = 0.5*(y[k+1]+y[k])\n", 74 | " u += ρ*g*ym*sqrt(1 + ẏ[k]^2)*Δx\n", 75 | " end\n", 76 | " return u\n", 77 | "end" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "id": "014c668b", 84 | "metadata": {}, 85 | "outputs": [], 86 | "source": [ 87 | "#End points\n", 88 | "y1 = 0.0\n", 89 | "yn = -2.0" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": null, 95 | "id": "60b1ad58", 96 | "metadata": {}, 97 | "outputs": [], 98 | "source": [ 99 | "#Objective and constraint functions for IPOPT\n", 100 | "\n", 101 | "function objective(z)\n", 102 | " y = z[1:n]\n", 103 | " ẏ = z[(n+1):end]\n", 104 | " U(y,ẏ)\n", 105 | "end\n", 106 | "\n", 107 | "function constraint!(c,z)\n", 108 | " y = z[1:n]\n", 109 | " ẏ = z[(n+1):end]\n", 110 | " \n", 111 | " c .= [\n", 112 | " y[1] - y1 #initial point\n", 113 | " y[end] - yn #final point\n", 114 | " S(y,ẏ) - ℓ #length constraint\n", 115 | " y[2:end] .- y[1:(end-1)] .- ẏ.*Δx #velocity constraints\n", 116 | " ]\n", 117 | " \n", 118 | " return nothing\n", 119 | " \n", 120 | "end" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "id": "48fc9d6c", 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [ 130 | "#Boilerplate code to interface with IPOPT\n", 131 | "struct ProblemMOI <: MOI.AbstractNLPEvaluator\n", 132 | " n_nlp::Int\n", 133 | " m_nlp::Int\n", 134 | " idx_ineq\n", 135 | " obj_grad::Bool\n", 136 | " con_jac::Bool\n", 137 | " sparsity_jac\n", 138 | " sparsity_hess\n", 139 | " primal_bounds\n", 140 | " constraint_bounds\n", 141 | " hessian_lagrangian::Bool\n", 142 | "end\n", 143 | "\n", 144 | "function ProblemMOI(n_nlp,m_nlp;\n", 145 | " idx_ineq=(1:0),\n", 146 | " obj_grad=true,\n", 147 | " con_jac=true,\n", 148 | " sparsity_jac=sparsity_jacobian(n_nlp,m_nlp),\n", 149 | " sparsity_hess=sparsity_hessian(n_nlp,m_nlp),\n", 150 | " primal_bounds=primal_bounds(n_nlp),\n", 151 | " constraint_bounds=constraint_bounds(m_nlp,idx_ineq=idx_ineq),\n", 152 | " hessian_lagrangian=false)\n", 153 | "\n", 154 | " ProblemMOI(n_nlp,m_nlp,\n", 155 | " idx_ineq,\n", 156 | " obj_grad,\n", 157 | " con_jac,\n", 158 | " sparsity_jac,\n", 159 | " sparsity_hess,\n", 160 | " primal_bounds,\n", 161 | " constraint_bounds,\n", 162 | " hessian_lagrangian)\n", 163 | "end\n", 164 | "\n", 165 | "function primal_bounds(n)\n", 166 | " x_l = -Inf*ones(n)\n", 167 | " x_u = Inf*ones(n)\n", 168 | " return x_l, x_u\n", 169 | "end\n", 170 | "\n", 171 | "function constraint_bounds(m; idx_ineq=(1:0))\n", 172 | " c_l = zeros(m)\n", 173 | " c_l[idx_ineq] .= -Inf\n", 174 | "\n", 175 | " c_u = zeros(m)\n", 176 | " return c_l, c_u\n", 177 | "end\n", 178 | "\n", 179 | "function row_col!(row,col,r,c)\n", 180 | " for cc in c\n", 181 | " for rr in r\n", 182 | " push!(row,convert(Int,rr))\n", 183 | " push!(col,convert(Int,cc))\n", 184 | " end\n", 185 | " end\n", 186 | " return row, col\n", 187 | "end\n", 188 | "\n", 189 | "function sparsity_jacobian(n,m)\n", 190 | "\n", 191 | " row = []\n", 192 | " col = []\n", 193 | "\n", 194 | " r = 1:m\n", 195 | " c = 1:n\n", 196 | "\n", 197 | " row_col!(row,col,r,c)\n", 198 | "\n", 199 | " return collect(zip(row,col))\n", 200 | "end\n", 201 | "\n", 202 | "function sparsity_hessian(n,m)\n", 203 | "\n", 204 | " row = []\n", 205 | " col = []\n", 206 | "\n", 207 | " r = 1:m\n", 208 | " c = 1:n\n", 209 | "\n", 210 | " row_col!(row,col,r,c)\n", 211 | "\n", 212 | " return collect(zip(row,col))\n", 213 | "end\n", 214 | "\n", 215 | "function MOI.eval_objective(prob::MOI.AbstractNLPEvaluator, x)\n", 216 | " objective(x)\n", 217 | "end\n", 218 | "\n", 219 | "function MOI.eval_objective_gradient(prob::MOI.AbstractNLPEvaluator, grad_f, x)\n", 220 | " ForwardDiff.gradient!(grad_f,objective,x)\n", 221 | " return nothing\n", 222 | "end\n", 223 | "\n", 224 | "function MOI.eval_constraint(prob::MOI.AbstractNLPEvaluator,g,x)\n", 225 | " constraint!(g,x)\n", 226 | " return nothing\n", 227 | "end\n", 228 | "\n", 229 | "function MOI.eval_constraint_jacobian(prob::MOI.AbstractNLPEvaluator, jac, x)\n", 230 | " ForwardDiff.jacobian!(reshape(jac,prob.m_nlp,prob.n_nlp), constraint!, zeros(prob.m_nlp), x)\n", 231 | " return nothing\n", 232 | "end\n", 233 | "\n", 234 | "function MOI.features_available(prob::MOI.AbstractNLPEvaluator)\n", 235 | " return [:Grad, :Jac]\n", 236 | "end\n", 237 | "\n", 238 | "MOI.initialize(prob::MOI.AbstractNLPEvaluator, features) = nothing\n", 239 | "MOI.jacobian_structure(prob::MOI.AbstractNLPEvaluator) = prob.sparsity_jac\n", 240 | "\n", 241 | "function solve(x0,prob::MOI.AbstractNLPEvaluator;\n", 242 | " tol=1.0e-6,c_tol=1.0e-6,max_iter=10000)\n", 243 | " x_l, x_u = prob.primal_bounds\n", 244 | " c_l, c_u = prob.constraint_bounds\n", 245 | "\n", 246 | " nlp_bounds = MOI.NLPBoundsPair.(c_l,c_u)\n", 247 | " block_data = MOI.NLPBlockData(nlp_bounds,prob,true)\n", 248 | "\n", 249 | " solver = Ipopt.Optimizer()\n", 250 | " solver.options[\"max_iter\"] = max_iter\n", 251 | " solver.options[\"tol\"] = tol\n", 252 | " solver.options[\"constr_viol_tol\"] = c_tol\n", 253 | "\n", 254 | " x = MOI.add_variables(solver,prob.n_nlp)\n", 255 | "\n", 256 | " for i = 1:prob.n_nlp\n", 257 | " xi = MOI.SingleVariable(x[i])\n", 258 | " MOI.add_constraint(solver, xi, MOI.LessThan(x_u[i]))\n", 259 | " MOI.add_constraint(solver, xi, MOI.GreaterThan(x_l[i]))\n", 260 | " MOI.set(solver, MOI.VariablePrimalStart(), x[i], x0[i])\n", 261 | " end\n", 262 | "\n", 263 | " # Solve the problem\n", 264 | " MOI.set(solver, MOI.NLPBlock(), block_data)\n", 265 | " MOI.set(solver, MOI.ObjectiveSense(), MOI.MIN_SENSE)\n", 266 | " MOI.optimize!(solver)\n", 267 | "\n", 268 | " # Get the solution\n", 269 | " res = MOI.get(solver, MOI.VariablePrimal(), x)\n", 270 | "\n", 271 | " return res\n", 272 | "end" 273 | ] 274 | }, 275 | { 276 | "cell_type": "code", 277 | "execution_count": null, 278 | "id": "76c018b8", 279 | "metadata": {}, 280 | "outputs": [], 281 | "source": [ 282 | "#initial guess\n", 283 | "z_guess = zeros(2*n-1);" 284 | ] 285 | }, 286 | { 287 | "cell_type": "code", 288 | "execution_count": null, 289 | "id": "b6cdf7d9", 290 | "metadata": { 291 | "scrolled": true 292 | }, 293 | "outputs": [], 294 | "source": [ 295 | "#Solve with IPOPT\n", 296 | "n_nlp = 2*n-1\n", 297 | "m_nlp = 2+n\n", 298 | "prob = ProblemMOI(n_nlp,m_nlp)\n", 299 | "\n", 300 | "z_sol = solve(z_guess,prob)" 301 | ] 302 | }, 303 | { 304 | "cell_type": "code", 305 | "execution_count": null, 306 | "id": "8b549fa1", 307 | "metadata": {}, 308 | "outputs": [], 309 | "source": [ 310 | "#Plot Solution\n", 311 | "y_sol = z_sol[1:n]\n", 312 | "plot(y_sol)" 313 | ] 314 | }, 315 | { 316 | "cell_type": "code", 317 | "execution_count": null, 318 | "id": "909976bd", 319 | "metadata": {}, 320 | "outputs": [], 321 | "source": [] 322 | }, 323 | { 324 | "cell_type": "code", 325 | "execution_count": null, 326 | "id": "c74f2fa4-808c-4cb0-8424-6b7d12c0da6b", 327 | "metadata": {}, 328 | "outputs": [], 329 | "source": [] 330 | } 331 | ], 332 | "metadata": { 333 | "@webio": { 334 | "lastCommId": "33bfe908dd4f4965865f41584637fec5", 335 | "lastKernelId": "df83269f-cb82-48f0-b784-10ecf63300d6" 336 | }, 337 | "kernelspec": { 338 | "display_name": "Julia 1.6.7", 339 | "language": "julia", 340 | "name": "julia-1.6" 341 | }, 342 | "language_info": { 343 | "file_extension": ".jl", 344 | "mimetype": "application/julia", 345 | "name": "julia", 346 | "version": "1.6.7" 347 | } 348 | }, 349 | "nbformat": 4, 350 | "nbformat_minor": 5 351 | } 352 | -------------------------------------------------------------------------------- /Lecture 14/Lecture 14.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamics-simulation-16-715/lecture-notebooks/a2f7642b3ba31629fc15ca77b738e3f21a5e6c8c/Lecture 14/Lecture 14.pdf -------------------------------------------------------------------------------- /Lecture 15/Lecture 15.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamics-simulation-16-715/lecture-notebooks/a2f7642b3ba31629fc15ca77b738e3f21a5e6c8c/Lecture 15/Lecture 15.pdf -------------------------------------------------------------------------------- /Lecture 15/Manifest.toml: -------------------------------------------------------------------------------- 1 | # This file is machine-generated - editing it directly is not advised 2 | 3 | -------------------------------------------------------------------------------- /Lecture 15/Project.toml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamics-simulation-16-715/lecture-notebooks/a2f7642b3ba31629fc15ca77b738e3f21a5e6c8c/Lecture 15/Project.toml -------------------------------------------------------------------------------- /Lecture 16/Lecture 16.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamics-simulation-16-715/lecture-notebooks/a2f7642b3ba31629fc15ca77b738e3f21a5e6c8c/Lecture 16/Lecture 16.pdf -------------------------------------------------------------------------------- /Lecture 16/Manifest.toml: -------------------------------------------------------------------------------- 1 | # This file is machine-generated - editing it directly is not advised 2 | 3 | -------------------------------------------------------------------------------- /Lecture 16/Project.toml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamics-simulation-16-715/lecture-notebooks/a2f7642b3ba31629fc15ca77b738e3f21a5e6c8c/Lecture 16/Project.toml -------------------------------------------------------------------------------- /Lecture 17/Lecture 17.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamics-simulation-16-715/lecture-notebooks/a2f7642b3ba31629fc15ca77b738e3f21a5e6c8c/Lecture 17/Lecture 17.pdf -------------------------------------------------------------------------------- /Lecture 17/Project.toml: -------------------------------------------------------------------------------- 1 | [deps] 2 | OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" 3 | -------------------------------------------------------------------------------- /Lecture 17/pendulum-damped.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "eff6e7bf", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "import Pkg; Pkg.activate(@__DIR__); Pkg.instantiate()" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": null, 16 | "id": "33eae950", 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "using LinearAlgebra\n", 21 | "using OrdinaryDiffEq\n", 22 | "using ForwardDiff\n", 23 | "using Plots" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": null, 29 | "id": "e97a3945", 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "#Pendulum Parameters\n", 34 | "ℓ = 1.0\n", 35 | "m = 1.0\n", 36 | "g = 9.81\n", 37 | "c = 0.25" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": null, 43 | "id": "b276c67e", 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "#Reference Solution w/ODE Solver\n", 48 | "\n", 49 | "#Classical pendulum dynamics\n", 50 | "function f(x,p,t)\n", 51 | " q = x[1]\n", 52 | " v = x[2]\n", 53 | " \n", 54 | " v̇ = -(g/ℓ)*sin(q) - c*v\n", 55 | " \n", 56 | " ẋ = [v; v̇]\n", 57 | "end\n", 58 | "\n", 59 | "#initial conditions\n", 60 | "x0 = [pi/2; 0]\n", 61 | "\n", 62 | "#Simulate\n", 63 | "tspan = (0.0,50.0)\n", 64 | "prob = ODEProblem(f,x0,tspan)\n", 65 | "sol = solve(prob,Tsit5());\n", 66 | "#sol = solve(prob,Tsit5(),abstol=1e-5,reltol=1e-5);\n", 67 | "#sol = solve(prob,Tsit5(),abstol=1e-6,reltol=1e-6);" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": null, 73 | "id": "9e5cbbb4", 74 | "metadata": {}, 75 | "outputs": [], 76 | "source": [ 77 | "plot(sol,idxs=(0,1))" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "id": "a486d4c9", 84 | "metadata": {}, 85 | "outputs": [], 86 | "source": [ 87 | "#Energy Functions\n", 88 | "\n", 89 | "function T(θ̇)\n", 90 | " 0.5*m*ℓ*ℓ*θ̇*θ̇\n", 91 | "end\n", 92 | "\n", 93 | "function U(θ)\n", 94 | " m*g*ℓ*(1-cos(θ))\n", 95 | "end\n", 96 | "\n", 97 | "function H(x)\n", 98 | " U(x[1]) + T(x[2])\n", 99 | "end" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": null, 105 | "id": "401ba644", 106 | "metadata": {}, 107 | "outputs": [], 108 | "source": [ 109 | "#Define discrete Lagrangian and DEL equation\n", 110 | "\n", 111 | "function L(q,q̇)\n", 112 | " T(q̇) - U(q)\n", 113 | "end\n", 114 | "\n", 115 | "function Ld(q1,q2)\n", 116 | " θm = 0.5*(q1+q2)\n", 117 | " θ̇m = (q2-q1)/h\n", 118 | " return h*L(θm,θ̇m)\n", 119 | "end\n", 120 | "\n", 121 | "function DEL(q1,q2,q3)\n", 122 | " ForwardDiff.derivative(dq2->Ld(q1,dq2),q2) + ForwardDiff.derivative(dq2->Ld(dq2,q3),q2) - (h/2)*c*(q2-q1)/h - (h/2)*c*(q3-q2)/h\n", 123 | "end" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": null, 129 | "id": "f4330150", 130 | "metadata": {}, 131 | "outputs": [], 132 | "source": [ 133 | "#Simulate using DEL equation\n", 134 | "\n", 135 | "h = 0.05 #time step\n", 136 | "tk = 0:h:tspan[2]\n", 137 | "\n", 138 | "#Initial conditions taken from reference solution\n", 139 | "q1 = sol(tk[1])[1]\n", 140 | "q2 = sol(tk[2])[1]\n", 141 | "\n", 142 | "qhist = zeros(length(tk))\n", 143 | "qhist[1] = q1\n", 144 | "qhist[2] = q2\n", 145 | "\n", 146 | "for k = 2:(length(tk)-1)\n", 147 | " qhist[k+1] = qhist[k]\n", 148 | " r = DEL(qhist[k-1],qhist[k],qhist[k+1])\n", 149 | " \n", 150 | " #Newton's method\n", 151 | " while norm(r) > 1e-12\n", 152 | " R = ForwardDiff.derivative(dq3->DEL(qhist[k-1],qhist[k],dq3),qhist[k+1])\n", 153 | " qhist[k+1] = qhist[k+1] - R\\r\n", 154 | " r = DEL(qhist[k-1],qhist[k],qhist[k+1])\n", 155 | " end\n", 156 | "end" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": null, 162 | "id": "a8628211", 163 | "metadata": {}, 164 | "outputs": [], 165 | "source": [ 166 | "plot(tk,qhist)\n", 167 | "plot!(sol,idxs=(0,1))" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": null, 173 | "id": "2ef81491", 174 | "metadata": {}, 175 | "outputs": [], 176 | "source": [] 177 | } 178 | ], 179 | "metadata": { 180 | "kernelspec": { 181 | "display_name": "Julia 1.6.7", 182 | "language": "julia", 183 | "name": "julia-1.6" 184 | }, 185 | "language_info": { 186 | "file_extension": ".jl", 187 | "mimetype": "application/julia", 188 | "name": "julia", 189 | "version": "1.6.7" 190 | } 191 | }, 192 | "nbformat": 4, 193 | "nbformat_minor": 5 194 | } 195 | -------------------------------------------------------------------------------- /Lecture 17/pendulum-legendre.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "07c5fc2d", 7 | "metadata": { 8 | "tags": [] 9 | }, 10 | "outputs": [], 11 | "source": [ 12 | "import Pkg; Pkg.activate(@__DIR__); Pkg.instantiate()" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": null, 18 | "id": "2da97d0b", 19 | "metadata": {}, 20 | "outputs": [], 21 | "source": [ 22 | "using LinearAlgebra\n", 23 | "using OrdinaryDiffEq\n", 24 | "using ForwardDiff\n", 25 | "using Plots" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": null, 31 | "id": "0665a6c7", 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "#Pendulum Parameters\n", 36 | "ℓ = 1.0\n", 37 | "m = 1.0\n", 38 | "g = 9.81" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": null, 44 | "id": "d78dd765", 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "#Reference Solution w/ODE Solver\n", 49 | "\n", 50 | "#Classical pendulum dynamics\n", 51 | "function f(x,p,t)\n", 52 | " q = x[1]\n", 53 | " v = x[2]\n", 54 | " \n", 55 | " v̇ = -(g/ℓ)*sin(q)\n", 56 | " \n", 57 | " ẋ = [v; v̇]\n", 58 | "end\n", 59 | "\n", 60 | "#initial conditions\n", 61 | "x0 = [pi/2; 0]\n", 62 | "\n", 63 | "#Simulate\n", 64 | "tspan = (0.0,100.0)\n", 65 | "prob = ODEProblem(f,x0,tspan)\n", 66 | "sol = solve(prob,Tsit5());\n", 67 | "#sol = solve(prob,Tsit5(),abstol=1e-5,reltol=1e-5);\n", 68 | "#sol = solve(prob,Tsit5(),abstol=1e-6,reltol=1e-6);" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": null, 74 | "id": "a61a1d13", 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [ 78 | "plot(sol,idxs=(0,1))" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": null, 84 | "id": "5cc9ff61", 85 | "metadata": {}, 86 | "outputs": [], 87 | "source": [ 88 | "#Energy Functions\n", 89 | "\n", 90 | "function T(θ̇)\n", 91 | " 0.5*m*ℓ*ℓ*θ̇*θ̇\n", 92 | "end\n", 93 | "\n", 94 | "function U(θ)\n", 95 | " m*g*ℓ*(1-cos(θ))\n", 96 | "end\n", 97 | "\n", 98 | "function H(x)\n", 99 | " U(x[1]) + T(x[2])\n", 100 | "end" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": null, 106 | "id": "1ad4c46d", 107 | "metadata": {}, 108 | "outputs": [], 109 | "source": [ 110 | "#Plot total energy\n", 111 | "h = 0.01 #time step\n", 112 | "tk = 0:h:tspan[2]\n", 113 | "\n", 114 | "N = length(tk)\n", 115 | "E = zeros(N)\n", 116 | "for k = 1:N\n", 117 | " E[k] = H(sol(tk[k]))\n", 118 | "end\n", 119 | "\n", 120 | "plot(tk,E)" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "id": "fd1a422b-b4cd-467b-a044-8e15aaafc953", 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [ 130 | "h = 0.001 #time step\n", 131 | "tk = 0:h:100" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": null, 137 | "id": "02634e5a-d8d9-4ec5-898f-fce3c70ec624", 138 | "metadata": {}, 139 | "outputs": [], 140 | "source": [] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": null, 145 | "id": "5f0dd0e1", 146 | "metadata": {}, 147 | "outputs": [], 148 | "source": [ 149 | "#Define discrete Lagrangian\n", 150 | "function L(q,q̇)\n", 151 | " T(q̇) - U(q)\n", 152 | "end\n", 153 | "\n", 154 | "function Ld(q1,q2)\n", 155 | " θm = 0.5*(q1+q2)\n", 156 | " θ̇m = (q2-q1)/h\n", 157 | " return h*L(θm,θ̇m)\n", 158 | "end" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": null, 164 | "id": "0015d923", 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [ 168 | "#Let's do this again with the discrete Legendre transform\n", 169 | "\n", 170 | "function D1Ld(q1,q2)\n", 171 | " ForwardDiff.derivative(dq1->Ld(dq1,q2),q1)\n", 172 | "end\n", 173 | "\n", 174 | "function D2Ld(q1,q2)\n", 175 | " ForwardDiff.derivative(dq2->Ld(q1,dq2),q2)\n", 176 | "end\n", 177 | "\n", 178 | "#Initial conditions\n", 179 | "q1 = x0[1]\n", 180 | "v1 = x0[2]\n", 181 | "p1 = m*v1\n", 182 | "\n", 183 | "qhist = zeros(length(tk))\n", 184 | "qhist[1] = q1\n", 185 | "phist = zeros(length(tk))\n", 186 | "phist[1] = p1\n", 187 | "\n", 188 | "for k = 1:(length(tk)-1)\n", 189 | " qhist[k+1] = qhist[k]\n", 190 | " \n", 191 | " #Calculate residual with right discrete Legendre transform\n", 192 | " r = phist[k] + D1Ld(qhist[k],qhist[k+1])\n", 193 | " \n", 194 | " #Newton's method to solve for q_k+1\n", 195 | " while norm(r) > 1e-12\n", 196 | " R = ForwardDiff.derivative(dqn->D1Ld(qhist[k],dqn),qhist[k+1])\n", 197 | " qhist[k+1] = qhist[k+1] - R\\r\n", 198 | " r = phist[k] + D1Ld(qhist[k],qhist[k+1])\n", 199 | " end\n", 200 | " \n", 201 | " #Update momentum (left discrete Legendre transform)\n", 202 | " phist[k+1] = D2Ld(qhist[k],qhist[k+1])\n", 203 | "end" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": null, 209 | "id": "05640b94", 210 | "metadata": {}, 211 | "outputs": [], 212 | "source": [ 213 | "plot(qhist)" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": null, 219 | "id": "f320a139", 220 | "metadata": {}, 221 | "outputs": [], 222 | "source": [ 223 | "#Plot energy\n", 224 | "\n", 225 | "E2 = zeros(length(tk))\n", 226 | "for k = 1:(length(tk))\n", 227 | " E2[k] = (0.5/m)*phist[k]*phist[k] + U(qhist[k])\n", 228 | "end\n", 229 | "\n", 230 | "plot(E2)\n", 231 | "#plot!(E)" 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": null, 237 | "id": "0335da0c", 238 | "metadata": {}, 239 | "outputs": [], 240 | "source": [ 241 | "#Implicit Midpoint\n", 242 | "\n", 243 | "xhist = zeros(2,length(tk))\n", 244 | "xhist[:,1] .= x0\n", 245 | "\n", 246 | "for k = 1:(length(tk)-1)\n", 247 | " xhist[:,k+1] .= xhist[:,k]\n", 248 | " r = xhist[:,k+1] - xhist[:,k] - h*f(0.5*(xhist[:,k+1]+xhist[:,k]),[],[])\n", 249 | " \n", 250 | " #Newton's method\n", 251 | " while norm(r) > 1e-12\n", 252 | " R = I - 0.5*h*ForwardDiff.jacobian(dx->f(dx,[],[]),0.5*(xhist[:,k+1]+xhist[:,k]))\n", 253 | " xhist[:,k+1] .= xhist[:,k+1] - R\\r\n", 254 | " r = xhist[:,k+1] - xhist[:,k] - h*f(0.5*(xhist[:,k+1]+xhist[:,k]),[],[])\n", 255 | " end\n", 256 | "end" 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": null, 262 | "id": "d433a82f", 263 | "metadata": {}, 264 | "outputs": [], 265 | "source": [ 266 | "plot(tk,xhist[1,:])\n", 267 | "plot!(tk,qhist)" 268 | ] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": null, 273 | "id": "e733e499", 274 | "metadata": {}, 275 | "outputs": [], 276 | "source": [ 277 | "#Plot total energy\n", 278 | "\n", 279 | "E3 = zeros(length(tk))\n", 280 | "for k = 1:(length(tk))\n", 281 | " E3[k] = H(xhist[:,k])\n", 282 | "end" 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "execution_count": null, 288 | "id": "523520bb", 289 | "metadata": {}, 290 | "outputs": [], 291 | "source": [ 292 | "plot(E2)\n", 293 | "plot!(E3)" 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": null, 299 | "id": "20a9a10c", 300 | "metadata": {}, 301 | "outputs": [], 302 | "source": [ 303 | "qhist-xhist[1,:]" 304 | ] 305 | }, 306 | { 307 | "cell_type": "code", 308 | "execution_count": null, 309 | "id": "c23c8a0a", 310 | "metadata": {}, 311 | "outputs": [], 312 | "source": [ 313 | "phist-xhist[2,:]" 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": null, 319 | "id": "1d6aacba", 320 | "metadata": {}, 321 | "outputs": [], 322 | "source": [] 323 | } 324 | ], 325 | "metadata": { 326 | "kernelspec": { 327 | "display_name": "Julia 1.6.7", 328 | "language": "julia", 329 | "name": "julia-1.6" 330 | }, 331 | "language_info": { 332 | "file_extension": ".jl", 333 | "mimetype": "application/julia", 334 | "name": "julia", 335 | "version": "1.6.7" 336 | } 337 | }, 338 | "nbformat": 4, 339 | "nbformat_minor": 5 340 | } 341 | -------------------------------------------------------------------------------- /Lecture 17/variational-constraints.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "b52894f4", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "import Pkg; Pkg.activate(@__DIR__); Pkg.instantiate()" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": null, 16 | "id": "4c493956", 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "using LinearAlgebra\n", 21 | "using OrdinaryDiffEq\n", 22 | "using ForwardDiff\n", 23 | "using Plots" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": null, 29 | "id": "fe160263", 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "#Pendulum Parameters\n", 34 | "ℓ = 1.0\n", 35 | "m = 1.0\n", 36 | "g = 9.81" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": null, 42 | "id": "fcfdb15d", 43 | "metadata": {}, 44 | "outputs": [], 45 | "source": [ 46 | "#Reference solution with Baumgarte stabilization\n", 47 | "function f(x,p,t)\n", 48 | " q = x[1:2]\n", 49 | " v = x[3:4]\n", 50 | " \n", 51 | " α = 10.0\n", 52 | " β = 3.0\n", 53 | " \n", 54 | " M = m*I(2)\n", 55 | " G = [0.0; m*g]\n", 56 | " J = dcdq(q)\n", 57 | " \n", 58 | " c = constraint(q)\n", 59 | " ċ = J*v\n", 60 | " d = 2*v'*v\n", 61 | " e = J*(M\\(J'*(α*c + β*ċ)))\n", 62 | " \n", 63 | " z = [M J'; J 0]\\[-G; -(d+e)]\n", 64 | " \n", 65 | " v̇ = z[1:2]\n", 66 | " \n", 67 | " ẋ = [v; v̇]\n", 68 | "end\n", 69 | "\n", 70 | "function constraint(q)\n", 71 | " return q[1]^2 + q[2]^2 - ℓ^2\n", 72 | "end\n", 73 | "\n", 74 | "function dcdq(q)\n", 75 | " return [2*q[1]; 2*q[2]]'\n", 76 | "end" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": null, 82 | "id": "11c7fbea", 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "#initial conditions\n", 87 | "q0 = [1.0; 0]\n", 88 | "x0 = [q0; 0; 0]" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": null, 94 | "id": "c8ec96a7", 95 | "metadata": {}, 96 | "outputs": [], 97 | "source": [ 98 | "#Simulate\n", 99 | "tspan = (0.0,10.0)\n", 100 | "prob = ODEProblem(f,x0,tspan)\n", 101 | "sol = solve(prob,Tsit5());\n", 102 | "#sol = solve(prob,Tsit5(),abstol=1e-6,reltol=1e-6);\n", 103 | "plot(sol)" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": null, 109 | "id": "0407fa3b", 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [ 113 | "#Calculate constraint violation\n", 114 | "N = 1000\n", 115 | "t = LinRange(tspan[1],tspan[2],N)\n", 116 | "c_error = zeros(N)\n", 117 | "for k = 1:N\n", 118 | " c_error[k] = constraint(sol(t[k])[1:2])\n", 119 | "end\n", 120 | "\n", 121 | "plot(t,c_error)" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": null, 127 | "id": "9652109c", 128 | "metadata": {}, 129 | "outputs": [], 130 | "source": [ 131 | "h = 0.05\n", 132 | "t = 0:h:10.0\n", 133 | "\n", 134 | "function T(q,q̇)\n", 135 | " 0.5*m*q̇'*q̇\n", 136 | "end\n", 137 | "\n", 138 | "function U(q)\n", 139 | " m*g*q[2]\n", 140 | "end\n", 141 | "\n", 142 | "function L(q,q̇)\n", 143 | " return T(q,q̇) - U(q)\n", 144 | "end\n", 145 | "\n", 146 | "function Ld(q1,q2)\n", 147 | " qm = 0.5*(q1+q2)\n", 148 | " q̇m = (q2-q1)/h\n", 149 | " return h*L(qm,q̇m)\n", 150 | "end\n", 151 | "\n", 152 | "function DEL(q1,q2,q3,λ)\n", 153 | " ForwardDiff.gradient(dq2->Ld(q1,dq2),q2) + ForwardDiff.gradient(dq2->Ld(dq2,q3),q2) + h*λ*dcdq(q2)'\n", 154 | "end" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": null, 160 | "id": "ec7a57e5", 161 | "metadata": {}, 162 | "outputs": [], 163 | "source": [ 164 | "qhist = zeros(2,length(t))\n", 165 | "qhist[:,1] .= q0\n", 166 | "qhist[:,2] .= sol(h)[1:2]\n", 167 | "\n", 168 | "for k = 2:(length(t)-1)\n", 169 | " #Initial guess for Newton\n", 170 | " qhist[:,k+1] .= qhist[:,k]\n", 171 | " λ = 0.0\n", 172 | " \n", 173 | " #KKT residual\n", 174 | " r = [DEL(qhist[:,k-1],qhist[:,k],qhist[:,k+1],λ); constraint(qhist[:,k+1])]\n", 175 | " \n", 176 | " #Newton's method\n", 177 | " while maximum(abs.(r)) > 1e-12\n", 178 | " drdq3 = ForwardDiff.jacobian(dq3->DEL(qhist[:,k-1],qhist[:,k],dq3,λ),qhist[:,k+1])\n", 179 | " drdλ = ForwardDiff.derivative(dλ->DEL(qhist[:,k-1],qhist[:,k],qhist[:,k+1],dλ),λ)\n", 180 | " \n", 181 | " Δ = -[drdq3 drdλ; dcdq(qhist[:,k+1]) 0]\\r\n", 182 | " qhist[:,k+1] .= qhist[:,k+1] + Δ[1:2]\n", 183 | " λ = λ + Δ[3]\n", 184 | " r = [DEL(qhist[:,k-1],qhist[:,k],qhist[:,k+1],λ); constraint(qhist[:,k+1])]\n", 185 | " end\n", 186 | "end" 187 | ] 188 | }, 189 | { 190 | "cell_type": "code", 191 | "execution_count": null, 192 | "id": "34821eba", 193 | "metadata": {}, 194 | "outputs": [], 195 | "source": [ 196 | "plot(sol,idxs=(0,1))\n", 197 | "plot!(t,qhist[1,:])" 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": null, 203 | "id": "43e0d3f7", 204 | "metadata": {}, 205 | "outputs": [], 206 | "source": [ 207 | "#Calculate constraint violation\n", 208 | "N = length(t)\n", 209 | "c_error1 = zeros(N)\n", 210 | "c_error2 = zeros(N)\n", 211 | "for k = 1:N\n", 212 | " c_error1[k] = constraint(sol(t[k])[1:2])\n", 213 | " c_error2[k] = constraint(qhist[:,k])\n", 214 | "end\n", 215 | "\n", 216 | "plot(t,c_error1)\n", 217 | "plot!(t,c_error2)" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": null, 223 | "id": "78f5a7ce", 224 | "metadata": {}, 225 | "outputs": [], 226 | "source": [] 227 | } 228 | ], 229 | "metadata": { 230 | "kernelspec": { 231 | "display_name": "Julia 1.6.7", 232 | "language": "julia", 233 | "name": "julia-1.6" 234 | }, 235 | "language_info": { 236 | "file_extension": ".jl", 237 | "mimetype": "application/julia", 238 | "name": "julia", 239 | "version": "1.6.7" 240 | } 241 | }, 242 | "nbformat": 4, 243 | "nbformat_minor": 5 244 | } 245 | -------------------------------------------------------------------------------- /Lecture 18/Lecture 18.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamics-simulation-16-715/lecture-notebooks/a2f7642b3ba31629fc15ca77b738e3f21a5e6c8c/Lecture 18/Lecture 18.pdf -------------------------------------------------------------------------------- /Lecture 18/Manifest.toml: -------------------------------------------------------------------------------- 1 | # This file is machine-generated - editing it directly is not advised 2 | 3 | [[ASL_jll]] 4 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 5 | git-tree-sha1 = "6252039f98492252f9e47c312c8ffda0e3b9e78d" 6 | uuid = "ae81ac8f-d209-56e5-92de-9978fef736f9" 7 | version = "0.1.3+0" 8 | 9 | [[ArgTools]] 10 | uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f" 11 | 12 | [[Artifacts]] 13 | uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33" 14 | 15 | [[Base64]] 16 | uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" 17 | 18 | [[BenchmarkTools]] 19 | deps = ["JSON", "Logging", "Printf", "Profile", "Statistics", "UUIDs"] 20 | git-tree-sha1 = "d9a9701b899b30332bbcb3e1679c41cce81fb0e8" 21 | uuid = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" 22 | version = "1.3.2" 23 | 24 | [[Bzip2_jll]] 25 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 26 | git-tree-sha1 = "19a35467a82e236ff51bc17a3a44b69ef35185a2" 27 | uuid = "6e34b625-4abd-537c-b88f-471c36dfa7a0" 28 | version = "1.0.8+0" 29 | 30 | [[ChainRulesCore]] 31 | deps = ["Compat", "LinearAlgebra", "SparseArrays"] 32 | git-tree-sha1 = "e7ff6cadf743c098e08fca25c91103ee4303c9bb" 33 | uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" 34 | version = "1.15.6" 35 | 36 | [[ChangesOfVariables]] 37 | deps = ["ChainRulesCore", "LinearAlgebra", "Test"] 38 | git-tree-sha1 = "38f7a08f19d8810338d4f5085211c7dfa5d5bdd8" 39 | uuid = "9e997f8a-9a97-42d5-a9f1-ce6bfc15e2c0" 40 | version = "0.1.4" 41 | 42 | [[CodecBzip2]] 43 | deps = ["Bzip2_jll", "Libdl", "TranscodingStreams"] 44 | git-tree-sha1 = "2e62a725210ce3c3c2e1a3080190e7ca491f18d7" 45 | uuid = "523fee87-0ab8-5b00-afb7-3ecf72e48cfd" 46 | version = "0.7.2" 47 | 48 | [[CodecZlib]] 49 | deps = ["TranscodingStreams", "Zlib_jll"] 50 | git-tree-sha1 = "ded953804d019afa9a3f98981d99b33e3db7b6da" 51 | uuid = "944b1d66-785c-5afd-91f1-9de20f533193" 52 | version = "0.7.0" 53 | 54 | [[CommonSubexpressions]] 55 | deps = ["MacroTools", "Test"] 56 | git-tree-sha1 = "7b8a93dba8af7e3b42fecabf646260105ac373f7" 57 | uuid = "bbf7d656-a473-5ed7-a52c-81e309532950" 58 | version = "0.3.0" 59 | 60 | [[Compat]] 61 | deps = ["Dates", "LinearAlgebra", "UUIDs"] 62 | git-tree-sha1 = "3ca828fe1b75fa84b021a7860bd039eaea84d2f2" 63 | uuid = "34da2185-b29b-5c13-b0c7-acf172513d20" 64 | version = "4.3.0" 65 | 66 | [[CompilerSupportLibraries_jll]] 67 | deps = ["Artifacts", "Libdl"] 68 | uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae" 69 | 70 | [[DataStructures]] 71 | deps = ["Compat", "InteractiveUtils", "OrderedCollections"] 72 | git-tree-sha1 = "d1fff3a548102f48987a52a2e0d114fa97d730f0" 73 | uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" 74 | version = "0.18.13" 75 | 76 | [[Dates]] 77 | deps = ["Printf"] 78 | uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" 79 | 80 | [[DiffResults]] 81 | deps = ["StaticArraysCore"] 82 | git-tree-sha1 = "782dd5f4561f5d267313f23853baaaa4c52ea621" 83 | uuid = "163ba53b-c6d8-5494-b064-1a9d43ac40c5" 84 | version = "1.1.0" 85 | 86 | [[DiffRules]] 87 | deps = ["IrrationalConstants", "LogExpFunctions", "NaNMath", "Random", "SpecialFunctions"] 88 | git-tree-sha1 = "8b7a4d23e22f5d44883671da70865ca98f2ebf9d" 89 | uuid = "b552c78f-8df3-52c6-915a-8e097449b14b" 90 | version = "1.12.0" 91 | 92 | [[DocStringExtensions]] 93 | deps = ["LibGit2"] 94 | git-tree-sha1 = "c36550cb29cbe373e95b3f40486b9a4148f89ffd" 95 | uuid = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" 96 | version = "0.9.2" 97 | 98 | [[Downloads]] 99 | deps = ["ArgTools", "LibCURL", "NetworkOptions"] 100 | uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6" 101 | 102 | [[ForwardDiff]] 103 | deps = ["CommonSubexpressions", "DiffResults", "DiffRules", "LinearAlgebra", "LogExpFunctions", "NaNMath", "Preferences", "Printf", "Random", "SpecialFunctions", "StaticArrays"] 104 | git-tree-sha1 = "187198a4ed8ccd7b5d99c41b69c679269ea2b2d4" 105 | uuid = "f6369f11-7733-5829-9624-2563aa707210" 106 | version = "0.10.32" 107 | 108 | [[InteractiveUtils]] 109 | deps = ["Markdown"] 110 | uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" 111 | 112 | [[InverseFunctions]] 113 | deps = ["Test"] 114 | git-tree-sha1 = "49510dfcb407e572524ba94aeae2fced1f3feb0f" 115 | uuid = "3587e190-3f89-42d0-90ee-14403ec27112" 116 | version = "0.1.8" 117 | 118 | [[Ipopt]] 119 | deps = ["Ipopt_jll", "MathOptInterface"] 120 | git-tree-sha1 = "14a305ededd75330246aaa0380130561d8924120" 121 | uuid = "b6b21f68-93f8-5de0-b562-5493be1d77c9" 122 | version = "1.1.0" 123 | 124 | [[Ipopt_jll]] 125 | deps = ["ASL_jll", "Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "MUMPS_seq_jll", "OpenBLAS32_jll", "Pkg"] 126 | git-tree-sha1 = "e3e202237d93f18856b6ff1016166b0f172a49a8" 127 | uuid = "9cc047cb-c261-5740-88fc-0cf96f7bdcc7" 128 | version = "300.1400.400+0" 129 | 130 | [[IrrationalConstants]] 131 | git-tree-sha1 = "7fd44fd4ff43fc60815f8e764c0f352b83c49151" 132 | uuid = "92d709cd-6900-40b7-9082-c6be49f344b6" 133 | version = "0.1.1" 134 | 135 | [[JLLWrappers]] 136 | deps = ["Preferences"] 137 | git-tree-sha1 = "abc9885a7ca2052a736a600f7fa66209f96506e1" 138 | uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210" 139 | version = "1.4.1" 140 | 141 | [[JSON]] 142 | deps = ["Dates", "Mmap", "Parsers", "Unicode"] 143 | git-tree-sha1 = "3c837543ddb02250ef42f4738347454f95079d4e" 144 | uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" 145 | version = "0.21.3" 146 | 147 | [[LibCURL]] 148 | deps = ["LibCURL_jll", "MozillaCACerts_jll"] 149 | uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21" 150 | 151 | [[LibCURL_jll]] 152 | deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"] 153 | uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0" 154 | 155 | [[LibGit2]] 156 | deps = ["Base64", "NetworkOptions", "Printf", "SHA"] 157 | uuid = "76f85450-5226-5b5a-8eaa-529ad045b433" 158 | 159 | [[LibSSH2_jll]] 160 | deps = ["Artifacts", "Libdl", "MbedTLS_jll"] 161 | uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8" 162 | 163 | [[Libdl]] 164 | uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" 165 | 166 | [[LinearAlgebra]] 167 | deps = ["Libdl"] 168 | uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" 169 | 170 | [[LogExpFunctions]] 171 | deps = ["ChainRulesCore", "ChangesOfVariables", "DocStringExtensions", "InverseFunctions", "IrrationalConstants", "LinearAlgebra"] 172 | git-tree-sha1 = "94d9c52ca447e23eac0c0f074effbcd38830deb5" 173 | uuid = "2ab3a3ac-af41-5b50-aa03-7779005ae688" 174 | version = "0.3.18" 175 | 176 | [[Logging]] 177 | uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" 178 | 179 | [[METIS_jll]] 180 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 181 | git-tree-sha1 = "1fd0a97409e418b78c53fac671cf4622efdf0f21" 182 | uuid = "d00139f3-1899-568f-a2f0-47f597d42d70" 183 | version = "5.1.2+0" 184 | 185 | [[MUMPS_seq_jll]] 186 | deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "METIS_jll", "OpenBLAS32_jll", "Pkg"] 187 | git-tree-sha1 = "29de2841fa5aefe615dea179fcde48bb87b58f57" 188 | uuid = "d7ed1dd3-d0ae-5e8e-bfb4-87a502085b8d" 189 | version = "5.4.1+0" 190 | 191 | [[MacroTools]] 192 | deps = ["Markdown", "Random"] 193 | git-tree-sha1 = "42324d08725e200c23d4dfb549e0d5d89dede2d2" 194 | uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" 195 | version = "0.5.10" 196 | 197 | [[Markdown]] 198 | deps = ["Base64"] 199 | uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" 200 | 201 | [[MathOptInterface]] 202 | deps = ["BenchmarkTools", "CodecBzip2", "CodecZlib", "DataStructures", "ForwardDiff", "JSON", "LinearAlgebra", "MutableArithmetics", "NaNMath", "OrderedCollections", "Printf", "SparseArrays", "SpecialFunctions", "Test", "Unicode"] 203 | git-tree-sha1 = "ceed48edffe0325a6e9ea00ecf3607af5089c413" 204 | uuid = "b8f27783-ece8-5eb3-8dc8-9495eed66fee" 205 | version = "1.9.0" 206 | 207 | [[MbedTLS_jll]] 208 | deps = ["Artifacts", "Libdl"] 209 | uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1" 210 | 211 | [[Mmap]] 212 | uuid = "a63ad114-7e13-5084-954f-fe012c677804" 213 | 214 | [[MozillaCACerts_jll]] 215 | uuid = "14a3606d-f60d-562e-9121-12d972cd8159" 216 | 217 | [[MutableArithmetics]] 218 | deps = ["LinearAlgebra", "SparseArrays", "Test"] 219 | git-tree-sha1 = "1d57a7dc42d563ad6b5e95d7a8aebd550e5162c0" 220 | uuid = "d8a4904e-b15c-11e9-3269-09a3773c0cb0" 221 | version = "1.0.5" 222 | 223 | [[NaNMath]] 224 | deps = ["OpenLibm_jll"] 225 | git-tree-sha1 = "a7c3d1da1189a1c2fe843a3bfa04d18d20eb3211" 226 | uuid = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3" 227 | version = "1.0.1" 228 | 229 | [[NetworkOptions]] 230 | uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908" 231 | 232 | [[OpenBLAS32_jll]] 233 | deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "Pkg"] 234 | git-tree-sha1 = "ba4a8f683303c9082e84afba96f25af3c7fb2436" 235 | uuid = "656ef2d0-ae68-5445-9ca0-591084a874a2" 236 | version = "0.3.12+1" 237 | 238 | [[OpenLibm_jll]] 239 | deps = ["Artifacts", "Libdl"] 240 | uuid = "05823500-19ac-5b8b-9628-191a04bc5112" 241 | 242 | [[OpenSpecFun_jll]] 243 | deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "Pkg"] 244 | git-tree-sha1 = "13652491f6856acfd2db29360e1bbcd4565d04f1" 245 | uuid = "efe28fd5-8261-553b-a9e1-b2916fc3738e" 246 | version = "0.5.5+0" 247 | 248 | [[OrderedCollections]] 249 | git-tree-sha1 = "85f8e6578bf1f9ee0d11e7bb1b1456435479d47c" 250 | uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" 251 | version = "1.4.1" 252 | 253 | [[Parsers]] 254 | deps = ["Dates", "SnoopPrecompile"] 255 | git-tree-sha1 = "cceb0257b662528ecdf0b4b4302eb00e767b38e7" 256 | uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0" 257 | version = "2.5.0" 258 | 259 | [[Pkg]] 260 | deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"] 261 | uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" 262 | 263 | [[Preferences]] 264 | deps = ["TOML"] 265 | git-tree-sha1 = "47e5f437cc0e7ef2ce8406ce1e7e24d44915f88d" 266 | uuid = "21216c6a-2e73-6563-6e65-726566657250" 267 | version = "1.3.0" 268 | 269 | [[Printf]] 270 | deps = ["Unicode"] 271 | uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" 272 | 273 | [[Profile]] 274 | deps = ["Printf"] 275 | uuid = "9abbd945-dff8-562f-b5e8-e1ebf5ef1b79" 276 | 277 | [[REPL]] 278 | deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"] 279 | uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" 280 | 281 | [[Random]] 282 | deps = ["Serialization"] 283 | uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" 284 | 285 | [[SHA]] 286 | uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" 287 | 288 | [[Serialization]] 289 | uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" 290 | 291 | [[SnoopPrecompile]] 292 | git-tree-sha1 = "f604441450a3c0569830946e5b33b78c928e1a85" 293 | uuid = "66db9d55-30c0-4569-8b51-7e840670fc0c" 294 | version = "1.0.1" 295 | 296 | [[Sockets]] 297 | uuid = "6462fe0b-24de-5631-8697-dd941f90decc" 298 | 299 | [[SparseArrays]] 300 | deps = ["LinearAlgebra", "Random"] 301 | uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" 302 | 303 | [[SpecialFunctions]] 304 | deps = ["ChainRulesCore", "IrrationalConstants", "LogExpFunctions", "OpenLibm_jll", "OpenSpecFun_jll"] 305 | git-tree-sha1 = "d75bda01f8c31ebb72df80a46c88b25d1c79c56d" 306 | uuid = "276daf66-3868-5448-9aa4-cd146d93841b" 307 | version = "2.1.7" 308 | 309 | [[StaticArrays]] 310 | deps = ["LinearAlgebra", "Random", "StaticArraysCore", "Statistics"] 311 | git-tree-sha1 = "f86b3a049e5d05227b10e15dbb315c5b90f14988" 312 | uuid = "90137ffa-7385-5640-81b9-e52037218182" 313 | version = "1.5.9" 314 | 315 | [[StaticArraysCore]] 316 | git-tree-sha1 = "6b7ba252635a5eff6a0b0664a41ee140a1c9e72a" 317 | uuid = "1e83bf80-4336-4d27-bf5d-d5a4f845583c" 318 | version = "1.4.0" 319 | 320 | [[Statistics]] 321 | deps = ["LinearAlgebra", "SparseArrays"] 322 | uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" 323 | 324 | [[TOML]] 325 | deps = ["Dates"] 326 | uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76" 327 | 328 | [[Tar]] 329 | deps = ["ArgTools", "SHA"] 330 | uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e" 331 | 332 | [[Test]] 333 | deps = ["InteractiveUtils", "Logging", "Random", "Serialization"] 334 | uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" 335 | 336 | [[TranscodingStreams]] 337 | deps = ["Random", "Test"] 338 | git-tree-sha1 = "8a75929dcd3c38611db2f8d08546decb514fcadf" 339 | uuid = "3bb67fe8-82b1-5028-8e26-92a6c54297fa" 340 | version = "0.9.9" 341 | 342 | [[UUIDs]] 343 | deps = ["Random", "SHA"] 344 | uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" 345 | 346 | [[Unicode]] 347 | uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" 348 | 349 | [[Zlib_jll]] 350 | deps = ["Libdl"] 351 | uuid = "83775a58-1f1d-513f-b197-d71354ab007a" 352 | 353 | [[nghttp2_jll]] 354 | deps = ["Artifacts", "Libdl"] 355 | uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d" 356 | 357 | [[p7zip_jll]] 358 | deps = ["Artifacts", "Libdl"] 359 | uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0" 360 | -------------------------------------------------------------------------------- /Lecture 18/Project.toml: -------------------------------------------------------------------------------- 1 | [deps] 2 | Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" 3 | MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee" 4 | -------------------------------------------------------------------------------- /Lecture 18/falling-particle.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "10f82cd7", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "import Pkg; Pkg.activate(@__DIR__); Pkg.instantiate()" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": null, 16 | "id": "9181ec76", 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "using LinearAlgebra\n", 21 | "using ForwardDiff\n", 22 | "using Plots\n", 23 | "using Ipopt\n", 24 | "using MathOptInterface\n", 25 | "const MOI = MathOptInterface" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": null, 31 | "id": "e0966c44", 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "#Parameters\n", 36 | "g = 9.81\n", 37 | "m = 1.0\n", 38 | "h = 0.05 #20 Hz\n", 39 | "Tf = 1.5 #final time (sec)\n", 40 | "thist = Array(0:h:Tf)\n", 41 | "N = length(thist)" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": null, 47 | "id": "f4393dfa", 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "#Initial Conditions\n", 52 | "q0 = [0; 3.0]\n", 53 | "v0 = [1.0; 0.0]\n", 54 | "q1 = q0 + h*v0 + 0.5*h*h*[0; -g]" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": null, 60 | "id": "a2b611b0", 61 | "metadata": {}, 62 | "outputs": [], 63 | "source": [ 64 | "#Signed-distance function for a particle in 2D\n", 65 | "function ϕ(q)\n", 66 | " return q[2]\n", 67 | "end\n", 68 | "\n", 69 | "Dϕ = [0; 1.0]'" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": null, 75 | "id": "07e4eec6", 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "#Discrete Lagrangian stuff\n", 80 | "function L(q,q̇)\n", 81 | " 0.5*m*q̇'*q̇ - m*g*q[2]\n", 82 | "end\n", 83 | "\n", 84 | "function Ld(q1,q2)\n", 85 | " qm = 0.5*(q1 + q2)\n", 86 | " vm = (q2-q1)/h\n", 87 | " return L(qm,vm)\n", 88 | "end\n", 89 | "\n", 90 | "function D1Ld(q1,q2)\n", 91 | " return ForwardDiff.gradient(dq1->Ld(dq1,q2),q1)\n", 92 | "end\n", 93 | "\n", 94 | "function D2Ld(q1,q2)\n", 95 | " return ForwardDiff.gradient(dq2->Ld(q1,dq2),q2)\n", 96 | "end" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": null, 102 | "id": "d28aaeb7", 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "function DEL(q1,q2,q3,n)\n", 107 | " D2Ld(q1,q2) + D1Ld(q2,q3) + h*Dϕ'*n\n", 108 | "end" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": null, 114 | "id": "a56748af", 115 | "metadata": {}, 116 | "outputs": [], 117 | "source": [ 118 | "#Boilerplate setup code to interface with IPOPT.\n", 119 | "\n", 120 | "struct ProblemMOI <: MOI.AbstractNLPEvaluator\n", 121 | " n_nlp::Int\n", 122 | " m_nlp::Int\n", 123 | " idx_ineq\n", 124 | " obj_grad::Bool\n", 125 | " con_jac::Bool\n", 126 | " sparsity_jac\n", 127 | " sparsity_hess\n", 128 | " primal_bounds\n", 129 | " constraint_bounds\n", 130 | " hessian_lagrangian::Bool\n", 131 | "end\n", 132 | "\n", 133 | "function ProblemMOI(n_nlp,m_nlp;\n", 134 | " idx_ineq=(1:0),\n", 135 | " obj_grad=true,\n", 136 | " con_jac=true,\n", 137 | " sparsity_jac=sparsity_jacobian(n_nlp,m_nlp),\n", 138 | " sparsity_hess=sparsity_hessian(n_nlp,m_nlp),\n", 139 | " primal_bounds=primal_bounds(n_nlp),\n", 140 | " constraint_bounds=constraint_bounds(m_nlp,idx_ineq=idx_ineq),\n", 141 | " hessian_lagrangian=false)\n", 142 | "\n", 143 | " ProblemMOI(n_nlp,m_nlp,\n", 144 | " idx_ineq,\n", 145 | " obj_grad,\n", 146 | " con_jac,\n", 147 | " sparsity_jac,\n", 148 | " sparsity_hess,\n", 149 | " primal_bounds,\n", 150 | " constraint_bounds,\n", 151 | " hessian_lagrangian)\n", 152 | "end\n", 153 | "\n", 154 | "function constraint_bounds(m; idx_ineq=(1:0))\n", 155 | " c_l = zeros(m)\n", 156 | "\n", 157 | " c_u = zeros(m)\n", 158 | " c_u[idx_ineq] .= Inf\n", 159 | " \n", 160 | " return c_l, c_u\n", 161 | "end\n", 162 | "\n", 163 | "function row_col!(row,col,r,c)\n", 164 | " for cc in c\n", 165 | " for rr in r\n", 166 | " push!(row,convert(Int,rr))\n", 167 | " push!(col,convert(Int,cc))\n", 168 | " end\n", 169 | " end\n", 170 | " return row, col\n", 171 | "end\n", 172 | "\n", 173 | "function sparsity_jacobian(n,m)\n", 174 | "\n", 175 | " row = []\n", 176 | " col = []\n", 177 | "\n", 178 | " r = 1:m\n", 179 | " c = 1:n\n", 180 | "\n", 181 | " row_col!(row,col,r,c)\n", 182 | "\n", 183 | " return collect(zip(row,col))\n", 184 | "end\n", 185 | "\n", 186 | "function sparsity_hessian(n,m)\n", 187 | "\n", 188 | " row = []\n", 189 | " col = []\n", 190 | "\n", 191 | " r = 1:m\n", 192 | " c = 1:n\n", 193 | "\n", 194 | " row_col!(row,col,r,c)\n", 195 | "\n", 196 | " return collect(zip(row,col))\n", 197 | "end\n", 198 | "\n", 199 | "function MOI.eval_objective(prob::MOI.AbstractNLPEvaluator, x)\n", 200 | " objective(x)\n", 201 | "end\n", 202 | "\n", 203 | "function MOI.eval_objective_gradient(prob::MOI.AbstractNLPEvaluator, grad_f, x)\n", 204 | " ForwardDiff.gradient!(grad_f,objective,x)\n", 205 | " return nothing\n", 206 | "end\n", 207 | "\n", 208 | "function MOI.eval_constraint(prob::MOI.AbstractNLPEvaluator,g,x)\n", 209 | " constraint!(g,x)\n", 210 | " return nothing\n", 211 | "end\n", 212 | "\n", 213 | "function MOI.eval_constraint_jacobian(prob::MOI.AbstractNLPEvaluator, jac, x)\n", 214 | " ForwardDiff.jacobian!(reshape(jac,prob.m_nlp,prob.n_nlp), constraint!, zeros(prob.m_nlp), x)\n", 215 | " return nothing\n", 216 | "end\n", 217 | "\n", 218 | "function MOI.features_available(prob::MOI.AbstractNLPEvaluator)\n", 219 | " return [:Grad, :Jac]\n", 220 | "end\n", 221 | "\n", 222 | "MOI.initialize(prob::MOI.AbstractNLPEvaluator, features) = nothing\n", 223 | "MOI.jacobian_structure(prob::MOI.AbstractNLPEvaluator) = prob.sparsity_jac\n", 224 | "\n", 225 | "function ipopt_solve(x0,prob::MOI.AbstractNLPEvaluator;\n", 226 | " tol=1.0e-6,c_tol=1.0e-6,max_iter=10000)\n", 227 | " x_l, x_u = prob.primal_bounds\n", 228 | " c_l, c_u = prob.constraint_bounds\n", 229 | "\n", 230 | " nlp_bounds = MOI.NLPBoundsPair.(c_l,c_u)\n", 231 | " block_data = MOI.NLPBlockData(nlp_bounds,prob,true)\n", 232 | "\n", 233 | " solver = Ipopt.Optimizer()\n", 234 | " solver.options[\"max_iter\"] = max_iter\n", 235 | " solver.options[\"tol\"] = tol\n", 236 | " solver.options[\"constr_viol_tol\"] = c_tol\n", 237 | " \n", 238 | " #Uncomment the following line to turn off verbose IPOPT output\n", 239 | " solver.options[\"print_level\"] = 0\n", 240 | "\n", 241 | " x = MOI.add_variables(solver,prob.n_nlp)\n", 242 | "\n", 243 | " for i = 1:prob.n_nlp\n", 244 | " MOI.add_constraint(solver, x[i], MOI.LessThan(x_u[i]))\n", 245 | " MOI.add_constraint(solver, x[i], MOI.GreaterThan(x_l[i]))\n", 246 | " MOI.set(solver, MOI.VariablePrimalStart(), x[i], x0[i])\n", 247 | " end\n", 248 | "\n", 249 | " # Solve the problem\n", 250 | " MOI.set(solver, MOI.NLPBlock(), block_data)\n", 251 | " MOI.set(solver, MOI.ObjectiveSense(), MOI.MIN_SENSE)\n", 252 | " MOI.optimize!(solver)\n", 253 | "\n", 254 | " # Get the solution\n", 255 | " res = MOI.get(solver, MOI.VariablePrimal(), x)\n", 256 | "\n", 257 | " return res\n", 258 | "end" 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": null, 264 | "id": "7c1e7448", 265 | "metadata": {}, 266 | "outputs": [], 267 | "source": [ 268 | "#Objective and constraint functions for IPOPT\n", 269 | "\n", 270 | "function objective(z)\n", 271 | " qn = z[1:2]\n", 272 | " n = z[3]\n", 273 | " s = z[4]\n", 274 | " \n", 275 | " return s #Minimize slacks associated with relaxed complementarity condition\n", 276 | "end\n", 277 | "\n", 278 | "function constraint!(c,z)\n", 279 | " qn = z[1:2]\n", 280 | " n = z[3]\n", 281 | " s = z[4]\n", 282 | " \n", 283 | " c .= [DEL(qhist[:,k-1],qhist[:,k],qn,n); #DEL\n", 284 | " ϕ(qn); #signed distance\n", 285 | " s-n*ϕ(qn)] #relaxed complementarity\n", 286 | " \n", 287 | " return nothing\n", 288 | "end\n", 289 | "\n", 290 | "#Specify the indecies of c (constraint output) that should be non-negative.\n", 291 | "#The rest will be treated as equality constraints.\n", 292 | "nonnegative_constraint_indices = (3:4)\n", 293 | "\n", 294 | "function primal_bounds(n)\n", 295 | " #Enforce simple bound constraints on the decision variables (e.g. positivity) here\n", 296 | " \n", 297 | " x_l = [-Inf*ones(2); zeros(2)]\n", 298 | " x_u = Inf*ones(4)\n", 299 | " \n", 300 | " return x_l, x_u\n", 301 | "end" 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "execution_count": null, 307 | "id": "15a945b1", 308 | "metadata": {}, 309 | "outputs": [], 310 | "source": [ 311 | "#Solve with IPOPT\n", 312 | "n_nlp = 4\n", 313 | "m_nlp = 4\n", 314 | "nlp_prob = ProblemMOI(n_nlp,m_nlp, idx_ineq=nonnegative_constraint_indices);\n", 315 | "\n", 316 | "#Initial conditions\n", 317 | "qhist = zeros(2,N)\n", 318 | "qhist[:,1] .= q0\n", 319 | "qhist[:,2] .= q1\n", 320 | "\n", 321 | "nhist = zeros(N-1)\n", 322 | "shist = zeros(N-1)\n", 323 | "k = 0\n", 324 | "\n", 325 | "for kk = 2:(N-1)\n", 326 | " k = kk\n", 327 | " z_guess = [qhist[:,k]; 0; 1.0]\n", 328 | " z_sol = ipopt_solve(z_guess,nlp_prob);\n", 329 | " qhist[:,k+1] .= z_sol[1:2]\n", 330 | " nhist[k] = z_sol[3]\n", 331 | " shist[k] = z_sol[4]\n", 332 | "end" 333 | ] 334 | }, 335 | { 336 | "cell_type": "code", 337 | "execution_count": null, 338 | "id": "8ca8d747", 339 | "metadata": {}, 340 | "outputs": [], 341 | "source": [ 342 | "plot(thist,qhist[1,:])" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": null, 348 | "id": "77130676", 349 | "metadata": {}, 350 | "outputs": [], 351 | "source": [ 352 | "plot(thist,qhist[2,:])" 353 | ] 354 | }, 355 | { 356 | "cell_type": "code", 357 | "execution_count": null, 358 | "id": "801801e2", 359 | "metadata": {}, 360 | "outputs": [], 361 | "source": [ 362 | "plot(thist[1:end-1],nhist)" 363 | ] 364 | }, 365 | { 366 | "cell_type": "code", 367 | "execution_count": null, 368 | "id": "524d0eb7", 369 | "metadata": {}, 370 | "outputs": [], 371 | "source": [ 372 | "maximum(shist)" 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": null, 378 | "id": "89c35ed8", 379 | "metadata": {}, 380 | "outputs": [], 381 | "source": [] 382 | } 383 | ], 384 | "metadata": { 385 | "@webio": { 386 | "lastCommId": null, 387 | "lastKernelId": null 388 | }, 389 | "kernelspec": { 390 | "display_name": "Julia 1.6.7", 391 | "language": "julia", 392 | "name": "julia-1.6" 393 | }, 394 | "language_info": { 395 | "file_extension": ".jl", 396 | "mimetype": "application/julia", 397 | "name": "julia", 398 | "version": "1.6.7" 399 | } 400 | }, 401 | "nbformat": 4, 402 | "nbformat_minor": 5 403 | } 404 | -------------------------------------------------------------------------------- /Lecture 19/Lecture 19.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamics-simulation-16-715/lecture-notebooks/a2f7642b3ba31629fc15ca77b738e3f21a5e6c8c/Lecture 19/Lecture 19.pdf -------------------------------------------------------------------------------- /Lecture 19/Manifest.toml: -------------------------------------------------------------------------------- 1 | # This file is machine-generated - editing it directly is not advised 2 | 3 | [[ASL_jll]] 4 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 5 | git-tree-sha1 = "6252039f98492252f9e47c312c8ffda0e3b9e78d" 6 | uuid = "ae81ac8f-d209-56e5-92de-9978fef736f9" 7 | version = "0.1.3+0" 8 | 9 | [[ArgTools]] 10 | uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f" 11 | 12 | [[Artifacts]] 13 | uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33" 14 | 15 | [[Base64]] 16 | uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" 17 | 18 | [[BenchmarkTools]] 19 | deps = ["JSON", "Logging", "Printf", "Profile", "Statistics", "UUIDs"] 20 | git-tree-sha1 = "d9a9701b899b30332bbcb3e1679c41cce81fb0e8" 21 | uuid = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" 22 | version = "1.3.2" 23 | 24 | [[Bzip2_jll]] 25 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 26 | git-tree-sha1 = "19a35467a82e236ff51bc17a3a44b69ef35185a2" 27 | uuid = "6e34b625-4abd-537c-b88f-471c36dfa7a0" 28 | version = "1.0.8+0" 29 | 30 | [[ChainRulesCore]] 31 | deps = ["Compat", "LinearAlgebra", "SparseArrays"] 32 | git-tree-sha1 = "e7ff6cadf743c098e08fca25c91103ee4303c9bb" 33 | uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" 34 | version = "1.15.6" 35 | 36 | [[ChangesOfVariables]] 37 | deps = ["ChainRulesCore", "LinearAlgebra", "Test"] 38 | git-tree-sha1 = "38f7a08f19d8810338d4f5085211c7dfa5d5bdd8" 39 | uuid = "9e997f8a-9a97-42d5-a9f1-ce6bfc15e2c0" 40 | version = "0.1.4" 41 | 42 | [[CodecBzip2]] 43 | deps = ["Bzip2_jll", "Libdl", "TranscodingStreams"] 44 | git-tree-sha1 = "2e62a725210ce3c3c2e1a3080190e7ca491f18d7" 45 | uuid = "523fee87-0ab8-5b00-afb7-3ecf72e48cfd" 46 | version = "0.7.2" 47 | 48 | [[CodecZlib]] 49 | deps = ["TranscodingStreams", "Zlib_jll"] 50 | git-tree-sha1 = "ded953804d019afa9a3f98981d99b33e3db7b6da" 51 | uuid = "944b1d66-785c-5afd-91f1-9de20f533193" 52 | version = "0.7.0" 53 | 54 | [[CommonSubexpressions]] 55 | deps = ["MacroTools", "Test"] 56 | git-tree-sha1 = "7b8a93dba8af7e3b42fecabf646260105ac373f7" 57 | uuid = "bbf7d656-a473-5ed7-a52c-81e309532950" 58 | version = "0.3.0" 59 | 60 | [[Compat]] 61 | deps = ["Dates", "LinearAlgebra", "UUIDs"] 62 | git-tree-sha1 = "3ca828fe1b75fa84b021a7860bd039eaea84d2f2" 63 | uuid = "34da2185-b29b-5c13-b0c7-acf172513d20" 64 | version = "4.3.0" 65 | 66 | [[CompilerSupportLibraries_jll]] 67 | deps = ["Artifacts", "Libdl"] 68 | uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae" 69 | 70 | [[DataStructures]] 71 | deps = ["Compat", "InteractiveUtils", "OrderedCollections"] 72 | git-tree-sha1 = "d1fff3a548102f48987a52a2e0d114fa97d730f0" 73 | uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" 74 | version = "0.18.13" 75 | 76 | [[Dates]] 77 | deps = ["Printf"] 78 | uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" 79 | 80 | [[DiffResults]] 81 | deps = ["StaticArraysCore"] 82 | git-tree-sha1 = "782dd5f4561f5d267313f23853baaaa4c52ea621" 83 | uuid = "163ba53b-c6d8-5494-b064-1a9d43ac40c5" 84 | version = "1.1.0" 85 | 86 | [[DiffRules]] 87 | deps = ["IrrationalConstants", "LogExpFunctions", "NaNMath", "Random", "SpecialFunctions"] 88 | git-tree-sha1 = "8b7a4d23e22f5d44883671da70865ca98f2ebf9d" 89 | uuid = "b552c78f-8df3-52c6-915a-8e097449b14b" 90 | version = "1.12.0" 91 | 92 | [[DocStringExtensions]] 93 | deps = ["LibGit2"] 94 | git-tree-sha1 = "c36550cb29cbe373e95b3f40486b9a4148f89ffd" 95 | uuid = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" 96 | version = "0.9.2" 97 | 98 | [[Downloads]] 99 | deps = ["ArgTools", "LibCURL", "NetworkOptions"] 100 | uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6" 101 | 102 | [[ForwardDiff]] 103 | deps = ["CommonSubexpressions", "DiffResults", "DiffRules", "LinearAlgebra", "LogExpFunctions", "NaNMath", "Preferences", "Printf", "Random", "SpecialFunctions", "StaticArrays"] 104 | git-tree-sha1 = "187198a4ed8ccd7b5d99c41b69c679269ea2b2d4" 105 | uuid = "f6369f11-7733-5829-9624-2563aa707210" 106 | version = "0.10.32" 107 | 108 | [[InteractiveUtils]] 109 | deps = ["Markdown"] 110 | uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" 111 | 112 | [[InverseFunctions]] 113 | deps = ["Test"] 114 | git-tree-sha1 = "49510dfcb407e572524ba94aeae2fced1f3feb0f" 115 | uuid = "3587e190-3f89-42d0-90ee-14403ec27112" 116 | version = "0.1.8" 117 | 118 | [[Ipopt]] 119 | deps = ["Ipopt_jll", "MathOptInterface"] 120 | git-tree-sha1 = "14a305ededd75330246aaa0380130561d8924120" 121 | uuid = "b6b21f68-93f8-5de0-b562-5493be1d77c9" 122 | version = "1.1.0" 123 | 124 | [[Ipopt_jll]] 125 | deps = ["ASL_jll", "Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "MUMPS_seq_jll", "OpenBLAS32_jll", "Pkg"] 126 | git-tree-sha1 = "e3e202237d93f18856b6ff1016166b0f172a49a8" 127 | uuid = "9cc047cb-c261-5740-88fc-0cf96f7bdcc7" 128 | version = "300.1400.400+0" 129 | 130 | [[IrrationalConstants]] 131 | git-tree-sha1 = "7fd44fd4ff43fc60815f8e764c0f352b83c49151" 132 | uuid = "92d709cd-6900-40b7-9082-c6be49f344b6" 133 | version = "0.1.1" 134 | 135 | [[JLLWrappers]] 136 | deps = ["Preferences"] 137 | git-tree-sha1 = "abc9885a7ca2052a736a600f7fa66209f96506e1" 138 | uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210" 139 | version = "1.4.1" 140 | 141 | [[JSON]] 142 | deps = ["Dates", "Mmap", "Parsers", "Unicode"] 143 | git-tree-sha1 = "3c837543ddb02250ef42f4738347454f95079d4e" 144 | uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" 145 | version = "0.21.3" 146 | 147 | [[LibCURL]] 148 | deps = ["LibCURL_jll", "MozillaCACerts_jll"] 149 | uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21" 150 | 151 | [[LibCURL_jll]] 152 | deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"] 153 | uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0" 154 | 155 | [[LibGit2]] 156 | deps = ["Base64", "NetworkOptions", "Printf", "SHA"] 157 | uuid = "76f85450-5226-5b5a-8eaa-529ad045b433" 158 | 159 | [[LibSSH2_jll]] 160 | deps = ["Artifacts", "Libdl", "MbedTLS_jll"] 161 | uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8" 162 | 163 | [[Libdl]] 164 | uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" 165 | 166 | [[LinearAlgebra]] 167 | deps = ["Libdl"] 168 | uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" 169 | 170 | [[LogExpFunctions]] 171 | deps = ["ChainRulesCore", "ChangesOfVariables", "DocStringExtensions", "InverseFunctions", "IrrationalConstants", "LinearAlgebra"] 172 | git-tree-sha1 = "94d9c52ca447e23eac0c0f074effbcd38830deb5" 173 | uuid = "2ab3a3ac-af41-5b50-aa03-7779005ae688" 174 | version = "0.3.18" 175 | 176 | [[Logging]] 177 | uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" 178 | 179 | [[METIS_jll]] 180 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 181 | git-tree-sha1 = "1fd0a97409e418b78c53fac671cf4622efdf0f21" 182 | uuid = "d00139f3-1899-568f-a2f0-47f597d42d70" 183 | version = "5.1.2+0" 184 | 185 | [[MUMPS_seq_jll]] 186 | deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "METIS_jll", "OpenBLAS32_jll", "Pkg"] 187 | git-tree-sha1 = "29de2841fa5aefe615dea179fcde48bb87b58f57" 188 | uuid = "d7ed1dd3-d0ae-5e8e-bfb4-87a502085b8d" 189 | version = "5.4.1+0" 190 | 191 | [[MacroTools]] 192 | deps = ["Markdown", "Random"] 193 | git-tree-sha1 = "42324d08725e200c23d4dfb549e0d5d89dede2d2" 194 | uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" 195 | version = "0.5.10" 196 | 197 | [[Markdown]] 198 | deps = ["Base64"] 199 | uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" 200 | 201 | [[MathOptInterface]] 202 | deps = ["BenchmarkTools", "CodecBzip2", "CodecZlib", "DataStructures", "ForwardDiff", "JSON", "LinearAlgebra", "MutableArithmetics", "NaNMath", "OrderedCollections", "Printf", "SparseArrays", "SpecialFunctions", "Test", "Unicode"] 203 | git-tree-sha1 = "ceed48edffe0325a6e9ea00ecf3607af5089c413" 204 | uuid = "b8f27783-ece8-5eb3-8dc8-9495eed66fee" 205 | version = "1.9.0" 206 | 207 | [[MbedTLS_jll]] 208 | deps = ["Artifacts", "Libdl"] 209 | uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1" 210 | 211 | [[Mmap]] 212 | uuid = "a63ad114-7e13-5084-954f-fe012c677804" 213 | 214 | [[MozillaCACerts_jll]] 215 | uuid = "14a3606d-f60d-562e-9121-12d972cd8159" 216 | 217 | [[MutableArithmetics]] 218 | deps = ["LinearAlgebra", "SparseArrays", "Test"] 219 | git-tree-sha1 = "1d57a7dc42d563ad6b5e95d7a8aebd550e5162c0" 220 | uuid = "d8a4904e-b15c-11e9-3269-09a3773c0cb0" 221 | version = "1.0.5" 222 | 223 | [[NaNMath]] 224 | deps = ["OpenLibm_jll"] 225 | git-tree-sha1 = "a7c3d1da1189a1c2fe843a3bfa04d18d20eb3211" 226 | uuid = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3" 227 | version = "1.0.1" 228 | 229 | [[NetworkOptions]] 230 | uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908" 231 | 232 | [[OpenBLAS32_jll]] 233 | deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "Pkg"] 234 | git-tree-sha1 = "ba4a8f683303c9082e84afba96f25af3c7fb2436" 235 | uuid = "656ef2d0-ae68-5445-9ca0-591084a874a2" 236 | version = "0.3.12+1" 237 | 238 | [[OpenLibm_jll]] 239 | deps = ["Artifacts", "Libdl"] 240 | uuid = "05823500-19ac-5b8b-9628-191a04bc5112" 241 | 242 | [[OpenSpecFun_jll]] 243 | deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "Pkg"] 244 | git-tree-sha1 = "13652491f6856acfd2db29360e1bbcd4565d04f1" 245 | uuid = "efe28fd5-8261-553b-a9e1-b2916fc3738e" 246 | version = "0.5.5+0" 247 | 248 | [[OrderedCollections]] 249 | git-tree-sha1 = "85f8e6578bf1f9ee0d11e7bb1b1456435479d47c" 250 | uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" 251 | version = "1.4.1" 252 | 253 | [[Parsers]] 254 | deps = ["Dates", "SnoopPrecompile"] 255 | git-tree-sha1 = "cceb0257b662528ecdf0b4b4302eb00e767b38e7" 256 | uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0" 257 | version = "2.5.0" 258 | 259 | [[Pkg]] 260 | deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"] 261 | uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" 262 | 263 | [[Preferences]] 264 | deps = ["TOML"] 265 | git-tree-sha1 = "47e5f437cc0e7ef2ce8406ce1e7e24d44915f88d" 266 | uuid = "21216c6a-2e73-6563-6e65-726566657250" 267 | version = "1.3.0" 268 | 269 | [[Printf]] 270 | deps = ["Unicode"] 271 | uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" 272 | 273 | [[Profile]] 274 | deps = ["Printf"] 275 | uuid = "9abbd945-dff8-562f-b5e8-e1ebf5ef1b79" 276 | 277 | [[REPL]] 278 | deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"] 279 | uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" 280 | 281 | [[Random]] 282 | deps = ["Serialization"] 283 | uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" 284 | 285 | [[SHA]] 286 | uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" 287 | 288 | [[Serialization]] 289 | uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" 290 | 291 | [[SnoopPrecompile]] 292 | git-tree-sha1 = "f604441450a3c0569830946e5b33b78c928e1a85" 293 | uuid = "66db9d55-30c0-4569-8b51-7e840670fc0c" 294 | version = "1.0.1" 295 | 296 | [[Sockets]] 297 | uuid = "6462fe0b-24de-5631-8697-dd941f90decc" 298 | 299 | [[SparseArrays]] 300 | deps = ["LinearAlgebra", "Random"] 301 | uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" 302 | 303 | [[SpecialFunctions]] 304 | deps = ["ChainRulesCore", "IrrationalConstants", "LogExpFunctions", "OpenLibm_jll", "OpenSpecFun_jll"] 305 | git-tree-sha1 = "d75bda01f8c31ebb72df80a46c88b25d1c79c56d" 306 | uuid = "276daf66-3868-5448-9aa4-cd146d93841b" 307 | version = "2.1.7" 308 | 309 | [[StaticArrays]] 310 | deps = ["LinearAlgebra", "Random", "StaticArraysCore", "Statistics"] 311 | git-tree-sha1 = "f86b3a049e5d05227b10e15dbb315c5b90f14988" 312 | uuid = "90137ffa-7385-5640-81b9-e52037218182" 313 | version = "1.5.9" 314 | 315 | [[StaticArraysCore]] 316 | git-tree-sha1 = "6b7ba252635a5eff6a0b0664a41ee140a1c9e72a" 317 | uuid = "1e83bf80-4336-4d27-bf5d-d5a4f845583c" 318 | version = "1.4.0" 319 | 320 | [[Statistics]] 321 | deps = ["LinearAlgebra", "SparseArrays"] 322 | uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" 323 | 324 | [[TOML]] 325 | deps = ["Dates"] 326 | uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76" 327 | 328 | [[Tar]] 329 | deps = ["ArgTools", "SHA"] 330 | uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e" 331 | 332 | [[Test]] 333 | deps = ["InteractiveUtils", "Logging", "Random", "Serialization"] 334 | uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" 335 | 336 | [[TranscodingStreams]] 337 | deps = ["Random", "Test"] 338 | git-tree-sha1 = "8a75929dcd3c38611db2f8d08546decb514fcadf" 339 | uuid = "3bb67fe8-82b1-5028-8e26-92a6c54297fa" 340 | version = "0.9.9" 341 | 342 | [[UUIDs]] 343 | deps = ["Random", "SHA"] 344 | uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" 345 | 346 | [[Unicode]] 347 | uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" 348 | 349 | [[Zlib_jll]] 350 | deps = ["Libdl"] 351 | uuid = "83775a58-1f1d-513f-b197-d71354ab007a" 352 | 353 | [[nghttp2_jll]] 354 | deps = ["Artifacts", "Libdl"] 355 | uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d" 356 | 357 | [[p7zip_jll]] 358 | deps = ["Artifacts", "Libdl"] 359 | uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0" 360 | -------------------------------------------------------------------------------- /Lecture 19/Project.toml: -------------------------------------------------------------------------------- 1 | [deps] 2 | Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" 3 | MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee" 4 | -------------------------------------------------------------------------------- /Lecture 19/falling-particle-friction.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "5c41c817", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "import Pkg; Pkg.activate(@__DIR__); Pkg.instantiate()" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": null, 16 | "id": "8ec79d58", 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "using LinearAlgebra\n", 21 | "using ForwardDiff\n", 22 | "using Plots\n", 23 | "using Ipopt\n", 24 | "using MathOptInterface\n", 25 | "const MOI = MathOptInterface" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": null, 31 | "id": "fcc1f3ad", 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "#Parameters\n", 36 | "g = 9.81\n", 37 | "m = 1.0\n", 38 | "h = 0.05 #20 Hz\n", 39 | "Tf = 1.5 #final time (sec)\n", 40 | "thist = Array(0:h:Tf)\n", 41 | "N = length(thist)" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": null, 47 | "id": "fec57ac4", 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "#Initial Conditions\n", 52 | "q0 = [0; 3.0]\n", 53 | "v0 = [1.0; 0.0]\n", 54 | "q1 = q0 + h*v0 + 0.5*h*h*[0; -g]" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": null, 60 | "id": "41873fae", 61 | "metadata": {}, 62 | "outputs": [], 63 | "source": [ 64 | "#Signed-distance function for a particle in 2D\n", 65 | "function ϕ(q)\n", 66 | " return q[2]\n", 67 | "end\n", 68 | "\n", 69 | "Dϕ = [0; 1.0]'" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": null, 75 | "id": "dd118ebc", 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "#Discrete Lagrangian stuff\n", 80 | "function L(q,q̇)\n", 81 | " 0.5*m*q̇'*q̇ - m*g*q[2]\n", 82 | "end\n", 83 | "\n", 84 | "function Ld(q1,q2)\n", 85 | " qm = 0.5*(q1 + q2)\n", 86 | " vm = (q2-q1)/h\n", 87 | " return L(qm,vm)\n", 88 | "end\n", 89 | "\n", 90 | "function D1Ld(q1,q2)\n", 91 | " return ForwardDiff.gradient(dq1->Ld(dq1,q2),q1)\n", 92 | "end\n", 93 | "\n", 94 | "function D2Ld(q1,q2)\n", 95 | " return ForwardDiff.gradient(dq2->Ld(q1,dq2),q2)\n", 96 | "end" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": null, 102 | "id": "80f78ea4", 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "function DEL(q1,q2,q3,n)\n", 107 | " D2Ld(q1,q2) + D1Ld(q2,q3) + h*Dϕ'*n\n", 108 | "end" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": null, 114 | "id": "068cdd22", 115 | "metadata": {}, 116 | "outputs": [], 117 | "source": [ 118 | "#Boilerplate setup code to interface with IPOPT.\n", 119 | "\n", 120 | "struct ProblemMOI <: MOI.AbstractNLPEvaluator\n", 121 | " n_nlp::Int\n", 122 | " m_nlp::Int\n", 123 | " idx_ineq\n", 124 | " obj_grad::Bool\n", 125 | " con_jac::Bool\n", 126 | " sparsity_jac\n", 127 | " sparsity_hess\n", 128 | " primal_bounds\n", 129 | " constraint_bounds\n", 130 | " hessian_lagrangian::Bool\n", 131 | "end\n", 132 | "\n", 133 | "function ProblemMOI(n_nlp,m_nlp;\n", 134 | " idx_ineq=(1:0),\n", 135 | " obj_grad=true,\n", 136 | " con_jac=true,\n", 137 | " sparsity_jac=sparsity_jacobian(n_nlp,m_nlp),\n", 138 | " sparsity_hess=sparsity_hessian(n_nlp,m_nlp),\n", 139 | " primal_bounds=primal_bounds(n_nlp),\n", 140 | " constraint_bounds=constraint_bounds(m_nlp,idx_ineq=idx_ineq),\n", 141 | " hessian_lagrangian=false)\n", 142 | "\n", 143 | " ProblemMOI(n_nlp,m_nlp,\n", 144 | " idx_ineq,\n", 145 | " obj_grad,\n", 146 | " con_jac,\n", 147 | " sparsity_jac,\n", 148 | " sparsity_hess,\n", 149 | " primal_bounds,\n", 150 | " constraint_bounds,\n", 151 | " hessian_lagrangian)\n", 152 | "end\n", 153 | "\n", 154 | "function constraint_bounds(m; idx_ineq=(1:0))\n", 155 | " c_l = zeros(m)\n", 156 | "\n", 157 | " c_u = zeros(m)\n", 158 | " c_u[idx_ineq] .= Inf\n", 159 | " \n", 160 | " return c_l, c_u\n", 161 | "end\n", 162 | "\n", 163 | "function row_col!(row,col,r,c)\n", 164 | " for cc in c\n", 165 | " for rr in r\n", 166 | " push!(row,convert(Int,rr))\n", 167 | " push!(col,convert(Int,cc))\n", 168 | " end\n", 169 | " end\n", 170 | " return row, col\n", 171 | "end\n", 172 | "\n", 173 | "function sparsity_jacobian(n,m)\n", 174 | "\n", 175 | " row = []\n", 176 | " col = []\n", 177 | "\n", 178 | " r = 1:m\n", 179 | " c = 1:n\n", 180 | "\n", 181 | " row_col!(row,col,r,c)\n", 182 | "\n", 183 | " return collect(zip(row,col))\n", 184 | "end\n", 185 | "\n", 186 | "function sparsity_hessian(n,m)\n", 187 | "\n", 188 | " row = []\n", 189 | " col = []\n", 190 | "\n", 191 | " r = 1:m\n", 192 | " c = 1:n\n", 193 | "\n", 194 | " row_col!(row,col,r,c)\n", 195 | "\n", 196 | " return collect(zip(row,col))\n", 197 | "end\n", 198 | "\n", 199 | "function MOI.eval_objective(prob::MOI.AbstractNLPEvaluator, x)\n", 200 | " objective(x)\n", 201 | "end\n", 202 | "\n", 203 | "function MOI.eval_objective_gradient(prob::MOI.AbstractNLPEvaluator, grad_f, x)\n", 204 | " ForwardDiff.gradient!(grad_f,objective,x)\n", 205 | " return nothing\n", 206 | "end\n", 207 | "\n", 208 | "function MOI.eval_constraint(prob::MOI.AbstractNLPEvaluator,g,x)\n", 209 | " constraint!(g,x)\n", 210 | " return nothing\n", 211 | "end\n", 212 | "\n", 213 | "function MOI.eval_constraint_jacobian(prob::MOI.AbstractNLPEvaluator, jac, x)\n", 214 | " ForwardDiff.jacobian!(reshape(jac,prob.m_nlp,prob.n_nlp), constraint!, zeros(prob.m_nlp), x)\n", 215 | " return nothing\n", 216 | "end\n", 217 | "\n", 218 | "function MOI.features_available(prob::MOI.AbstractNLPEvaluator)\n", 219 | " return [:Grad, :Jac]\n", 220 | "end\n", 221 | "\n", 222 | "MOI.initialize(prob::MOI.AbstractNLPEvaluator, features) = nothing\n", 223 | "MOI.jacobian_structure(prob::MOI.AbstractNLPEvaluator) = prob.sparsity_jac\n", 224 | "\n", 225 | "function ipopt_solve(x0,prob::MOI.AbstractNLPEvaluator;\n", 226 | " tol=1.0e-6,c_tol=1.0e-6,max_iter=10000)\n", 227 | " x_l, x_u = prob.primal_bounds\n", 228 | " c_l, c_u = prob.constraint_bounds\n", 229 | "\n", 230 | " nlp_bounds = MOI.NLPBoundsPair.(c_l,c_u)\n", 231 | " block_data = MOI.NLPBlockData(nlp_bounds,prob,true)\n", 232 | "\n", 233 | " solver = Ipopt.Optimizer()\n", 234 | " solver.options[\"max_iter\"] = max_iter\n", 235 | " solver.options[\"tol\"] = tol\n", 236 | " solver.options[\"constr_viol_tol\"] = c_tol\n", 237 | " \n", 238 | " #Uncomment the following line to turn off verbose IPOPT output\n", 239 | " solver.options[\"print_level\"] = 0\n", 240 | "\n", 241 | " x = MOI.add_variables(solver,prob.n_nlp)\n", 242 | "\n", 243 | " for i = 1:prob.n_nlp\n", 244 | " MOI.add_constraint(solver, x[i], MOI.LessThan(x_u[i]))\n", 245 | " MOI.add_constraint(solver, x[i], MOI.GreaterThan(x_l[i]))\n", 246 | " MOI.set(solver, MOI.VariablePrimalStart(), x[i], x0[i])\n", 247 | " end\n", 248 | "\n", 249 | " # Solve the problem\n", 250 | " MOI.set(solver, MOI.NLPBlock(), block_data)\n", 251 | " MOI.set(solver, MOI.ObjectiveSense(), MOI.MIN_SENSE)\n", 252 | " MOI.optimize!(solver)\n", 253 | "\n", 254 | " # Get the solution\n", 255 | " res = MOI.get(solver, MOI.VariablePrimal(), x)\n", 256 | "\n", 257 | " return res\n", 258 | "end" 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": null, 264 | "id": "ab76414b", 265 | "metadata": {}, 266 | "outputs": [], 267 | "source": [ 268 | "#Objective and constraint functions for IPOPT\n", 269 | "\n", 270 | "function objective(z)\n", 271 | " qn = z[1:2]\n", 272 | " n = z[3]\n", 273 | " s = z[4]\n", 274 | " \n", 275 | " return s #Minimize slacks associated with relaxed complementarity condition\n", 276 | "end\n", 277 | "\n", 278 | "function constraint!(c,z)\n", 279 | " qn = z[1:2]\n", 280 | " n = z[3]\n", 281 | " s = z[4]\n", 282 | " \n", 283 | " c .= [DEL(qhist[:,k-1],qhist[:,k],qn,n); #DEL\n", 284 | " ϕ(qn); #signed distance\n", 285 | " s-n*ϕ(qn)] #relaxed complementarity\n", 286 | " \n", 287 | " return nothing\n", 288 | "end\n", 289 | "\n", 290 | "#Specify the indecies of c (constraint output) that should be non-negative.\n", 291 | "#The rest will be treated as equality constraints.\n", 292 | "nonnegative_constraint_indices = (3:4)\n", 293 | "\n", 294 | "function primal_bounds(n)\n", 295 | " #Enforce simple bound constraints on the decision variables (e.g. positivity) here\n", 296 | " \n", 297 | " x_l = [-Inf*ones(2); zeros(2)]\n", 298 | " x_u = Inf*ones(4)\n", 299 | " \n", 300 | " return x_l, x_u\n", 301 | "end" 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "execution_count": null, 307 | "id": "d17b6565", 308 | "metadata": {}, 309 | "outputs": [], 310 | "source": [ 311 | "#Solve with IPOPT\n", 312 | "n_nlp = 4\n", 313 | "m_nlp = 4\n", 314 | "nlp_prob = ProblemMOI(n_nlp,m_nlp, idx_ineq=nonnegative_constraint_indices);\n", 315 | "\n", 316 | "#Initial conditions\n", 317 | "qhist = zeros(2,N)\n", 318 | "qhist[:,1] .= q0\n", 319 | "qhist[:,2] .= q1\n", 320 | "\n", 321 | "nhist = zeros(N-1)\n", 322 | "shist = zeros(N-1)\n", 323 | "k = 0\n", 324 | "\n", 325 | "for kk = 2:(N-1)\n", 326 | " k = kk\n", 327 | " z_guess = [qhist[:,k]; 0; 1.0]\n", 328 | " z_sol = ipopt_solve(z_guess,nlp_prob);\n", 329 | " qhist[:,k+1] .= z_sol[1:2]\n", 330 | " nhist[k] = z_sol[3]\n", 331 | " shist[k] = z_sol[4]\n", 332 | "end" 333 | ] 334 | }, 335 | { 336 | "cell_type": "code", 337 | "execution_count": null, 338 | "id": "ee019863", 339 | "metadata": {}, 340 | "outputs": [], 341 | "source": [ 342 | "plot(thist,qhist[1,:])" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": null, 348 | "id": "e84d4f14", 349 | "metadata": {}, 350 | "outputs": [], 351 | "source": [ 352 | "plot(thist,qhist[2,:])" 353 | ] 354 | }, 355 | { 356 | "cell_type": "code", 357 | "execution_count": null, 358 | "id": "fb550522", 359 | "metadata": {}, 360 | "outputs": [], 361 | "source": [ 362 | "plot(thist[1:end-1],nhist)" 363 | ] 364 | }, 365 | { 366 | "cell_type": "code", 367 | "execution_count": null, 368 | "id": "27aad2a2", 369 | "metadata": {}, 370 | "outputs": [], 371 | "source": [ 372 | "maximum(shist)" 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": null, 378 | "id": "19964753", 379 | "metadata": {}, 380 | "outputs": [], 381 | "source": [ 382 | "#Parameters\n", 383 | "g = 9.81\n", 384 | "m = 1.0\n", 385 | "μ = .05\n", 386 | "h = 0.05 #10 Hz\n", 387 | "Tf = 1.5 #final time (sec)\n", 388 | "thist = Array(0:h:Tf)\n", 389 | "N = length(thist)" 390 | ] 391 | }, 392 | { 393 | "cell_type": "code", 394 | "execution_count": null, 395 | "id": "cfde5c20", 396 | "metadata": {}, 397 | "outputs": [], 398 | "source": [ 399 | "function DEL(q1,q2,q3,n,b)\n", 400 | " D2Ld(q1,q2) + D1Ld(q2,q3) + h*[b;n]\n", 401 | "end" 402 | ] 403 | }, 404 | { 405 | "cell_type": "code", 406 | "execution_count": null, 407 | "id": "57407878", 408 | "metadata": {}, 409 | "outputs": [], 410 | "source": [ 411 | "function smoothsqrt(x)\n", 412 | " ϵ = 1e-6\n", 413 | " return sqrt(x+ϵ*ϵ) - ϵ\n", 414 | "end" 415 | ] 416 | }, 417 | { 418 | "cell_type": "code", 419 | "execution_count": null, 420 | "id": "c04c5f1a", 421 | "metadata": {}, 422 | "outputs": [], 423 | "source": [ 424 | "#Objective and constraint functions for IPOPT with Friction\n", 425 | "\n", 426 | "function objective(z)\n", 427 | " qn = z[1:2]\n", 428 | " n = z[3]\n", 429 | " b = z[4]\n", 430 | " λ = z[5]\n", 431 | " s = z[6:7]\n", 432 | " \n", 433 | " return sum(s) #Minimize slacks associated with relaxed complementarity conditions\n", 434 | "end\n", 435 | "\n", 436 | "function constraint!(c,z)\n", 437 | " qn = z[1:2]\n", 438 | " n = z[3]\n", 439 | " b = z[4]\n", 440 | " λ = z[5]\n", 441 | " s = z[6:7]\n", 442 | " \n", 443 | " vm = (qn-qhist[:,k])/h\n", 444 | " \n", 445 | " c .= [DEL(qhist[:,k-1],qhist[:,k],qn,n,b); #DEL\n", 446 | " [1; 0]'*vm + λ*b/smoothsqrt(b*b); #maximum dissipation\n", 447 | " μ*n - smoothsqrt(b*b); #friction cone\n", 448 | " s[1]-n*ϕ(qn); #relaxed complementarity\n", 449 | " s[2]-λ*(μ*n - smoothsqrt(b*b))] #relaxed complementarity\n", 450 | " \n", 451 | " return nothing\n", 452 | "end\n", 453 | "\n", 454 | "#Specify the indecies of c (constraint output) that should be non-negative.\n", 455 | "#The rest will be treated as equality constraints.\n", 456 | "nonnegative_constraint_indices = (4:6)\n", 457 | "\n", 458 | "function primal_bounds(n)\n", 459 | " #Enforce simple bound constraints on the decision variables (e.g. positivity) here\n", 460 | " \n", 461 | " x_l = [-Inf; 0; 0; -Inf; zeros(3)]\n", 462 | " x_u = Inf*ones(7)\n", 463 | " \n", 464 | " return x_l, x_u\n", 465 | "end" 466 | ] 467 | }, 468 | { 469 | "cell_type": "code", 470 | "execution_count": null, 471 | "id": "f758a27e", 472 | "metadata": {}, 473 | "outputs": [], 474 | "source": [ 475 | "#Solve with IPOPT\n", 476 | "n_nlp = 7\n", 477 | "m_nlp = 6\n", 478 | "nlp_prob = ProblemMOI(n_nlp,m_nlp, idx_ineq=nonnegative_constraint_indices);\n", 479 | "\n", 480 | "#Initial conditions\n", 481 | "qhist = zeros(2,N)\n", 482 | "qhist[:,1] .= q0\n", 483 | "qhist[:,2] .= q1\n", 484 | "\n", 485 | "nhist = zeros(N-1)\n", 486 | "bhist = zeros(N-1)\n", 487 | "λhist = zeros(N-1)\n", 488 | "shist = zeros(2,N-1)\n", 489 | "k = 0\n", 490 | "\n", 491 | "for kk = 2:(N-1)\n", 492 | " k = kk\n", 493 | " z_guess = [qhist[:,k]; ones(5)]\n", 494 | " z_sol = ipopt_solve(z_guess,nlp_prob);\n", 495 | " qhist[:,k+1] .= z_sol[1:2]\n", 496 | " nhist[k] = z_sol[3]\n", 497 | " bhist[k] = z_sol[4]\n", 498 | " λhist[k] = z_sol[5]\n", 499 | " shist[:,k] .= z_sol[6:7]\n", 500 | "end" 501 | ] 502 | }, 503 | { 504 | "cell_type": "code", 505 | "execution_count": null, 506 | "id": "57d88df0", 507 | "metadata": {}, 508 | "outputs": [], 509 | "source": [ 510 | "plot(thist, qhist[1,:])" 511 | ] 512 | }, 513 | { 514 | "cell_type": "code", 515 | "execution_count": null, 516 | "id": "47ddfdf8", 517 | "metadata": {}, 518 | "outputs": [], 519 | "source": [ 520 | "plot(thist, qhist[2,:])" 521 | ] 522 | }, 523 | { 524 | "cell_type": "code", 525 | "execution_count": null, 526 | "id": "6a4ca995", 527 | "metadata": {}, 528 | "outputs": [], 529 | "source": [ 530 | "plot(thist[1:end-1], nhist)" 531 | ] 532 | }, 533 | { 534 | "cell_type": "code", 535 | "execution_count": null, 536 | "id": "05fec2b1", 537 | "metadata": {}, 538 | "outputs": [], 539 | "source": [ 540 | "plot(thist[1:end-1], bhist)" 541 | ] 542 | }, 543 | { 544 | "cell_type": "code", 545 | "execution_count": null, 546 | "id": "355b4bf9", 547 | "metadata": {}, 548 | "outputs": [], 549 | "source": [ 550 | "plot(thist[1:end-1], λhist)" 551 | ] 552 | }, 553 | { 554 | "cell_type": "code", 555 | "execution_count": null, 556 | "id": "221e36fb", 557 | "metadata": {}, 558 | "outputs": [], 559 | "source": [ 560 | "maximum(abs.(shist))" 561 | ] 562 | }, 563 | { 564 | "cell_type": "code", 565 | "execution_count": null, 566 | "id": "b730e642", 567 | "metadata": {}, 568 | "outputs": [], 569 | "source": [] 570 | } 571 | ], 572 | "metadata": { 573 | "@webio": { 574 | "lastCommId": null, 575 | "lastKernelId": null 576 | }, 577 | "kernelspec": { 578 | "display_name": "Julia 1.6.7", 579 | "language": "julia", 580 | "name": "julia-1.6" 581 | }, 582 | "language_info": { 583 | "file_extension": ".jl", 584 | "mimetype": "application/julia", 585 | "name": "julia", 586 | "version": "1.6.7" 587 | } 588 | }, 589 | "nbformat": 4, 590 | "nbformat_minor": 5 591 | } 592 | -------------------------------------------------------------------------------- /Lecture 2/Lecture 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamics-simulation-16-715/lecture-notebooks/a2f7642b3ba31629fc15ca77b738e3f21a5e6c8c/Lecture 2/Lecture 2.pdf -------------------------------------------------------------------------------- /Lecture 2/Project.toml: -------------------------------------------------------------------------------- 1 | [deps] 2 | MeshCat = "283c5d60-a78f-5afe-a0af-af636b173e11" 3 | RobotZoo = "74be38bb-dcc2-4b9e-baf3-d6373cd95f10" 4 | StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" 5 | TrajOptPlots = "7770976a-8dee-4930-bf39-a1782fd21ce6" 6 | -------------------------------------------------------------------------------- /Lecture 2/pendulum.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "7438b978", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "import Pkg; Pkg.activate(@__DIR__); Pkg.instantiate()" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": null, 16 | "id": "eaec848b", 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "using LinearAlgebra\n", 21 | "using PyPlot" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": null, 27 | "id": "99b6635d", 28 | "metadata": {}, 29 | "outputs": [], 30 | "source": [ 31 | "ℓ = 1.0\n", 32 | "m = 1.0\n", 33 | "g = 9.81\n", 34 | "function f(x)\n", 35 | " #pendulum dynamics\n", 36 | " \n", 37 | " θ = x[1]\n", 38 | " θ̇ = x[2]\n", 39 | " \n", 40 | " ẋ = [θ̇; -(g/ℓ)*sin(θ)]\n", 41 | "end" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": null, 47 | "id": "529d295e", 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "function euler_step(xk)\n", 52 | " xn = xk + h*f(xk)\n", 53 | "end" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": null, 59 | "id": "533e2eda", 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "function simulate!(xtraj, N)\n", 64 | " for k = 1:(N-1)\n", 65 | " xtraj[:,k+1] .= euler_step(xtraj[:,k])\n", 66 | " end\n", 67 | "end" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": null, 73 | "id": "78c55ebc", 74 | "metadata": {}, 75 | "outputs": [], 76 | "source": [ 77 | "Tf = 10.0\n", 78 | "h = 0.05 #20 Hz\n", 79 | "N = Int(floor(Tf./h + 1))\n", 80 | "thist = h.*Array(0:(N-1));" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": null, 86 | "id": "7d1f831a", 87 | "metadata": {}, 88 | "outputs": [], 89 | "source": [ 90 | "x0 = [30*(pi/180); 0.0]\n", 91 | "xtraj = zeros(2,N)\n", 92 | "xtraj[:,1] = x0;" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": null, 98 | "id": "9cd13c10", 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "simulate!(xtraj, N)" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": null, 108 | "id": "c5a6be11", 109 | "metadata": {}, 110 | "outputs": [], 111 | "source": [ 112 | "using TrajOptPlots\n", 113 | "using MeshCat\n", 114 | "using StaticArrays\n", 115 | "using RobotZoo\n", 116 | "\n", 117 | "vis = Visualizer()\n", 118 | "TrajOptPlots.set_mesh!(vis, RobotZoo.Pendulum())\n", 119 | "render(vis)" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": null, 125 | "id": "25d7b9b2", 126 | "metadata": {}, 127 | "outputs": [], 128 | "source": [ 129 | "X1 = [SVector{2}(x) for x in eachcol(xtraj)];\n", 130 | "visualize!(vis, RobotZoo.Pendulum(), thist[end], X1)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": null, 136 | "id": "556888d0", 137 | "metadata": {}, 138 | "outputs": [], 139 | "source": [ 140 | "plot(thist,xtraj[1,:])\n", 141 | "xlabel(\"Time (sec)\")\n", 142 | "ylabel(\"θ (rad)\")" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": null, 148 | "id": "883f5f3b", 149 | "metadata": {}, 150 | "outputs": [], 151 | "source": [ 152 | "#Let's plot the total energy of the system\n", 153 | "Ehist = 0.5*m*ℓ^2*(xtraj[2,:].^2) .+ m*g*ℓ*(1.0.-cos.(xtraj[1,:]))\n", 154 | "\n", 155 | "plot(thist,Ehist)\n", 156 | "xlabel(\"Time (sec)\")\n", 157 | "ylabel(\"Total Energy\")" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": null, 163 | "id": "3b633d2a", 164 | "metadata": {}, 165 | "outputs": [], 166 | "source": [] 167 | } 168 | ], 169 | "metadata": { 170 | "kernelspec": { 171 | "display_name": "Julia 1.6.7", 172 | "language": "julia", 173 | "name": "julia-1.6" 174 | }, 175 | "language_info": { 176 | "file_extension": ".jl", 177 | "mimetype": "application/julia", 178 | "name": "julia", 179 | "version": "1.6.7" 180 | } 181 | }, 182 | "nbformat": 4, 183 | "nbformat_minor": 5 184 | } 185 | -------------------------------------------------------------------------------- /Lecture 20/Lecture 20.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamics-simulation-16-715/lecture-notebooks/a2f7642b3ba31629fc15ca77b738e3f21a5e6c8c/Lecture 20/Lecture 20.pdf -------------------------------------------------------------------------------- /Lecture 20/Project.toml: -------------------------------------------------------------------------------- 1 | [deps] 2 | OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" 3 | -------------------------------------------------------------------------------- /Lecture 20/double-pendulum.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "321bbf89", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "import Pkg; Pkg.activate(@__DIR__); Pkg.instantiate()" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": null, 16 | "id": "929635b1", 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "using LinearAlgebra\n", 21 | "using OrdinaryDiffEq\n", 22 | "using ForwardDiff\n", 23 | "using Plots" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": null, 29 | "id": "51e03382", 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "#Parameters\n", 34 | "g = 9.81\n", 35 | "m = 1.0 #link mass\n", 36 | "ℓ = 1.0 #link length\n", 37 | "J = 0.5*m*ℓ*ℓ #link inertia" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": null, 43 | "id": "3c48fba0", 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "function kinematics(q)\n", 48 | " \n", 49 | " r = [0.5*ℓ*sin(q[1]); -0.5*ℓ*cos(q[1]); q[1]; \n", 50 | " ℓ*sin(q[1]) + 0.5*ℓ*sin(q[1]+q[2]);\n", 51 | " -ℓ*cos(q[1]) - 0.5*ℓ*cos(q[1]+q[2]);\n", 52 | " q[1]+q[2]]\n", 53 | "end" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": null, 59 | "id": "f1087e4f", 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "function potential(q)\n", 64 | " \n", 65 | " r = kinematics(q)\n", 66 | " y1 = r[2]\n", 67 | " y2 = r[5]\n", 68 | " \n", 69 | " U = m*g*y1 + m*g*y2\n", 70 | "end" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "id": "21ab5555", 77 | "metadata": {}, 78 | "outputs": [], 79 | "source": [ 80 | "function kinetic(q,q̇)\n", 81 | " \n", 82 | " K = ForwardDiff.jacobian(kinematics,q)\n", 83 | " ṙ = K*q̇\n", 84 | " \n", 85 | " T = 0.5*ṙ'*Diagonal([m; m; J; m; m; J])*ṙ\n", 86 | "end" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "id": "25cab4c6", 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [ 96 | "function Lagrangian(q,q̇)\n", 97 | " return kinetic(q,q̇) - potential(q)\n", 98 | "end" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": null, 104 | "id": "3c21e8d1", 105 | "metadata": {}, 106 | "outputs": [], 107 | "source": [ 108 | "function D1L(q,q̇)\n", 109 | " return ForwardDiff.gradient(dq->Lagrangian(dq,q̇),q)\n", 110 | "end\n", 111 | "\n", 112 | "function D2L(q,q̇)\n", 113 | " return ForwardDiff.gradient(dq̇->Lagrangian(q,dq̇),q̇)\n", 114 | "end\n", 115 | "\n", 116 | "function D2D2L(q,q̇)\n", 117 | " return ForwardDiff.jacobian(dq̇->D2L(q,dq̇),q̇)\n", 118 | "end\n", 119 | "\n", 120 | "function D1D2L(q,q̇)\n", 121 | " return ForwardDiff.jacobian(dq->D2L(dq,q̇),q)\n", 122 | "end" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": null, 128 | "id": "8962529f", 129 | "metadata": {}, 130 | "outputs": [], 131 | "source": [ 132 | "function dynamics(x,τ)\n", 133 | " q = x[1:2]\n", 134 | " q̇ = x[3:4]\n", 135 | " \n", 136 | " #Forced Euler-Lagrange Equation\n", 137 | " q̈ = D2D2L(q,q̇)\\(D1L(q,q̇) - D1D2L(q,q̇)*q̇ + τ)\n", 138 | " \n", 139 | " ẋ = [q̇; q̈]\n", 140 | "end" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": null, 146 | "id": "cb374202", 147 | "metadata": {}, 148 | "outputs": [], 149 | "source": [ 150 | "function controller(x)\n", 151 | " q = x[1:2]\n", 152 | " q̇ = x[3:4]\n", 153 | " \n", 154 | " kp = 100.0\n", 155 | " kd = 10.0\n", 156 | " \n", 157 | " u = -kp*(q-[pi; 0.0]) - kd*q̇\n", 158 | "end" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": null, 164 | "id": "a7054ffa", 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [ 168 | "#Simulate\n", 169 | "x0 = randn(4)\n", 170 | "\n", 171 | "function f(x,p,t)\n", 172 | " #u = zeros(2)\n", 173 | " u = controller(x)\n", 174 | " dynamics(x, u)\n", 175 | "end\n", 176 | "\n", 177 | "tspan = (0.0,10.0)\n", 178 | "prob = ODEProblem(f,x0,tspan)\n", 179 | "sol = solve(prob,Tsit5(),abstol=1e-6,reltol=1e-6);" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": null, 185 | "id": "9f07b6dc", 186 | "metadata": {}, 187 | "outputs": [], 188 | "source": [ 189 | "plot(sol,idxs=(0,1))\n", 190 | "plot!(sol,idxs=(0,2))" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": null, 196 | "id": "5815ea2f", 197 | "metadata": {}, 198 | "outputs": [], 199 | "source": [ 200 | "function y(q)\n", 201 | " #We're going to apply a force in the x-direction at the end of the 2nd link\n", 202 | " return ℓ*sin(q[1]) + ℓ*sin(q[1]+q[2])\n", 203 | "end\n", 204 | "\n", 205 | "function Y(q)\n", 206 | " ForwardDiff.gradient(dq->y(dq),q)'\n", 207 | "end" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": null, 213 | "id": "9d331737", 214 | "metadata": {}, 215 | "outputs": [], 216 | "source": [ 217 | "function dynamics(x,F)\n", 218 | " q = x[1:2]\n", 219 | " q̇ = x[3:4]\n", 220 | " \n", 221 | " #damping constant\n", 222 | " c = 10.0\n", 223 | " \n", 224 | " #Forced Euler-Lagrange Equation\n", 225 | " q̈ = D2D2L(q,q̇)\\(D1L(q,q̇) - D1D2L(q,q̇)*q̇ - c*q̇ + Y(q)'*F)\n", 226 | " \n", 227 | " ẋ = [q̇; q̈]\n", 228 | "end" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": null, 234 | "id": "2f16add4", 235 | "metadata": {}, 236 | "outputs": [], 237 | "source": [ 238 | "#Simulate\n", 239 | "x0 = zeros(4)\n", 240 | "\n", 241 | "function f(x,p,t)\n", 242 | " F = 100.0\n", 243 | " dynamics(x, F)\n", 244 | "end\n", 245 | "\n", 246 | "tspan = (0.0,10.0)\n", 247 | "prob = ODEProblem(f,x0,tspan)\n", 248 | "sol = solve(prob,Tsit5(),abstol=1e-6,reltol=1e-6);" 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": null, 254 | "id": "967b7f60", 255 | "metadata": {}, 256 | "outputs": [], 257 | "source": [ 258 | "plot(sol,idxs=(0,1))\n", 259 | "plot!(sol,idxs=(0,2))" 260 | ] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": null, 265 | "id": "0286ff0a", 266 | "metadata": {}, 267 | "outputs": [], 268 | "source": [] 269 | } 270 | ], 271 | "metadata": { 272 | "kernelspec": { 273 | "display_name": "Julia 1.6.7", 274 | "language": "julia", 275 | "name": "julia-1.6" 276 | }, 277 | "language_info": { 278 | "file_extension": ".jl", 279 | "mimetype": "application/julia", 280 | "name": "julia", 281 | "version": "1.6.7" 282 | } 283 | }, 284 | "nbformat": 4, 285 | "nbformat_minor": 5 286 | } 287 | -------------------------------------------------------------------------------- /Lecture 21/Lecture 21.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamics-simulation-16-715/lecture-notebooks/a2f7642b3ba31629fc15ca77b738e3f21a5e6c8c/Lecture 21/Lecture 21.pdf -------------------------------------------------------------------------------- /Lecture 21/Manifest.toml: -------------------------------------------------------------------------------- 1 | # This file is machine-generated - editing it directly is not advised 2 | 3 | [[Adapt]] 4 | deps = ["LinearAlgebra"] 5 | git-tree-sha1 = "195c5505521008abea5aee4f96930717958eac6f" 6 | uuid = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" 7 | version = "3.4.0" 8 | 9 | [[ArgTools]] 10 | uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f" 11 | 12 | [[Artifacts]] 13 | uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33" 14 | 15 | [[AssetRegistry]] 16 | deps = ["Distributed", "JSON", "Pidfile", "SHA", "Test"] 17 | git-tree-sha1 = "b25e88db7944f98789130d7b503276bc34bc098e" 18 | uuid = "bf4720bc-e11a-5d0c-854e-bdca1663c893" 19 | version = "0.1.0" 20 | 21 | [[Base64]] 22 | uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" 23 | 24 | [[BinDeps]] 25 | deps = ["Libdl", "Pkg", "SHA", "URIParser", "Unicode"] 26 | git-tree-sha1 = "1289b57e8cf019aede076edab0587eb9644175bd" 27 | uuid = "9e28174c-4ba2-5203-b857-d8d62c4213ee" 28 | version = "1.0.2" 29 | 30 | [[Blink]] 31 | deps = ["Base64", "BinDeps", "Distributed", "JSExpr", "JSON", "Lazy", "Logging", "MacroTools", "Mustache", "Mux", "Reexport", "Sockets", "WebIO", "WebSockets"] 32 | git-tree-sha1 = "08d0b679fd7caa49e2bca9214b131289e19808c0" 33 | uuid = "ad839575-38b3-5650-b840-f874b8c74a25" 34 | version = "0.12.5" 35 | 36 | [[Bzip2_jll]] 37 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 38 | git-tree-sha1 = "19a35467a82e236ff51bc17a3a44b69ef35185a2" 39 | uuid = "6e34b625-4abd-537c-b88f-471c36dfa7a0" 40 | version = "1.0.8+0" 41 | 42 | [[Cairo_jll]] 43 | deps = ["Artifacts", "Bzip2_jll", "Fontconfig_jll", "FreeType2_jll", "Glib_jll", "JLLWrappers", "LZO_jll", "Libdl", "Pixman_jll", "Pkg", "Xorg_libXext_jll", "Xorg_libXrender_jll", "Zlib_jll", "libpng_jll"] 44 | git-tree-sha1 = "4b859a208b2397a7a623a03449e4636bdb17bcf2" 45 | uuid = "83423d85-b0ee-5818-9007-b63ccbeb887a" 46 | version = "1.16.1+1" 47 | 48 | [[Cassette]] 49 | git-tree-sha1 = "a70f220ea09ec61401745ff338f8fb340420165c" 50 | uuid = "7057c7e9-c182-5462-911a-8362d720325c" 51 | version = "0.3.11" 52 | 53 | [[ColorTypes]] 54 | deps = ["FixedPointNumbers", "Random"] 55 | git-tree-sha1 = "eb7f0f8307f71fac7c606984ea5fb2817275d6e4" 56 | uuid = "3da002f7-5984-5a60-b8a6-cbb66c0b333f" 57 | version = "0.11.4" 58 | 59 | [[Colors]] 60 | deps = ["ColorTypes", "FixedPointNumbers", "Reexport"] 61 | git-tree-sha1 = "417b0ed7b8b838aa6ca0a87aadf1bb9eb111ce40" 62 | uuid = "5ae59095-9a9b-59fe-a467-6f913c188581" 63 | version = "0.12.8" 64 | 65 | [[CompilerSupportLibraries_jll]] 66 | deps = ["Artifacts", "Libdl"] 67 | uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae" 68 | 69 | [[CoordinateTransformations]] 70 | deps = ["LinearAlgebra", "StaticArrays"] 71 | git-tree-sha1 = "681ea870b918e7cff7111da58791d7f718067a19" 72 | uuid = "150eb455-5306-5404-9cee-2592286d6298" 73 | version = "0.6.2" 74 | 75 | [[DataAPI]] 76 | git-tree-sha1 = "e08915633fcb3ea83bf9d6126292e5bc5c739922" 77 | uuid = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a" 78 | version = "1.13.0" 79 | 80 | [[DataValueInterfaces]] 81 | git-tree-sha1 = "bfc1187b79289637fa0ef6d4436ebdfe6905cbd6" 82 | uuid = "e2d170a0-9d28-54be-80f0-106bbe20a464" 83 | version = "1.0.0" 84 | 85 | [[Dates]] 86 | deps = ["Printf"] 87 | uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" 88 | 89 | [[Distributed]] 90 | deps = ["Random", "Serialization", "Sockets"] 91 | uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b" 92 | 93 | [[DocStringExtensions]] 94 | deps = ["LibGit2"] 95 | git-tree-sha1 = "b19534d1895d702889b219c382a6e18010797f0b" 96 | uuid = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" 97 | version = "0.8.6" 98 | 99 | [[Downloads]] 100 | deps = ["ArgTools", "LibCURL", "NetworkOptions"] 101 | uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6" 102 | 103 | [[EarCut_jll]] 104 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 105 | git-tree-sha1 = "e3290f2d49e661fbd94046d7e3726ffcb2d41053" 106 | uuid = "5ae413db-bbd1-5e63-b57d-d24a61df00f5" 107 | version = "2.2.4+0" 108 | 109 | [[Expat_jll]] 110 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 111 | git-tree-sha1 = "bad72f730e9e91c08d9427d5e8db95478a3c323d" 112 | uuid = "2e619515-83b5-522b-bb60-26c02a35a201" 113 | version = "2.4.8+0" 114 | 115 | [[FFMPEG]] 116 | deps = ["FFMPEG_jll"] 117 | git-tree-sha1 = "b57e3acbe22f8484b4b5ff66a7499717fe1a9cc8" 118 | uuid = "c87230d0-a227-11e9-1b43-d7ebe4e7570a" 119 | version = "0.4.1" 120 | 121 | [[FFMPEG_jll]] 122 | deps = ["Artifacts", "Bzip2_jll", "FreeType2_jll", "FriBidi_jll", "JLLWrappers", "LAME_jll", "Libdl", "Ogg_jll", "OpenSSL_jll", "Opus_jll", "PCRE2_jll", "Pkg", "Zlib_jll", "libaom_jll", "libass_jll", "libfdk_aac_jll", "libvorbis_jll", "x264_jll", "x265_jll"] 123 | git-tree-sha1 = "74faea50c1d007c85837327f6775bea60b5492dd" 124 | uuid = "b22a6f82-2f65-5046-a5b2-351ab43fb4e5" 125 | version = "4.4.2+2" 126 | 127 | [[FileWatching]] 128 | uuid = "7b1f6079-737a-58dc-b8bc-7a2ca5c1b5ee" 129 | 130 | [[FixedPointNumbers]] 131 | deps = ["Statistics"] 132 | git-tree-sha1 = "335bfdceacc84c5cdf16aadc768aa5ddfc5383cc" 133 | uuid = "53c48c17-4a7d-5ca2-90c5-79b7896eea93" 134 | version = "0.8.4" 135 | 136 | [[Fontconfig_jll]] 137 | deps = ["Artifacts", "Bzip2_jll", "Expat_jll", "FreeType2_jll", "JLLWrappers", "Libdl", "Libuuid_jll", "Pkg", "Zlib_jll"] 138 | git-tree-sha1 = "21efd19106a55620a188615da6d3d06cd7f6ee03" 139 | uuid = "a3f928ae-7b40-5064-980b-68af3947d34b" 140 | version = "2.13.93+0" 141 | 142 | [[FreeType2_jll]] 143 | deps = ["Artifacts", "Bzip2_jll", "JLLWrappers", "Libdl", "Pkg", "Zlib_jll"] 144 | git-tree-sha1 = "87eb71354d8ec1a96d4a7636bd57a7347dde3ef9" 145 | uuid = "d7e528f0-a631-5988-bf34-fe36492bcfd7" 146 | version = "2.10.4+0" 147 | 148 | [[FriBidi_jll]] 149 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 150 | git-tree-sha1 = "aa31987c2ba8704e23c6c8ba8a4f769d5d7e4f91" 151 | uuid = "559328eb-81f9-559d-9380-de523a88c83c" 152 | version = "1.0.10+0" 153 | 154 | [[FunctionalCollections]] 155 | deps = ["Test"] 156 | git-tree-sha1 = "04cb9cfaa6ba5311973994fe3496ddec19b6292a" 157 | uuid = "de31a74c-ac4f-5751-b3fd-e18cd04993ca" 158 | version = "0.5.0" 159 | 160 | [[GeometryBasics]] 161 | deps = ["EarCut_jll", "IterTools", "LinearAlgebra", "StaticArrays", "StructArrays", "Tables"] 162 | git-tree-sha1 = "15ff9a14b9e1218958d3530cc288cf31465d9ae2" 163 | uuid = "5c1252a2-5f33-56bf-86c9-59e7332b4326" 164 | version = "0.3.13" 165 | 166 | [[Gettext_jll]] 167 | deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "Libiconv_jll", "Pkg", "XML2_jll"] 168 | git-tree-sha1 = "9b02998aba7bf074d14de89f9d37ca24a1a0b046" 169 | uuid = "78b55507-aeef-58d4-861c-77aaff3498b1" 170 | version = "0.21.0+0" 171 | 172 | [[Glib_jll]] 173 | deps = ["Artifacts", "Gettext_jll", "JLLWrappers", "Libdl", "Libffi_jll", "Libiconv_jll", "Libmount_jll", "PCRE2_jll", "Pkg", "Zlib_jll"] 174 | git-tree-sha1 = "fb83fbe02fe57f2c068013aa94bcdf6760d3a7a7" 175 | uuid = "7746bdde-850d-59dc-9ae8-88ece973131d" 176 | version = "2.74.0+1" 177 | 178 | [[Graphite2_jll]] 179 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 180 | git-tree-sha1 = "344bf40dcab1073aca04aa0df4fb092f920e4011" 181 | uuid = "3b182d85-2403-5c21-9c21-1e1f0cc25472" 182 | version = "1.3.14+0" 183 | 184 | [[HTTP]] 185 | deps = ["Base64", "Dates", "IniFile", "Logging", "MbedTLS", "NetworkOptions", "Sockets", "URIs"] 186 | git-tree-sha1 = "0fa77022fe4b511826b39c894c90daf5fce3334a" 187 | uuid = "cd3eb016-35fb-5094-929b-558a96fad6f3" 188 | version = "0.9.17" 189 | 190 | [[HarfBuzz_jll]] 191 | deps = ["Artifacts", "Cairo_jll", "Fontconfig_jll", "FreeType2_jll", "Glib_jll", "Graphite2_jll", "JLLWrappers", "Libdl", "Libffi_jll", "Pkg"] 192 | git-tree-sha1 = "129acf094d168394e80ee1dc4bc06ec835e510a3" 193 | uuid = "2e76f6c2-a576-52d4-95c1-20adfe4de566" 194 | version = "2.8.1+1" 195 | 196 | [[Hiccup]] 197 | deps = ["MacroTools", "Test"] 198 | git-tree-sha1 = "6187bb2d5fcbb2007c39e7ac53308b0d371124bd" 199 | uuid = "9fb69e20-1954-56bb-a84f-559cc56a8ff7" 200 | version = "0.2.2" 201 | 202 | [[IniFile]] 203 | git-tree-sha1 = "f550e6e32074c939295eb5ea6de31849ac2c9625" 204 | uuid = "83e8ac13-25f8-5344-8a64-a9f2b223428f" 205 | version = "0.5.1" 206 | 207 | [[InteractiveUtils]] 208 | deps = ["Markdown"] 209 | uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" 210 | 211 | [[IterTools]] 212 | git-tree-sha1 = "fa6287a4469f5e048d763df38279ee729fbd44e5" 213 | uuid = "c8e1da08-722c-5040-9ed9-7db0dc04731e" 214 | version = "1.4.0" 215 | 216 | [[IteratorInterfaceExtensions]] 217 | git-tree-sha1 = "a3f24677c21f5bbe9d2a714f95dcd58337fb2856" 218 | uuid = "82899510-4779-5014-852e-03e436cf321d" 219 | version = "1.0.0" 220 | 221 | [[JLLWrappers]] 222 | deps = ["Preferences"] 223 | git-tree-sha1 = "abc9885a7ca2052a736a600f7fa66209f96506e1" 224 | uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210" 225 | version = "1.4.1" 226 | 227 | [[JSExpr]] 228 | deps = ["JSON", "MacroTools", "Observables", "WebIO"] 229 | git-tree-sha1 = "b413a73785b98474d8af24fd4c8a975e31df3658" 230 | uuid = "97c1335a-c9c5-57fe-bc5d-ec35cebe8660" 231 | version = "0.5.4" 232 | 233 | [[JSON]] 234 | deps = ["Dates", "Mmap", "Parsers", "Unicode"] 235 | git-tree-sha1 = "3c837543ddb02250ef42f4738347454f95079d4e" 236 | uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" 237 | version = "0.21.3" 238 | 239 | [[LAME_jll]] 240 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 241 | git-tree-sha1 = "f6250b16881adf048549549fba48b1161acdac8c" 242 | uuid = "c1c5ebd0-6772-5130-a774-d5fcae4a789d" 243 | version = "3.100.1+0" 244 | 245 | [[LZO_jll]] 246 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 247 | git-tree-sha1 = "e5b909bcf985c5e2605737d2ce278ed791b89be6" 248 | uuid = "dd4b983a-f0e5-5f8d-a1b7-129d4a5fb1ac" 249 | version = "2.10.1+0" 250 | 251 | [[Lazy]] 252 | deps = ["MacroTools"] 253 | git-tree-sha1 = "1370f8202dac30758f3c345f9909b97f53d87d3f" 254 | uuid = "50d2b5c4-7a5e-59d5-8109-a42b560f39c0" 255 | version = "0.15.1" 256 | 257 | [[LibCURL]] 258 | deps = ["LibCURL_jll", "MozillaCACerts_jll"] 259 | uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21" 260 | 261 | [[LibCURL_jll]] 262 | deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"] 263 | uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0" 264 | 265 | [[LibGit2]] 266 | deps = ["Base64", "NetworkOptions", "Printf", "SHA"] 267 | uuid = "76f85450-5226-5b5a-8eaa-529ad045b433" 268 | 269 | [[LibSSH2_jll]] 270 | deps = ["Artifacts", "Libdl", "MbedTLS_jll"] 271 | uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8" 272 | 273 | [[Libdl]] 274 | uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" 275 | 276 | [[Libffi_jll]] 277 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 278 | git-tree-sha1 = "0b4a5d71f3e5200a7dff793393e09dfc2d874290" 279 | uuid = "e9f186c6-92d2-5b65-8a66-fee21dc1b490" 280 | version = "3.2.2+1" 281 | 282 | [[Libgcrypt_jll]] 283 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Libgpg_error_jll", "Pkg"] 284 | git-tree-sha1 = "64613c82a59c120435c067c2b809fc61cf5166ae" 285 | uuid = "d4300ac3-e22c-5743-9152-c294e39db1e4" 286 | version = "1.8.7+0" 287 | 288 | [[Libgpg_error_jll]] 289 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 290 | git-tree-sha1 = "c333716e46366857753e273ce6a69ee0945a6db9" 291 | uuid = "7add5ba3-2f88-524e-9cd5-f83b8a55f7b8" 292 | version = "1.42.0+0" 293 | 294 | [[Libiconv_jll]] 295 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 296 | git-tree-sha1 = "42b62845d70a619f063a7da093d995ec8e15e778" 297 | uuid = "94ce4f54-9a6c-5748-9c1c-f9c7231a4531" 298 | version = "1.16.1+1" 299 | 300 | [[Libmount_jll]] 301 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 302 | git-tree-sha1 = "9c30530bf0effd46e15e0fdcf2b8636e78cbbd73" 303 | uuid = "4b2f31a3-9ecc-558c-b454-b3730dcb73e9" 304 | version = "2.35.0+0" 305 | 306 | [[Libuuid_jll]] 307 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 308 | git-tree-sha1 = "7f3efec06033682db852f8b3bc3c1d2b0a0ab066" 309 | uuid = "38a345b3-de98-5d2b-a5d3-14cd9215e700" 310 | version = "2.36.0+0" 311 | 312 | [[LinearAlgebra]] 313 | deps = ["Libdl"] 314 | uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" 315 | 316 | [[Logging]] 317 | uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" 318 | 319 | [[MacroTools]] 320 | deps = ["Markdown", "Random"] 321 | git-tree-sha1 = "42324d08725e200c23d4dfb549e0d5d89dede2d2" 322 | uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" 323 | version = "0.5.10" 324 | 325 | [[Markdown]] 326 | deps = ["Base64"] 327 | uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" 328 | 329 | [[MbedTLS]] 330 | deps = ["Dates", "MbedTLS_jll", "MozillaCACerts_jll", "Random", "Sockets"] 331 | git-tree-sha1 = "03a9b9718f5682ecb107ac9f7308991db4ce395b" 332 | uuid = "739be429-bea8-5141-9913-cc70e7f3736d" 333 | version = "1.1.7" 334 | 335 | [[MbedTLS_jll]] 336 | deps = ["Artifacts", "Libdl"] 337 | uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1" 338 | 339 | [[MeshCat]] 340 | deps = ["Base64", "BinDeps", "Blink", "Cassette", "Colors", "CoordinateTransformations", "DocStringExtensions", "FFMPEG", "GeometryBasics", "LinearAlgebra", "Logging", "MsgPack", "Mux", "Parameters", "Pkg", "Requires", "Rotations", "Sockets", "StaticArrays", "UUIDs", "WebSockets"] 341 | git-tree-sha1 = "a612432c12742141d38c5de5a0ff3c0cbc78130e" 342 | uuid = "283c5d60-a78f-5afe-a0af-af636b173e11" 343 | version = "0.14.1" 344 | 345 | [[Mmap]] 346 | uuid = "a63ad114-7e13-5084-954f-fe012c677804" 347 | 348 | [[MozillaCACerts_jll]] 349 | uuid = "14a3606d-f60d-562e-9121-12d972cd8159" 350 | 351 | [[MsgPack]] 352 | deps = ["Serialization"] 353 | git-tree-sha1 = "a8cbf066b54d793b9a48c5daa5d586cf2b5bd43d" 354 | uuid = "99f44e22-a591-53d1-9472-aa23ef4bd671" 355 | version = "1.1.0" 356 | 357 | [[Mustache]] 358 | deps = ["Printf", "Tables"] 359 | git-tree-sha1 = "1e566ae913a57d0062ff1af54d2697b9344b99cd" 360 | uuid = "ffc61752-8dc7-55ee-8c37-f3e9cdd09e70" 361 | version = "1.0.14" 362 | 363 | [[Mux]] 364 | deps = ["AssetRegistry", "Base64", "HTTP", "Hiccup", "Pkg", "Sockets", "WebSockets"] 365 | git-tree-sha1 = "82dfb2cead9895e10ee1b0ca37a01088456c4364" 366 | uuid = "a975b10e-0019-58db-a62f-e48ff68538c9" 367 | version = "0.7.6" 368 | 369 | [[NetworkOptions]] 370 | uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908" 371 | 372 | [[Observables]] 373 | git-tree-sha1 = "6862738f9796b3edc1c09d0890afce4eca9e7e93" 374 | uuid = "510215fc-4207-5dde-b226-833fc4488ee2" 375 | version = "0.5.4" 376 | 377 | [[Ogg_jll]] 378 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 379 | git-tree-sha1 = "887579a3eb005446d514ab7aeac5d1d027658b8f" 380 | uuid = "e7412a2a-1a6e-54c0-be00-318e2571c051" 381 | version = "1.3.5+1" 382 | 383 | [[OpenSSL_jll]] 384 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 385 | git-tree-sha1 = "f6e9dba33f9f2c44e08a020b0caf6903be540004" 386 | uuid = "458c3c95-2e84-50aa-8efc-19380b2a3a95" 387 | version = "1.1.19+0" 388 | 389 | [[Opus_jll]] 390 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 391 | git-tree-sha1 = "51a08fb14ec28da2ec7a927c4337e4332c2a4720" 392 | uuid = "91d4177d-7536-5919-b921-800302f37372" 393 | version = "1.3.2+0" 394 | 395 | [[OrderedCollections]] 396 | git-tree-sha1 = "85f8e6578bf1f9ee0d11e7bb1b1456435479d47c" 397 | uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" 398 | version = "1.4.1" 399 | 400 | [[PCRE2_jll]] 401 | deps = ["Artifacts", "Libdl"] 402 | uuid = "efcefdf7-47ab-520b-bdef-62a2eaa19f15" 403 | 404 | [[Parameters]] 405 | deps = ["OrderedCollections", "UnPack"] 406 | git-tree-sha1 = "34c0e9ad262e5f7fc75b10a9952ca7692cfc5fbe" 407 | uuid = "d96e819e-fc66-5662-9728-84c9c7592b0a" 408 | version = "0.12.3" 409 | 410 | [[Parsers]] 411 | deps = ["Dates", "SnoopPrecompile"] 412 | git-tree-sha1 = "b64719e8b4504983c7fca6cc9db3ebc8acc2a4d6" 413 | uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0" 414 | version = "2.5.1" 415 | 416 | [[Pidfile]] 417 | deps = ["FileWatching", "Test"] 418 | git-tree-sha1 = "2d8aaf8ee10df53d0dfb9b8ee44ae7c04ced2b03" 419 | uuid = "fa939f87-e72e-5be4-a000-7fc836dbe307" 420 | version = "1.3.0" 421 | 422 | [[Pixman_jll]] 423 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 424 | git-tree-sha1 = "b4f5d02549a10e20780a24fce72bea96b6329e29" 425 | uuid = "30392449-352a-5448-841d-b1acce4e97dc" 426 | version = "0.40.1+0" 427 | 428 | [[Pkg]] 429 | deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"] 430 | uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" 431 | 432 | [[Preferences]] 433 | deps = ["TOML"] 434 | git-tree-sha1 = "47e5f437cc0e7ef2ce8406ce1e7e24d44915f88d" 435 | uuid = "21216c6a-2e73-6563-6e65-726566657250" 436 | version = "1.3.0" 437 | 438 | [[Printf]] 439 | deps = ["Unicode"] 440 | uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" 441 | 442 | [[Quaternions]] 443 | deps = ["LinearAlgebra", "Random"] 444 | git-tree-sha1 = "fcebf40de9a04c58da5073ec09c1c1e95944c79b" 445 | uuid = "94ee1d12-ae83-5a48-8b1c-48b8ff168ae0" 446 | version = "0.6.1" 447 | 448 | [[REPL]] 449 | deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"] 450 | uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" 451 | 452 | [[Random]] 453 | deps = ["Serialization"] 454 | uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" 455 | 456 | [[Reexport]] 457 | git-tree-sha1 = "45e428421666073eab6f2da5c9d310d99bb12f9b" 458 | uuid = "189a3867-3050-52da-a836-e630ba90ab69" 459 | version = "1.2.2" 460 | 461 | [[Requires]] 462 | deps = ["UUIDs"] 463 | git-tree-sha1 = "838a3a4188e2ded87a4f9f184b4b0d78a1e91cb7" 464 | uuid = "ae029012-a4dd-5104-9daa-d747884805df" 465 | version = "1.3.0" 466 | 467 | [[Rotations]] 468 | deps = ["LinearAlgebra", "Quaternions", "Random", "StaticArrays", "Statistics"] 469 | git-tree-sha1 = "793b6ef92f9e96167ddbbd2d9685009e200eb84f" 470 | uuid = "6038ab10-8711-5258-84ad-4b1120ba62dc" 471 | version = "1.3.3" 472 | 473 | [[SHA]] 474 | uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" 475 | 476 | [[Serialization]] 477 | uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" 478 | 479 | [[SnoopPrecompile]] 480 | git-tree-sha1 = "f604441450a3c0569830946e5b33b78c928e1a85" 481 | uuid = "66db9d55-30c0-4569-8b51-7e840670fc0c" 482 | version = "1.0.1" 483 | 484 | [[Sockets]] 485 | uuid = "6462fe0b-24de-5631-8697-dd941f90decc" 486 | 487 | [[SparseArrays]] 488 | deps = ["LinearAlgebra", "Random"] 489 | uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" 490 | 491 | [[StaticArrays]] 492 | deps = ["LinearAlgebra", "Random", "StaticArraysCore", "Statistics"] 493 | git-tree-sha1 = "4e051b85454b4e4f66e6a6b7bdc452ad9da3dcf6" 494 | uuid = "90137ffa-7385-5640-81b9-e52037218182" 495 | version = "1.5.10" 496 | 497 | [[StaticArraysCore]] 498 | git-tree-sha1 = "6b7ba252635a5eff6a0b0664a41ee140a1c9e72a" 499 | uuid = "1e83bf80-4336-4d27-bf5d-d5a4f845583c" 500 | version = "1.4.0" 501 | 502 | [[Statistics]] 503 | deps = ["LinearAlgebra", "SparseArrays"] 504 | uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" 505 | 506 | [[StructArrays]] 507 | deps = ["Adapt", "DataAPI", "StaticArraysCore", "Tables"] 508 | git-tree-sha1 = "13237798b407150a6d2e2bce5d793d7d9576e99e" 509 | uuid = "09ab397b-f2b6-538f-b94a-2f83cf4a842a" 510 | version = "0.6.13" 511 | 512 | [[TOML]] 513 | deps = ["Dates"] 514 | uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76" 515 | 516 | [[TableTraits]] 517 | deps = ["IteratorInterfaceExtensions"] 518 | git-tree-sha1 = "c06b2f539df1c6efa794486abfb6ed2022561a39" 519 | uuid = "3783bdb8-4a98-5b6b-af9a-565f29a5fe9c" 520 | version = "1.0.1" 521 | 522 | [[Tables]] 523 | deps = ["DataAPI", "DataValueInterfaces", "IteratorInterfaceExtensions", "LinearAlgebra", "OrderedCollections", "TableTraits", "Test"] 524 | git-tree-sha1 = "c79322d36826aa2f4fd8ecfa96ddb47b174ac78d" 525 | uuid = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" 526 | version = "1.10.0" 527 | 528 | [[Tar]] 529 | deps = ["ArgTools", "SHA"] 530 | uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e" 531 | 532 | [[Test]] 533 | deps = ["InteractiveUtils", "Logging", "Random", "Serialization"] 534 | uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" 535 | 536 | [[URIParser]] 537 | deps = ["Unicode"] 538 | git-tree-sha1 = "53a9f49546b8d2dd2e688d216421d050c9a31d0d" 539 | uuid = "30578b45-9adc-5946-b283-645ec420af67" 540 | version = "0.4.1" 541 | 542 | [[URIs]] 543 | git-tree-sha1 = "e59ecc5a41b000fa94423a578d29290c7266fc10" 544 | uuid = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4" 545 | version = "1.4.0" 546 | 547 | [[UUIDs]] 548 | deps = ["Random", "SHA"] 549 | uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" 550 | 551 | [[UnPack]] 552 | git-tree-sha1 = "387c1f73762231e86e0c9c5443ce3b4a0a9a0c2b" 553 | uuid = "3a884ed6-31ef-47d7-9d2a-63182c4928ed" 554 | version = "1.0.2" 555 | 556 | [[Unicode]] 557 | uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" 558 | 559 | [[WebIO]] 560 | deps = ["AssetRegistry", "Base64", "Distributed", "FunctionalCollections", "JSON", "Logging", "Observables", "Pkg", "Random", "Requires", "Sockets", "UUIDs", "WebSockets", "Widgets"] 561 | git-tree-sha1 = "55ea1b43214edb1f6a228105a219c6e84f1f5533" 562 | uuid = "0f1e0344-ec1d-5b48-a673-e5cf874b6c29" 563 | version = "0.8.19" 564 | 565 | [[WebSockets]] 566 | deps = ["Base64", "Dates", "HTTP", "Logging", "Sockets"] 567 | git-tree-sha1 = "f91a602e25fe6b89afc93cf02a4ae18ee9384ce3" 568 | uuid = "104b5d7c-a370-577a-8038-80a2059c5097" 569 | version = "1.5.9" 570 | 571 | [[Widgets]] 572 | deps = ["Colors", "Dates", "Observables", "OrderedCollections"] 573 | git-tree-sha1 = "fcdae142c1cfc7d89de2d11e08721d0f2f86c98a" 574 | uuid = "cc8bc4a8-27d6-5769-a93b-9d913e69aa62" 575 | version = "0.6.6" 576 | 577 | [[XML2_jll]] 578 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Libiconv_jll", "Pkg", "Zlib_jll"] 579 | git-tree-sha1 = "58443b63fb7e465a8a7210828c91c08b92132dff" 580 | uuid = "02c8fc9c-b97f-50b9-bbe4-9be30ff0a78a" 581 | version = "2.9.14+0" 582 | 583 | [[XSLT_jll]] 584 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Libgcrypt_jll", "Libgpg_error_jll", "Libiconv_jll", "Pkg", "XML2_jll", "Zlib_jll"] 585 | git-tree-sha1 = "91844873c4085240b95e795f692c4cec4d805f8a" 586 | uuid = "aed1982a-8fda-507f-9586-7b0439959a61" 587 | version = "1.1.34+0" 588 | 589 | [[Xorg_libX11_jll]] 590 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libxcb_jll", "Xorg_xtrans_jll"] 591 | git-tree-sha1 = "5be649d550f3f4b95308bf0183b82e2582876527" 592 | uuid = "4f6342f7-b3d2-589e-9d20-edeb45f2b2bc" 593 | version = "1.6.9+4" 594 | 595 | [[Xorg_libXau_jll]] 596 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 597 | git-tree-sha1 = "4e490d5c960c314f33885790ed410ff3a94ce67e" 598 | uuid = "0c0b7dd1-d40b-584c-a123-a41640f87eec" 599 | version = "1.0.9+4" 600 | 601 | [[Xorg_libXdmcp_jll]] 602 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 603 | git-tree-sha1 = "4fe47bd2247248125c428978740e18a681372dd4" 604 | uuid = "a3789734-cfe1-5b06-b2d0-1dd0d9d62d05" 605 | version = "1.1.3+4" 606 | 607 | [[Xorg_libXext_jll]] 608 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libX11_jll"] 609 | git-tree-sha1 = "b7c0aa8c376b31e4852b360222848637f481f8c3" 610 | uuid = "1082639a-0dae-5f34-9b06-72781eeb8cb3" 611 | version = "1.3.4+4" 612 | 613 | [[Xorg_libXrender_jll]] 614 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libX11_jll"] 615 | git-tree-sha1 = "19560f30fd49f4d4efbe7002a1037f8c43d43b96" 616 | uuid = "ea2f1a96-1ddc-540d-b46f-429655e07cfa" 617 | version = "0.9.10+4" 618 | 619 | [[Xorg_libpthread_stubs_jll]] 620 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 621 | git-tree-sha1 = "6783737e45d3c59a4a4c4091f5f88cdcf0908cbb" 622 | uuid = "14d82f49-176c-5ed1-bb49-ad3f5cbd8c74" 623 | version = "0.1.0+3" 624 | 625 | [[Xorg_libxcb_jll]] 626 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "XSLT_jll", "Xorg_libXau_jll", "Xorg_libXdmcp_jll", "Xorg_libpthread_stubs_jll"] 627 | git-tree-sha1 = "daf17f441228e7a3833846cd048892861cff16d6" 628 | uuid = "c7cfdc94-dc32-55de-ac96-5a1b8d977c5b" 629 | version = "1.13.0+3" 630 | 631 | [[Xorg_xtrans_jll]] 632 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 633 | git-tree-sha1 = "79c31e7844f6ecf779705fbc12146eb190b7d845" 634 | uuid = "c5fb5394-a638-5e4d-96e5-b29de1b5cf10" 635 | version = "1.4.0+3" 636 | 637 | [[Zlib_jll]] 638 | deps = ["Libdl"] 639 | uuid = "83775a58-1f1d-513f-b197-d71354ab007a" 640 | 641 | [[libaom_jll]] 642 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 643 | git-tree-sha1 = "3a2ea60308f0996d26f1e5354e10c24e9ef905d4" 644 | uuid = "a4ae2306-e953-59d6-aa16-d00cac43593b" 645 | version = "3.4.0+0" 646 | 647 | [[libass_jll]] 648 | deps = ["Artifacts", "Bzip2_jll", "FreeType2_jll", "FriBidi_jll", "HarfBuzz_jll", "JLLWrappers", "Libdl", "Pkg", "Zlib_jll"] 649 | git-tree-sha1 = "5982a94fcba20f02f42ace44b9894ee2b140fe47" 650 | uuid = "0ac62f75-1d6f-5e53-bd7c-93b484bb37c0" 651 | version = "0.15.1+0" 652 | 653 | [[libfdk_aac_jll]] 654 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 655 | git-tree-sha1 = "daacc84a041563f965be61859a36e17c4e4fcd55" 656 | uuid = "f638f0a6-7fb0-5443-88ba-1cc74229b280" 657 | version = "2.0.2+0" 658 | 659 | [[libpng_jll]] 660 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Zlib_jll"] 661 | git-tree-sha1 = "94d180a6d2b5e55e447e2d27a29ed04fe79eb30c" 662 | uuid = "b53b4c65-9356-5827-b1ea-8c7a1a84506f" 663 | version = "1.6.38+0" 664 | 665 | [[libvorbis_jll]] 666 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Ogg_jll", "Pkg"] 667 | git-tree-sha1 = "b910cb81ef3fe6e78bf6acee440bda86fd6ae00c" 668 | uuid = "f27f6e37-5d2b-51aa-960f-b287f2bc3b7a" 669 | version = "1.3.7+1" 670 | 671 | [[nghttp2_jll]] 672 | deps = ["Artifacts", "Libdl"] 673 | uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d" 674 | 675 | [[p7zip_jll]] 676 | deps = ["Artifacts", "Libdl"] 677 | uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0" 678 | 679 | [[x264_jll]] 680 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 681 | git-tree-sha1 = "4fea590b89e6ec504593146bf8b988b2c00922b2" 682 | uuid = "1270edf5-f2f9-52d2-97e9-ab00b5d0237a" 683 | version = "2021.5.5+0" 684 | 685 | [[x265_jll]] 686 | deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] 687 | git-tree-sha1 = "ee567a171cce03570d77ad3a43e90218e38937a9" 688 | uuid = "dfaa095f-4041-5dcd-9319-2fabd8486b76" 689 | version = "3.5.0+0" 690 | -------------------------------------------------------------------------------- /Lecture 21/Project.toml: -------------------------------------------------------------------------------- 1 | [deps] 2 | CoordinateTransformations = "150eb455-5306-5404-9cee-2592286d6298" 3 | GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326" 4 | MeshCat = "283c5d60-a78f-5afe-a0af-af636b173e11" 5 | Rotations = "6038ab10-8711-5258-84ad-4b1120ba62dc" 6 | -------------------------------------------------------------------------------- /Lecture 21/floating-2bar.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "6ea74dda", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "using Pkg; Pkg.activate(@__DIR__); Pkg.instantiate()" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": null, 16 | "id": "45b4e494", 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "using LinearAlgebra\n", 21 | "using ForwardDiff\n", 22 | "using Plots" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": null, 28 | "id": "21d67ce4", 29 | "metadata": {}, 30 | "outputs": [], 31 | "source": [ 32 | "#Parameters\n", 33 | "m1 = 1.0\n", 34 | "m2 = 1.0\n", 35 | "J1 = Diagonal([0.1; 1.0; 1.0])\n", 36 | "J2 = Diagonal([0.1; 1.0; 1.0])\n", 37 | "ℓ1 = 1.0\n", 38 | "ℓ2 = 1.0" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": null, 44 | "id": "07a5938f", 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "M̄ = [m1*I(3) zeros(3,9);\n", 49 | " zeros(3,3) J1 zeros(3,6);\n", 50 | " zeros(3,6) m2*I(3) zeros(3,3);\n", 51 | " zeros(3,9) J2]" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": null, 57 | "id": "05035d79", 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [ 61 | "function hat(ω)\n", 62 | " return [0 -ω[3] ω[2];\n", 63 | " ω[3] 0 -ω[1];\n", 64 | " -ω[2] ω[1] 0]\n", 65 | "end\n", 66 | "\n", 67 | "function L(Q)\n", 68 | " [Q[1] -Q[2:4]'; Q[2:4] Q[1]*I + hat(Q[2:4])]\n", 69 | "end\n", 70 | "\n", 71 | "function R(Q)\n", 72 | " [Q[1] -Q[2:4]'; Q[2:4] Q[1]*I - hat(Q[2:4])]\n", 73 | "end\n", 74 | "\n", 75 | "H = [zeros(1,3); I];\n", 76 | "\n", 77 | "T = Diagonal([1.0; -1; -1; -1])\n", 78 | "\n", 79 | "function G(Q)\n", 80 | " return L(Q)*H\n", 81 | "end\n", 82 | "\n", 83 | "function Ḡ(q)\n", 84 | " Q = q[4:7]\n", 85 | " return [I(3) zeros(3,4); zeros(4,3) G(Q) zeros(4,1); zeros(1,6) 1]\n", 86 | "end\n", 87 | "\n", 88 | "function G̃(q)\n", 89 | " Q = q[4:7]\n", 90 | " #return [I(3) zeros(3,4); zeros(4,3) G(Q) zeros(4,1); zeros(1,6) 1]\n", 91 | " return [I(3) zeros(3,4); zeros(4,3) 0.5*G(Q) zeros(4,1); zeros(1,6) 1]\n", 92 | "end" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": null, 98 | "id": "8087fc23", 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "#Kinematics\n", 103 | "function kinematics(q)\n", 104 | " r0 = q[1:3]\n", 105 | " Q0 = q[4:7]\n", 106 | " q1 = q[8]\n", 107 | " \n", 108 | " #H'*L(q)*R(q)'*H\n", 109 | " \n", 110 | " Q1 = L(Q0)*[cos(q1/2); 0; 0; sin(q1/2)]\n", 111 | " r1 = r0 + H'L(Q0)*R(Q0)'*H*[0.5*ℓ1; 0; 0] + H'L(Q1)*R(Q1)'*H*[0.5*ℓ2; 0; 0]\n", 112 | " \n", 113 | " return [r0; Q0; r1; Q1]\n", 114 | "end" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": null, 120 | "id": "01f989d3", 121 | "metadata": {}, 122 | "outputs": [], 123 | "source": [ 124 | "#Kinematic Jacobian\n", 125 | "function K(q)\n", 126 | " k = kinematics(q);\n", 127 | " r0 = k[1:3]\n", 128 | " Q0 = k[4:7]\n", 129 | " r1 = k[8:10]\n", 130 | " Q1 = k[11:14]\n", 131 | " \n", 132 | " dk = ForwardDiff.jacobian(dq->kinematics(dq),q)\n", 133 | " K = [I(3) zeros(3,11); zeros(3,3) 2*G(Q0)' zeros(3,7); zeros(3,7) I(3) zeros(3,4); zeros(3,10) 2*G(Q1)']*dk*[I(3) zeros(3,4); zeros(4,3) 0.5*G(Q0) zeros(4,1); zeros(1,6) 1.0]\n", 134 | "end" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": null, 140 | "id": "7a1b796b", 141 | "metadata": {}, 142 | "outputs": [], 143 | "source": [ 144 | "#Mass Matrix\n", 145 | "function M(q) \n", 146 | " return K(q)'*M̄*K(q)\n", 147 | "end" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": null, 153 | "id": "548196ac", 154 | "metadata": {}, 155 | "outputs": [], 156 | "source": [ 157 | "M(q0)" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": null, 163 | "id": "a7c74713", 164 | "metadata": {}, 165 | "outputs": [], 166 | "source": [ 167 | "eigvals(M(q0))" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": null, 173 | "id": "39b37f39", 174 | "metadata": {}, 175 | "outputs": [], 176 | "source": [ 177 | "#Potential\n", 178 | "function U(q)\n", 179 | " return 0.0\n", 180 | "end\n", 181 | "\n", 182 | "function DQ0U(q)\n", 183 | " r0 = q[1:3]\n", 184 | " Q0 = q[4:7]\n", 185 | " q1 = q[8]\n", 186 | " \n", 187 | " ForwardDiff.gradient(dQ0->U([r0; dQ0; q1]), Q0) \n", 188 | "end" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": null, 194 | "id": "6854c510", 195 | "metadata": {}, 196 | "outputs": [], 197 | "source": [ 198 | "#Lagrangian\n", 199 | "function Lagrangian(q,v)\n", 200 | " 0.5*v'*M(q)*v - U(q)\n", 201 | "end" 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": null, 207 | "id": "c03cbbf0", 208 | "metadata": {}, 209 | "outputs": [], 210 | "source": [ 211 | "#Lagrangian Derivatives\n", 212 | "\n", 213 | "function Dr0L(q,v)\n", 214 | " r0 = q[1:3]\n", 215 | " Q0 = q[4:7]\n", 216 | " q1 = q[8]\n", 217 | " \n", 218 | " ForwardDiff.gradient(dr0->Lagrangian([dr0; Q0; q1],v), r0)\n", 219 | "end\n", 220 | "\n", 221 | "function DQ0L(q,v)\n", 222 | " r0 = q[1:3]\n", 223 | " Q0 = q[4:7]\n", 224 | " q1 = q[8]\n", 225 | " \n", 226 | " ForwardDiff.gradient(dQ0->Lagrangian([r0; dQ0; q1],v), Q0)\n", 227 | "end\n", 228 | "\n", 229 | "function Dq1L(q,v)\n", 230 | " r0 = q[1:3]\n", 231 | " Q0 = q[4:7]\n", 232 | " q1 = q[8]\n", 233 | " \n", 234 | " ForwardDiff.derivative(dq1->Lagrangian([r0; Q0; dq1],v), q1)\n", 235 | "end\n", 236 | "\n", 237 | "function Dṙ0L(q,v)\n", 238 | " ṙ0 = v[1:3]\n", 239 | " ω0 = v[4:6]\n", 240 | " q̇1 = v[7]\n", 241 | " \n", 242 | " ForwardDiff.gradient(dṙ0->Lagrangian(q,[dṙ0;ω0;q̇1]), ṙ0)\n", 243 | "end\n", 244 | " \n", 245 | "function Dω0L(q,v)\n", 246 | " ṙ0 = v[1:3]\n", 247 | " ω0 = v[4:6]\n", 248 | " q̇1 = v[7]\n", 249 | " \n", 250 | " ForwardDiff.gradient(dω0->Lagrangian(q,[ṙ0;dω0;q̇1]), ω0)\n", 251 | "end\n", 252 | "\n", 253 | "function Dq̇1L(q,v)\n", 254 | " ṙ0 = v[1:3]\n", 255 | " ω0 = v[4:6]\n", 256 | " q̇1 = v[7]\n", 257 | " \n", 258 | " ForwardDiff.derivative(dq̇1->Lagrangian(q,[ṙ0;ω0;dq̇1]), q̇1)\n", 259 | "end\n", 260 | " " 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "execution_count": null, 266 | "id": "352bb979", 267 | "metadata": {}, 268 | "outputs": [], 269 | "source": [ 270 | "#Input Jacobian\n", 271 | "function B(q)\n", 272 | " #[I(3) zeros(3); zeros(3,4); zeros(1,3) 1.0]\n", 273 | " [zeros(6); 1.0]\n", 274 | "end" 275 | ] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "execution_count": null, 280 | "id": "4cb9fda9", 281 | "metadata": {}, 282 | "outputs": [], 283 | "source": [ 284 | "#Initial Conditions\n", 285 | "r0 = zeros(3)\n", 286 | "Q0 = [1.0; 0; 0; 0]\n", 287 | "q1 = 0.0\n", 288 | "q0 = [r0; Q0; q1]\n", 289 | "\n", 290 | "q̇1 = 0.0\n", 291 | "ω0 = zeros(3)\n", 292 | "#ω0[3] = -q̇1\n", 293 | "ṙ0 = zeros(3) #cross(ω0,[-0.5*ℓ1; 0; 0])\n", 294 | "\n", 295 | "v0 = [ṙ0; ω0; q̇1]\n", 296 | "x0 = [q0; v0]" 297 | ] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": null, 302 | "id": "1736da95", 303 | "metadata": {}, 304 | "outputs": [], 305 | "source": [ 306 | "#EL Dynamics\n", 307 | "function f(x,u)\n", 308 | " q = x[1:8]\n", 309 | " v = x[9:15]\n", 310 | " ω = v[4:6]\n", 311 | " \n", 312 | " q̇ = G̃(q)*v\n", 313 | " \n", 314 | " v̇ = [ForwardDiff.jacobian(dv->Dṙ0L(q,dv),v);\n", 315 | " ForwardDiff.jacobian(dv->Dω0L(q,dv),v);\n", 316 | " ForwardDiff.gradient(dv->Dq̇1L(q,dv),v)']\\([-ForwardDiff.jacobian(dq->Dṙ0L(dq,v),q)*G̃(q)*v + Dr0L(q,v);\n", 317 | " -ForwardDiff.jacobian(dq->Dω0L(dq,v),q)*G̃(q)*v - hat(ω)*Dω0L(q,v) + 0.5*G(Q0)'*DQ0U(q);\n", 318 | " -ForwardDiff.gradient(dq->Dq̇1L(dq,v),q)'*G̃(q)*v + Dq1L(q,v)] + B(q)*u)\n", 319 | " \n", 320 | " #ForwardDiff.jacobian(dv->Dṙ0L(q,dv),v)*v̇ + ForwardDiff.jacobian(dq->Dṙ0L(dq,v),q)*G̃(q)*v - Dr0L(q,v)\n", 321 | " #ForwardDiff.jacobian(dv->Dω0L(q,dv),v)*v̇ + ForwardDiff.jacobian(dq->Dω0L(dq,v),q)*G̃(q)*v + hat(ω)*Dω0L(q,v) - 0.5*H'*L(Q0)'*DQ0L(q,v)\n", 322 | " #ForwardDiff.jacobian(dv->Dq̇1L(q,dv),v)*v̇ + ForwardDiff.jacobian(dq->Dq̇1L(dq,v),q)*G̃(q)*v - Dq1L(q,v)\n", 323 | " \n", 324 | " \n", 325 | " return [q̇; v̇]\n", 326 | "end" 327 | ] 328 | }, 329 | { 330 | "cell_type": "code", 331 | "execution_count": null, 332 | "id": "52d4be43", 333 | "metadata": {}, 334 | "outputs": [], 335 | "source": [ 336 | "#Simulate with RK4\n", 337 | "Tf = 6.0\n", 338 | "h = 0.05 #20 Hz\n", 339 | "thist = 0:h:Tf\n", 340 | "N = length(thist)\n", 341 | "\n", 342 | "xhist = zeros(15,N)\n", 343 | "xhist[:,1] .= [q0; v0]\n", 344 | "\n", 345 | "#Torque input at joint\n", 346 | "uhist = .5*[ones(20); -ones(40); zeros(10); ones(40); -ones(10); 0]; #-ones(20); ones(40); 0] #-0.1*sin.(pi*thist)\n", 347 | "\n", 348 | "for k = 1:(N-1)\n", 349 | " ẋ1 = f(xhist[:,k], uhist[k])\n", 350 | " ẋ2 = f(xhist[:,k]+0.5*h*ẋ1, 0.5*(uhist[k]+uhist[k+1]))\n", 351 | " ẋ3 = f(xhist[:,k]+0.5*h*ẋ2, 0.5*(uhist[k]+uhist[k+1]))\n", 352 | " ẋ4 = f(xhist[:,k]+h*ẋ3, uhist[k+1])\n", 353 | " \n", 354 | " xhist[:,k+1] = xhist[:,k] + (h/6.0)*(ẋ1 + 2*ẋ2 + 2*ẋ3 + ẋ4)\n", 355 | " xhist[4:7,k+1] .= xhist[4:7,k+1]/norm(xhist[4:7,k+1]) #re-normalize quaternion\n", 356 | "end" 357 | ] 358 | }, 359 | { 360 | "cell_type": "code", 361 | "execution_count": null, 362 | "id": "28c2865e", 363 | "metadata": {}, 364 | "outputs": [], 365 | "source": [ 366 | "plot(thist, xhist[1,:])\n", 367 | "plot!(thist, xhist[2,:])\n", 368 | "plot!(thist, xhist[3,:])" 369 | ] 370 | }, 371 | { 372 | "cell_type": "code", 373 | "execution_count": null, 374 | "id": "58e56a60", 375 | "metadata": {}, 376 | "outputs": [], 377 | "source": [ 378 | "plot(thist, xhist[4,:])\n", 379 | "plot!(thist, xhist[5,:])\n", 380 | "plot!(thist, xhist[6,:])\n", 381 | "plot!(thist, xhist[7,:])" 382 | ] 383 | }, 384 | { 385 | "cell_type": "code", 386 | "execution_count": null, 387 | "id": "d3ea845a", 388 | "metadata": {}, 389 | "outputs": [], 390 | "source": [ 391 | "plot(thist, xhist[8,:])" 392 | ] 393 | }, 394 | { 395 | "cell_type": "code", 396 | "execution_count": null, 397 | "id": "53fd5a41", 398 | "metadata": {}, 399 | "outputs": [], 400 | "source": [ 401 | "plot(thist, xhist[9,:])\n", 402 | "plot!(thist, xhist[10,:])\n", 403 | "plot!(thist, xhist[11,:])" 404 | ] 405 | }, 406 | { 407 | "cell_type": "code", 408 | "execution_count": null, 409 | "id": "17ad7bd7", 410 | "metadata": {}, 411 | "outputs": [], 412 | "source": [ 413 | "plot(thist, xhist[12,:])\n", 414 | "plot!(thist, xhist[13,:])\n", 415 | "plot!(thist, xhist[14,:])" 416 | ] 417 | }, 418 | { 419 | "cell_type": "code", 420 | "execution_count": null, 421 | "id": "c5a13c52", 422 | "metadata": {}, 423 | "outputs": [], 424 | "source": [ 425 | "plot(thist, xhist[15,:])" 426 | ] 427 | }, 428 | { 429 | "cell_type": "code", 430 | "execution_count": null, 431 | "id": "9d491787", 432 | "metadata": {}, 433 | "outputs": [], 434 | "source": [ 435 | "xkn = kinematics(xhist[:,end])" 436 | ] 437 | }, 438 | { 439 | "cell_type": "code", 440 | "execution_count": null, 441 | "id": "81a1e279", 442 | "metadata": {}, 443 | "outputs": [], 444 | "source": [ 445 | "xk0 = kinematics(xhist[:,1])" 446 | ] 447 | }, 448 | { 449 | "cell_type": "code", 450 | "execution_count": null, 451 | "id": "53de6b61", 452 | "metadata": {}, 453 | "outputs": [], 454 | "source": [ 455 | "0.5*(xk0[1:3] + xk0[8:10])" 456 | ] 457 | }, 458 | { 459 | "cell_type": "code", 460 | "execution_count": null, 461 | "id": "40b6b783", 462 | "metadata": {}, 463 | "outputs": [], 464 | "source": [ 465 | "0.5*(xkn[1:3] + xkn[8:10])" 466 | ] 467 | }, 468 | { 469 | "cell_type": "code", 470 | "execution_count": null, 471 | "id": "b1174abd", 472 | "metadata": {}, 473 | "outputs": [], 474 | "source": [ 475 | "using MeshCat, GeometryBasics, CoordinateTransformations, Rotations" 476 | ] 477 | }, 478 | { 479 | "cell_type": "code", 480 | "execution_count": null, 481 | "id": "1d7bcbf2", 482 | "metadata": {}, 483 | "outputs": [], 484 | "source": [ 485 | "vis = Visualizer()\n", 486 | "render(vis)" 487 | ] 488 | }, 489 | { 490 | "cell_type": "code", 491 | "execution_count": null, 492 | "id": "becd9774", 493 | "metadata": {}, 494 | "outputs": [], 495 | "source": [ 496 | "delete!(vis)\n", 497 | "\n", 498 | "green_material = MeshPhongMaterial(color=RGBA(0, 1, 0, 0.8))\n", 499 | "cylinder1 = Cylinder(Point(-.5, 0, 0), Point(0.5, 0, 0), 0.1)\n", 500 | "setobject!(vis[\"cylinder1\"],cylinder1,green_material)\n", 501 | "\n", 502 | "red_material = MeshPhongMaterial(color=RGBA(1, 0, 0, 0.8))\n", 503 | "cylinder2 = Cylinder(Point(-.5, 0, 0), Point(0.5, 0, 0), 0.1)\n", 504 | "setobject!(vis[\"cylinder2\"],cylinder2,red_material)\n", 505 | "\n", 506 | "for k = 1:N\n", 507 | " \n", 508 | " Xk = kinematics(xhist[:,k])\n", 509 | " \n", 510 | " # set position and attitude\n", 511 | " position1 = Translation(Xk[1:3]...)\n", 512 | " attitude1 = LinearMap(UnitQuaternion(Xk[4:7]))\n", 513 | " position2 = Translation(Xk[8:10]...)\n", 514 | " attitude2 = LinearMap(UnitQuaternion(Xk[11:14]))\n", 515 | " \n", 516 | " settransform!(vis[\"cylinder1\"], compose(position1,attitude1))\n", 517 | " settransform!(vis[\"cylinder2\"], compose(position2,attitude2))\n", 518 | " sleep(0.05)\n", 519 | "end" 520 | ] 521 | }, 522 | { 523 | "cell_type": "code", 524 | "execution_count": null, 525 | "id": "6a852ed0", 526 | "metadata": {}, 527 | "outputs": [], 528 | "source": [] 529 | } 530 | ], 531 | "metadata": { 532 | "kernelspec": { 533 | "display_name": "Julia 1.6.7", 534 | "language": "julia", 535 | "name": "julia-1.6" 536 | }, 537 | "language_info": { 538 | "file_extension": ".jl", 539 | "mimetype": "application/julia", 540 | "name": "julia", 541 | "version": "1.6.7" 542 | } 543 | }, 544 | "nbformat": 4, 545 | "nbformat_minor": 5 546 | } 547 | -------------------------------------------------------------------------------- /Lecture 22/Lecture 22.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamics-simulation-16-715/lecture-notebooks/a2f7642b3ba31629fc15ca77b738e3f21a5e6c8c/Lecture 22/Lecture 22.pdf -------------------------------------------------------------------------------- /Lecture 22/Project.toml: -------------------------------------------------------------------------------- 1 | [deps] 2 | CoordinateTransformations = "150eb455-5306-5404-9cee-2592286d6298" 3 | GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326" 4 | MeshCat = "283c5d60-a78f-5afe-a0af-af636b173e11" 5 | Rotations = "6038ab10-8711-5258-84ad-4b1120ba62dc" 6 | -------------------------------------------------------------------------------- /Lecture 22/floating-2bar-max.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 19, 6 | "id": "8819b5d3", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "name": "stderr", 11 | "output_type": "stream", 12 | "text": [ 13 | "\u001b[32m\u001b[1m Activating\u001b[22m\u001b[39m environment at `~/GitHub/dynamics-simulation-16-715/lecture-notebooks/Lecture 22/Project.toml`\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "using Pkg; Pkg.activate(@__DIR__); Pkg.instantiate()" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 20, 24 | "id": "41739eac", 25 | "metadata": {}, 26 | "outputs": [], 27 | "source": [ 28 | "using LinearAlgebra\n", 29 | "using ForwardDiff\n", 30 | "using Plots" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 21, 36 | "id": "efbcf314", 37 | "metadata": {}, 38 | "outputs": [ 39 | { 40 | "data": { 41 | "text/plain": [ 42 | "1.0" 43 | ] 44 | }, 45 | "execution_count": 21, 46 | "metadata": {}, 47 | "output_type": "execute_result" 48 | } 49 | ], 50 | "source": [ 51 | "#Parameters\n", 52 | "m1 = 1.0\n", 53 | "m2 = 1.0\n", 54 | "J1 = Diagonal([0.1; 1.0; 1.0])\n", 55 | "J2 = Diagonal([0.1; 1.0; 1.0])\n", 56 | "ℓ1 = 1.0\n", 57 | "ℓ2 = 1.0" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 22, 63 | "id": "14057d71", 64 | "metadata": {}, 65 | "outputs": [ 66 | { 67 | "data": { 68 | "text/plain": [ 69 | "12×12 SparseArrays.SparseMatrixCSC{Float64, Int64} with 12 stored entries:\n", 70 | " 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ \n", 71 | " ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ \n", 72 | " ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ \n", 73 | " ⋅ ⋅ ⋅ 0.1 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ \n", 74 | " ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ \n", 75 | " ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ \n", 76 | " ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ \n", 77 | " ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ \n", 78 | " ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ \n", 79 | " ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 0.1 ⋅ ⋅ \n", 80 | " ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ \n", 81 | " ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0" 82 | ] 83 | }, 84 | "execution_count": 22, 85 | "metadata": {}, 86 | "output_type": "execute_result" 87 | } 88 | ], 89 | "source": [ 90 | "M̄ = [m1*I(3) zeros(3,9);\n", 91 | " zeros(3,3) J1 zeros(3,6);\n", 92 | " zeros(3,6) m2*I(3) zeros(3,3);\n", 93 | " zeros(3,9) J2]" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 23, 99 | "id": "cc5df054", 100 | "metadata": {}, 101 | "outputs": [ 102 | { 103 | "data": { 104 | "text/plain": [ 105 | "101" 106 | ] 107 | }, 108 | "execution_count": 23, 109 | "metadata": {}, 110 | "output_type": "execute_result" 111 | } 112 | ], 113 | "source": [ 114 | "#Simulation Parameters\n", 115 | "h = 0.05 #20 Hz\n", 116 | "Tf = 5.0 #final time (sec)\n", 117 | "Thist = Array(0:h:Tf)\n", 118 | "N = length(Thist)" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 24, 124 | "id": "2a069858", 125 | "metadata": {}, 126 | "outputs": [ 127 | { 128 | "data": { 129 | "text/plain": [ 130 | "Ḡ (generic function with 1 method)" 131 | ] 132 | }, 133 | "execution_count": 24, 134 | "metadata": {}, 135 | "output_type": "execute_result" 136 | } 137 | ], 138 | "source": [ 139 | "function hat(ω)\n", 140 | " return [0 -ω[3] ω[2];\n", 141 | " ω[3] 0 -ω[1];\n", 142 | " -ω[2] ω[1] 0]\n", 143 | "end\n", 144 | "\n", 145 | "function L(Q)\n", 146 | " [Q[1] -Q[2:4]'; Q[2:4] Q[1]*I + hat(Q[2:4])]\n", 147 | "end\n", 148 | "\n", 149 | "function R(Q)\n", 150 | " [Q[1] -Q[2:4]'; Q[2:4] Q[1]*I - hat(Q[2:4])]\n", 151 | "end\n", 152 | "\n", 153 | "H = [zeros(1,3); I];\n", 154 | "\n", 155 | "T = Diagonal([1.0; -1; -1; -1])\n", 156 | "\n", 157 | "function G(Q)\n", 158 | " return L(Q)*H\n", 159 | "end\n", 160 | "\n", 161 | "function Ḡ(q)\n", 162 | " Q1 = q[4:7]\n", 163 | " Q2 = q[11:14]\n", 164 | " \n", 165 | " return [I zeros(3,9); zeros(4,3) G(Q1) zeros(4,6); zeros(3,6) I zeros(3,3); zeros(4,9) G(Q2)]\n", 166 | "end" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": 25, 172 | "id": "2d6609df", 173 | "metadata": {}, 174 | "outputs": [ 175 | { 176 | "data": { 177 | "text/plain": [ 178 | "DEL (generic function with 1 method)" 179 | ] 180 | }, 181 | "execution_count": 25, 182 | "metadata": {}, 183 | "output_type": "execute_result" 184 | } 185 | ], 186 | "source": [ 187 | "function DEL(q_1,q_2,q_3,λ,F1,F2)\n", 188 | " \n", 189 | " r1_1 = q_1[1:3]\n", 190 | " Q1_1 = q_1[4:7]\n", 191 | " r2_1 = q_1[8:10]\n", 192 | " Q2_1 = q_1[11:14]\n", 193 | " \n", 194 | " r1_2 = q_2[1:3]\n", 195 | " Q1_2 = q_2[4:7]\n", 196 | " r2_2 = q_2[8:10]\n", 197 | " Q2_2 = q_2[11:14]\n", 198 | " \n", 199 | " r1_3 = q_3[1:3]\n", 200 | " Q1_3 = q_3[4:7]\n", 201 | " r2_3 = q_3[8:10]\n", 202 | " Q2_3 = q_3[11:14]\n", 203 | " \n", 204 | " [(1/h)*m1*(r1_2-r1_1) - (1/h)*m1*(r1_3-r1_2);\n", 205 | " (2.0/h)*G(Q1_2)'*L(Q1_1)*H*J1*H'*L(Q1_1)'*Q1_2 + (2.0/h)*G(Q1_2)'*T*R(Q1_3)'*H*J1*H'*L(Q1_2)'*Q1_3;\n", 206 | " (1/h)*m2*(r2_2-r2_1) - (1/h)*m2*(r2_3-r2_2);\n", 207 | " (2.0/h)*G(Q2_2)'*L(Q2_1)*H*J2*H'*L(Q2_1)'*Q2_2 + (2.0/h)*G(Q2_2)'*T*R(Q2_3)'*H*J2*H'*L(Q2_2)'*Q2_3] + (h/2.0)*F1 + (h/2.0)*F2 + h*Dc(q_2)'*λ\n", 208 | " \n", 209 | "end" 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": 26, 215 | "id": "73e7f159", 216 | "metadata": {}, 217 | "outputs": [ 218 | { 219 | "data": { 220 | "text/plain": [ 221 | "Dq3DEL (generic function with 1 method)" 222 | ] 223 | }, 224 | "execution_count": 26, 225 | "metadata": {}, 226 | "output_type": "execute_result" 227 | } 228 | ], 229 | "source": [ 230 | "function Dq3DEL(q_1,q_2,q_3,λ,F1,F2)\n", 231 | " ForwardDiff.jacobian(dq->DEL(q_1,q_2,dq,λ,F1,F2), q_3)*Ḡ(q_3)\n", 232 | "end" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": 27, 238 | "id": "5647feb5", 239 | "metadata": {}, 240 | "outputs": [ 241 | { 242 | "data": { 243 | "text/plain": [ 244 | "c (generic function with 1 method)" 245 | ] 246 | }, 247 | "execution_count": 27, 248 | "metadata": {}, 249 | "output_type": "execute_result" 250 | } 251 | ], 252 | "source": [ 253 | "function c(q)\n", 254 | " r1 = q[1:3]\n", 255 | " Q1 = q[4:7]\n", 256 | " r2 = q[8:10]\n", 257 | " Q2 = q[11:14]\n", 258 | " \n", 259 | " [r1 + H'*R(Q1)'*L(Q1)*H*[0.5*ℓ1; 0; 0] - r2 - H'*R(Q2)'*L(Q2)*H*[-0.5*ℓ2; 0; 0];\n", 260 | " [0 1 0 0; 0 0 1 0]*L(Q1)'*Q2]\n", 261 | "end" 262 | ] 263 | }, 264 | { 265 | "cell_type": "code", 266 | "execution_count": 28, 267 | "id": "d0b7b96a", 268 | "metadata": {}, 269 | "outputs": [ 270 | { 271 | "data": { 272 | "text/plain": [ 273 | "Dc (generic function with 1 method)" 274 | ] 275 | }, 276 | "execution_count": 28, 277 | "metadata": {}, 278 | "output_type": "execute_result" 279 | } 280 | ], 281 | "source": [ 282 | "function Dc(q)\n", 283 | " ForwardDiff.jacobian(dq->c(dq),q)*Ḡ(q)\n", 284 | "end" 285 | ] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "execution_count": 29, 290 | "id": "94fd4535", 291 | "metadata": {}, 292 | "outputs": [ 293 | { 294 | "data": { 295 | "text/plain": [ 296 | "14-element Vector{Float64}:\n", 297 | " 0.0\n", 298 | " 0.0\n", 299 | " 0.0\n", 300 | " 1.0\n", 301 | " 0.0\n", 302 | " 0.0\n", 303 | " 0.0\n", 304 | " 1.0\n", 305 | " 0.0\n", 306 | " 0.0\n", 307 | " 1.0\n", 308 | " 0.0\n", 309 | " 0.0\n", 310 | " 0.0" 311 | ] 312 | }, 313 | "execution_count": 29, 314 | "metadata": {}, 315 | "output_type": "execute_result" 316 | } 317 | ], 318 | "source": [ 319 | "#initial conditions\n", 320 | "r1_0 = zeros(3)\n", 321 | "r2_0 = [1.0; 0; 0]\n", 322 | "Q1_0 = [1.0; 0; 0; 0]\n", 323 | "Q2_0 = [1.0; 0; 0; 0]\n", 324 | "\n", 325 | "q_0 = [r1_0; Q1_0; r2_0; Q2_0]" 326 | ] 327 | }, 328 | { 329 | "cell_type": "code", 330 | "execution_count": 31, 331 | "id": "d9211134", 332 | "metadata": {}, 333 | "outputs": [], 334 | "source": [ 335 | "#Torque input at joint\n", 336 | "uhist = .5*[0; 0; ones(19); -ones(40); zeros(10); ones(40); -ones(10)];\n", 337 | "\n", 338 | "#Corresponding F\n", 339 | "Fhist = zeros(12,N)\n", 340 | "for k = 1:N\n", 341 | " Fhist[:,k] = [0; 0; 0; 0; 0; -uhist[k]; 0; 0; 0; 0; 0; uhist[k]]\n", 342 | "end" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": 32, 348 | "id": "372cedc1", 349 | "metadata": {}, 350 | "outputs": [], 351 | "source": [ 352 | "#Simulate\n", 353 | "qhist = zeros(14,N)\n", 354 | "qhist[:,1] .= q_0\n", 355 | "qhist[:,2] .= q_0\n", 356 | "\n", 357 | "\n", 358 | "for k = 2:(N-1)\n", 359 | " \n", 360 | " #Initial guess\n", 361 | " qhist[:,k+1] .= qhist[:,k]\n", 362 | " λ = zeros(5)\n", 363 | " \n", 364 | " e = [DEL(qhist[:,k-1],qhist[:,k],qhist[:,k+1],λ,Fhist[:,k-1],Fhist[:,k]); c(qhist[:,k+1])]\n", 365 | " \n", 366 | " while maximum(abs.(e)) > 1e-12\n", 367 | " D = Dq3DEL(qhist[:,k-1],qhist[:,k],qhist[:,k+1],λ,Fhist[:,k-1],Fhist[:,k])\n", 368 | " C2 = Dc(qhist[:,k])\n", 369 | " C3 = Dc(qhist[:,k+1])\n", 370 | " \n", 371 | " Δ = -[D h*C2'; C3 zeros(5,5)]\\e\n", 372 | " \n", 373 | " qhist[1:3,k+1] .= qhist[1:3,k+1] + Δ[1:3]\n", 374 | " qhist[4:7,k+1] .= L(qhist[4:7,k+1])*[sqrt(1-Δ[4:6]'*Δ[4:6]); Δ[4:6]]\n", 375 | " qhist[8:10,k+1] .= qhist[8:10,k+1] + Δ[7:9]\n", 376 | " qhist[11:14,k+1] .= L(qhist[11:14,k+1])*[sqrt(1-Δ[10:12]'*Δ[10:12]); Δ[10:12]]\n", 377 | " \n", 378 | " λ .= λ + Δ[13:17]\n", 379 | " \n", 380 | " e = [DEL(qhist[:,k-1],qhist[:,k],qhist[:,k+1],λ,Fhist[:,k-1],Fhist[:,k]); c(qhist[:,k+1])]\n", 381 | " end\n", 382 | " \n", 383 | "end" 384 | ] 385 | }, 386 | { 387 | "cell_type": "code", 388 | "execution_count": 33, 389 | "id": "78411625", 390 | "metadata": {}, 391 | "outputs": [], 392 | "source": [ 393 | "using MeshCat, GeometryBasics, CoordinateTransformations, Rotations" 394 | ] 395 | }, 396 | { 397 | "cell_type": "code", 398 | "execution_count": 34, 399 | "id": "91b53d5b", 400 | "metadata": {}, 401 | "outputs": [ 402 | { 403 | "name": "stderr", 404 | "output_type": "stream", 405 | "text": [ 406 | "┌ Info: MeshCat server started. You can open the visualizer by visiting the following URL in your browser:\n", 407 | "│ http://127.0.0.1:8703\n", 408 | "└ @ MeshCat /Users/zac/.julia/packages/MeshCat/Ax8pH/src/visualizer.jl:73\n" 409 | ] 410 | }, 411 | { 412 | "data": { 413 | "text/html": [ 414 | "
\n", 415 | " \n", 416 | "
\n" 417 | ], 418 | "text/plain": [ 419 | "MeshCat.DisplayedVisualizer(MeshCat.CoreVisualizer(MeshCat.SceneTrees.SceneNode(nothing, nothing, Dict{String, Vector{UInt8}}(), nothing, Dict{String, MeshCat.SceneTrees.SceneNode}()), Set{Any}(), ip\"127.0.0.1\", 8703))" 420 | ] 421 | }, 422 | "execution_count": 34, 423 | "metadata": {}, 424 | "output_type": "execute_result" 425 | } 426 | ], 427 | "source": [ 428 | "vis = Visualizer()\n", 429 | "render(vis)" 430 | ] 431 | }, 432 | { 433 | "cell_type": "code", 434 | "execution_count": 35, 435 | "id": "6e64bf2d", 436 | "metadata": {}, 437 | "outputs": [], 438 | "source": [ 439 | "delete!(vis)\n", 440 | "\n", 441 | "green_material = MeshPhongMaterial(color=RGBA(0, 1, 0, 0.8))\n", 442 | "cylinder1 = Cylinder(Point(-.5, 0, 0), Point(0.5, 0, 0), 0.1)\n", 443 | "setobject!(vis[\"cylinder1\"],cylinder1,green_material)\n", 444 | "\n", 445 | "red_material = MeshPhongMaterial(color=RGBA(1, 0, 0, 0.8))\n", 446 | "cylinder2 = Cylinder(Point(-.5, 0, 0), Point(0.5, 0, 0), 0.1)\n", 447 | "setobject!(vis[\"cylinder2\"],cylinder2,red_material)\n", 448 | "\n", 449 | "for k = 1:N\n", 450 | " \n", 451 | " # set position and attitude\n", 452 | " position1 = Translation(qhist[1:3,k]...)\n", 453 | " attitude1 = LinearMap(UnitQuaternion(qhist[4:7,k]))\n", 454 | " position2 = Translation(qhist[8:10,k]...)\n", 455 | " attitude2 = LinearMap(UnitQuaternion(qhist[11:14,k]))\n", 456 | " \n", 457 | " settransform!(vis[\"cylinder1\"], compose(position1,attitude1))\n", 458 | " settransform!(vis[\"cylinder2\"], compose(position2,attitude2))\n", 459 | " sleep(0.05)\n", 460 | "end" 461 | ] 462 | }, 463 | { 464 | "cell_type": "code", 465 | "execution_count": 36, 466 | "id": "8187353b-2141-4443-99c9-f14225431c69", 467 | "metadata": {}, 468 | "outputs": [ 469 | { 470 | "data": { 471 | "text/plain": [ 472 | "3-element Vector{Float64}:\n", 473 | " 0.5\n", 474 | " 0.0\n", 475 | " 0.0" 476 | ] 477 | }, 478 | "execution_count": 36, 479 | "metadata": {}, 480 | "output_type": "execute_result" 481 | } 482 | ], 483 | "source": [ 484 | "0.5*(qhist[1:3,1] + qhist[8:10,1])" 485 | ] 486 | }, 487 | { 488 | "cell_type": "code", 489 | "execution_count": 37, 490 | "id": "0a3da13f-2c69-49f4-a36f-9dd5949414ee", 491 | "metadata": {}, 492 | "outputs": [ 493 | { 494 | "data": { 495 | "text/plain": [ 496 | "3-element Vector{Float64}:\n", 497 | " 0.5000000000000083\n", 498 | " 0.0\n", 499 | " 0.0" 500 | ] 501 | }, 502 | "execution_count": 37, 503 | "metadata": {}, 504 | "output_type": "execute_result" 505 | } 506 | ], 507 | "source": [ 508 | "0.5*(qhist[1:3,end] + qhist[8:10,end])" 509 | ] 510 | }, 511 | { 512 | "cell_type": "code", 513 | "execution_count": null, 514 | "id": "da9ef9ab-0c0c-46fc-87f2-e766b286f97a", 515 | "metadata": {}, 516 | "outputs": [], 517 | "source": [] 518 | } 519 | ], 520 | "metadata": { 521 | "kernelspec": { 522 | "display_name": "Julia 1.6.7", 523 | "language": "julia", 524 | "name": "julia-1.6" 525 | }, 526 | "language_info": { 527 | "file_extension": ".jl", 528 | "mimetype": "application/julia", 529 | "name": "julia", 530 | "version": "1.6.7" 531 | } 532 | }, 533 | "nbformat": 4, 534 | "nbformat_minor": 5 535 | } 536 | -------------------------------------------------------------------------------- /Lecture 23/Lecture 23.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamics-simulation-16-715/lecture-notebooks/a2f7642b3ba31629fc15ca77b738e3f21a5e6c8c/Lecture 23/Lecture 23.pdf -------------------------------------------------------------------------------- /Lecture 3/Lecture 3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamics-simulation-16-715/lecture-notebooks/a2f7642b3ba31629fc15ca77b738e3f21a5e6c8c/Lecture 3/Lecture 3.pdf -------------------------------------------------------------------------------- /Lecture 3/Project.toml: -------------------------------------------------------------------------------- 1 | [deps] 2 | ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" 3 | MeshCat = "283c5d60-a78f-5afe-a0af-af636b173e11" 4 | RobotZoo = "74be38bb-dcc2-4b9e-baf3-d6373cd95f10" 5 | StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" 6 | TrajOptPlots = "7770976a-8dee-4930-bf39-a1782fd21ce6" 7 | -------------------------------------------------------------------------------- /Lecture 3/pendulum-rk2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "7438b978", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "import Pkg; Pkg.activate(@__DIR__); Pkg.instantiate()" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": null, 16 | "id": "eaec848b", 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "using LinearAlgebra\n", 21 | "using PyPlot\n", 22 | "using ForwardDiff" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": null, 28 | "id": "99b6635d", 29 | "metadata": {}, 30 | "outputs": [], 31 | "source": [ 32 | "ℓ = 1.0\n", 33 | "m = 1.0\n", 34 | "c = 30.0\n", 35 | "g = 9.81\n", 36 | "\n", 37 | "function f(x)\n", 38 | " #pendulum dynamics\n", 39 | " \n", 40 | " θ = x[1]\n", 41 | " θ̇ = x[2]\n", 42 | " \n", 43 | " ẋ = [θ̇; -(g/ℓ)*sin(θ)-c*θ̇]\n", 44 | "end" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": null, 50 | "id": "529d295e", 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [ 54 | "function euler_step(xk)\n", 55 | " xn = xk + h*f(xk)\n", 56 | "end" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": null, 62 | "id": "fa2a2588", 63 | "metadata": {}, 64 | "outputs": [], 65 | "source": [ 66 | "function taylor_step(xk)\n", 67 | " Ak = ForwardDiff.jacobian(f,xk)\n", 68 | " xn = xk + h*f(xk) + 0.5*h*h*Ak*f(xk)\n", 69 | "end" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": null, 75 | "id": "fa802858", 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "function implicit_midpoint_step(xk)\n", 80 | " xn = xk\n", 81 | " r = xn - xk - h*f(0.5*xk + 0.5*xn)\n", 82 | " while maximum(abs.(r)) > 1e-12\n", 83 | " R = ForwardDiff.jacobian(x -> x-xk-h*f(0.5*xk + 0.5*x), xn)\n", 84 | " Δxn = -R\\r\n", 85 | " xn += Δxn\n", 86 | " r = xn - xk - h*f(0.5*xk + 0.5*xn)\n", 87 | " end\n", 88 | " \n", 89 | " return xn\n", 90 | "end" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": null, 96 | "id": "eccd70d7", 97 | "metadata": {}, 98 | "outputs": [], 99 | "source": [ 100 | "function explicit_midpoint_step(xk)\n", 101 | " xm = xk + 0.5*h*f(xk)\n", 102 | " xn = xk + h*f(xm)\n", 103 | "end" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": null, 109 | "id": "533e2eda", 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [ 113 | "function simulate_euler!(xtraj, N)\n", 114 | " for k = 1:(N-1)\n", 115 | " xtraj[:,k+1] .= euler_step(xtraj[:,k])\n", 116 | " end\n", 117 | "end" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": null, 123 | "id": "d29c2467", 124 | "metadata": {}, 125 | "outputs": [], 126 | "source": [ 127 | "function simulate_taylor!(xtraj, N)\n", 128 | " for k = 1:(N-1)\n", 129 | " xtraj[:,k+1] .= taylor_step(xtraj[:,k])\n", 130 | " end\n", 131 | "end" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": null, 137 | "id": "d9a5b652", 138 | "metadata": {}, 139 | "outputs": [], 140 | "source": [ 141 | "function simulate_implicit_mid!(xtraj, N)\n", 142 | " for k = 1:(N-1)\n", 143 | " xtraj[:,k+1] .= implicit_midpoint_step(xtraj[:,k])\n", 144 | " end\n", 145 | "end" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": null, 151 | "id": "069bec52", 152 | "metadata": {}, 153 | "outputs": [], 154 | "source": [ 155 | "function simulate_explicit_mid!(xtraj, N)\n", 156 | " for k = 1:(N-1)\n", 157 | " xtraj[:,k+1] .= explicit_midpoint_step(xtraj[:,k])\n", 158 | " end\n", 159 | "end" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": null, 165 | "id": "78c55ebc", 166 | "metadata": {}, 167 | "outputs": [], 168 | "source": [ 169 | "Tf = 10.0\n", 170 | "h = 0.1 #50 Hz\n", 171 | "N = Int(floor(Tf./h + 1))\n", 172 | "thist = h.*Array(0:(N-1));" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": null, 178 | "id": "7d1f831a", 179 | "metadata": {}, 180 | "outputs": [], 181 | "source": [ 182 | "x0 = [30*(pi/180); 0.0]\n", 183 | "xtraj1 = zeros(2,N)\n", 184 | "xtraj2 = zeros(2,N)\n", 185 | "xtraj3 = zeros(2,N)\n", 186 | "xtraj4 = zeros(2,N)\n", 187 | "xtraj1[:,1] = x0;\n", 188 | "xtraj2[:,1] = x0;\n", 189 | "xtraj3[:,1] = x0;\n", 190 | "xtraj4[:,1] = x0;" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": null, 196 | "id": "9cd13c10", 197 | "metadata": {}, 198 | "outputs": [], 199 | "source": [ 200 | "simulate_euler!(xtraj1, N)" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": null, 206 | "id": "7dd27c6c", 207 | "metadata": {}, 208 | "outputs": [], 209 | "source": [ 210 | "simulate_taylor!(xtraj2, N)" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": null, 216 | "id": "93f21c74", 217 | "metadata": {}, 218 | "outputs": [], 219 | "source": [ 220 | "simulate_implicit_mid!(xtraj3, N)" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": null, 226 | "id": "f81193c3", 227 | "metadata": {}, 228 | "outputs": [], 229 | "source": [ 230 | "simulate_explicit_mid!(xtraj4, N)" 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": null, 236 | "id": "c5a6be11", 237 | "metadata": {}, 238 | "outputs": [], 239 | "source": [ 240 | "using TrajOptPlots\n", 241 | "using MeshCat\n", 242 | "using StaticArrays\n", 243 | "using RobotZoo\n", 244 | "\n", 245 | "vis = Visualizer()\n", 246 | "TrajOptPlots.set_mesh!(vis, RobotZoo.Pendulum())\n", 247 | "render(vis)" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": null, 253 | "id": "25d7b9b2", 254 | "metadata": {}, 255 | "outputs": [], 256 | "source": [ 257 | "X1 = [SVector{2}(x) for x in eachcol(xtraj3)];\n", 258 | "visualize!(vis, RobotZoo.Pendulum(), thist[end], X1)" 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": null, 264 | "id": "556888d0", 265 | "metadata": {}, 266 | "outputs": [], 267 | "source": [ 268 | "#plot(thist,xtraj1[1,:])\n", 269 | "#plot(thist,xtraj2[1,:])\n", 270 | "plot(thist,xtraj3[1,:])\n", 271 | "plot(thist,xtraj4[1,:])\n", 272 | "xlabel(\"Time (sec)\")\n", 273 | "ylabel(\"θ (rad)\")" 274 | ] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "execution_count": null, 279 | "id": "883f5f3b", 280 | "metadata": { 281 | "scrolled": true 282 | }, 283 | "outputs": [], 284 | "source": [ 285 | "#Let's plot the total energy of the system\n", 286 | "Ehist1 = 0.5*m*ℓ^2*(xtraj1[2,:].^2) .+ m*g*ℓ*(1.0.-cos.(xtraj1[1,:]))\n", 287 | "Ehist2 = 0.5*m*ℓ^2*(xtraj2[2,:].^2) .+ m*g*ℓ*(1.0.-cos.(xtraj2[1,:]))\n", 288 | "Ehist3 = 0.5*m*ℓ^2*(xtraj3[2,:].^2) .+ m*g*ℓ*(1.0.-cos.(xtraj3[1,:]))\n", 289 | "Ehist4 = 0.5*m*ℓ^2*(xtraj4[2,:].^2) .+ m*g*ℓ*(1.0.-cos.(xtraj4[1,:]))\n", 290 | "#plot(thist,Ehist1)\n", 291 | "#plot(thist,Ehist2)\n", 292 | "plot(thist,Ehist3)\n", 293 | "#plot(thist,Ehist4)\n", 294 | "xlabel(\"Time (sec)\")\n", 295 | "ylabel(\"Total Energy\")" 296 | ] 297 | }, 298 | { 299 | "cell_type": "code", 300 | "execution_count": null, 301 | "id": "74cb6c9b-415c-4f80-969d-083c5891f4d3", 302 | "metadata": {}, 303 | "outputs": [], 304 | "source": [] 305 | } 306 | ], 307 | "metadata": { 308 | "kernelspec": { 309 | "display_name": "Julia 1.6.7", 310 | "language": "julia", 311 | "name": "julia-1.6" 312 | }, 313 | "language_info": { 314 | "file_extension": ".jl", 315 | "mimetype": "application/julia", 316 | "name": "julia", 317 | "version": "1.6.7" 318 | } 319 | }, 320 | "nbformat": 4, 321 | "nbformat_minor": 5 322 | } 323 | -------------------------------------------------------------------------------- /Lecture 4/Lecture 4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamics-simulation-16-715/lecture-notebooks/a2f7642b3ba31629fc15ca77b738e3f21a5e6c8c/Lecture 4/Lecture 4.pdf -------------------------------------------------------------------------------- /Lecture 4/Project.toml: -------------------------------------------------------------------------------- 1 | [deps] 2 | ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" 3 | MeshCat = "283c5d60-a78f-5afe-a0af-af636b173e11" 4 | RobotZoo = "74be38bb-dcc2-4b9e-baf3-d6373cd95f10" 5 | StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" 6 | TrajOptPlots = "7770976a-8dee-4930-bf39-a1782fd21ce6" 7 | -------------------------------------------------------------------------------- /Lecture 5/Lecture 5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamics-simulation-16-715/lecture-notebooks/a2f7642b3ba31629fc15ca77b738e3f21a5e6c8c/Lecture 5/Lecture 5.pdf -------------------------------------------------------------------------------- /Lecture 6/Lecture 6.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamics-simulation-16-715/lecture-notebooks/a2f7642b3ba31629fc15ca77b738e3f21a5e6c8c/Lecture 6/Lecture 6.pdf -------------------------------------------------------------------------------- /Lecture 7/Lecture 7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamics-simulation-16-715/lecture-notebooks/a2f7642b3ba31629fc15ca77b738e3f21a5e6c8c/Lecture 7/Lecture 7.pdf -------------------------------------------------------------------------------- /Lecture 8/Lecture 8.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamics-simulation-16-715/lecture-notebooks/a2f7642b3ba31629fc15ca77b738e3f21a5e6c8c/Lecture 8/Lecture 8.pdf -------------------------------------------------------------------------------- /Lecture 9/Lecture 9.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamics-simulation-16-715/lecture-notebooks/a2f7642b3ba31629fc15ca77b738e3f21a5e6c8c/Lecture 9/Lecture 9.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # lecture-notebooks --------------------------------------------------------------------------------