Skip to content

Commit

Permalink
use plotly; fix bitrot
Browse files Browse the repository at this point in the history
  • Loading branch information
jverzani committed Jun 7, 2024
1 parent 0bcc8b5 commit 55f37a6
Show file tree
Hide file tree
Showing 59 changed files with 423 additions and 176 deletions.
7 changes: 4 additions & 3 deletions quarto/ODEs/differential_equations.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -384,15 +384,16 @@ Finally, we note that the `ModelingToolkit` package provides symbolic-numeric co

```{julia}
#| hold: true
@parameters t γ g
@variables x(t) y(t)
@parameters γ g
@variables t x(t) y(t)
D = Differential(t)
eqs = [D(D(x)) ~ -γ * D(x),
D(D(y)) ~ -g - γ * D(y)]
@named sys = ODESystem(eqs)
@named sys = ODESystem(eqs, t, [x,y], [γ,g])
sys = ode_order_lowering(sys) # turn 2nd order into 1st
sys = structural_simplify(sys)
u0 = [D(x) => vxy₀[1],
D(y) => vxy₀[2],
Expand Down
8 changes: 7 additions & 1 deletion quarto/_common_code.qmd
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
```{julia}
#| output: false
#| echo: false
## Formatting options are included here; not in CalculusWithJulia.WeaveSupport
Expand All @@ -7,18 +8,20 @@ nothing
```

```{julia}
#| output: false
#| echo: false
fig_size=(800, 600)
nothing
```

```{julia}
#| output: false
#| echo: false
import Logging
Logging.disable_logging(Logging.Info) # or e.g. Logging.Info
Logging.disable_logging(Logging.Warn)
nothing
```

```{julia}
Expand All @@ -36,6 +39,7 @@ end
```

```{julia}
#| output: false
#| echo: false
# ImageFile
## WeaveSupport from CalculusWithJulia package
Expand Down Expand Up @@ -220,4 +224,6 @@ function Base.show(io::IO, m::MIME"text/plain", x::HTMLoutput)
println(io, caption)
return nothing
end
nothing
```
1 change: 1 addition & 0 deletions quarto/_quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,5 @@ format:

execute:
error: false
# freeze: false
freeze: auto
5 changes: 4 additions & 1 deletion quarto/alternatives/SciML.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ We can solve for several parameters at once, by using a matching number of initi

```{julia}
ps = [1, 2, 3, 4]
u0 = @SVector[1, 1, 1, 1]
u0 = [1, 1, 1, 1]
prob = NonlinearProblem(f, u0, ps)
solve(prob, NewtonRaphson())
```
Expand Down Expand Up @@ -235,13 +235,15 @@ The extra step is to specify a "`NonlinearSystem`." It is a system, as in practi

```{julia}
ns = NonlinearSystem([eq], [x], [α], name=:ns);
ns = complete(ns)
```

The `name` argument is special. The name of the object (`ns`) is assigned through `=`, but the system must also know this same name. However, the name on the left is not known when the name on the right is needed, so it is up to the user to keep them synchronized. The `@named` macro handles this behind the scenes by simply rewriting the syntax of the assignment:


```{julia}
@named ns = NonlinearSystem([eq], [x], [α]);
ns = complete(ns)
```

With the system defined, we can pass this to `NonlinearProblem`, as was done with a function. The parameter is specified here, and in this case is `α => 1.0`. The initial guess is `[1.0]`:
Expand Down Expand Up @@ -366,6 +368,7 @@ The above should be self explanatory. To put into a form to pass to `solve` we d

```{julia}
@named sys = OptimizationSystem(Area, [x], [P]);
sys = complete(sys)
```

(This step is different, as before an `OptimizationFunction` was defined; we use `@named`, as above, to ensure the system has the same name as the identifier, `sys`.)
Expand Down
78 changes: 78 additions & 0 deletions quarto/derivatives/alternative_implicit.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
## THIS IS FAILING
## Appendix


There are other packages in the `Julia` ecosystem that can plot implicit equations.


### The ImplicitEquations package


The `ImplicitEquations` packages can plot equations and inequalities. The use is somewhat similar to the examples above, but the object plotted is a predicate, not a function. These predicates are created with functions like `Eq` or `Lt`.


For example, the `ImplicitPlots` manual shows this function $f(x,y) = (x^4 + y^4 - 1) \cdot (x^2 + y^2 - 2) + x^5 \cdot y$ to plot. Using `ImplicitEquations`, this equation would be plotted with:


```{julia}
#| hold: true
using ImplicitEquations
f(x,y) = (x^4 + y^4 - 1) * (x^2 + y^2 - 2) + x^5 * y
r = Eq(f, 0) # the equation f(x,y) = 0
plot(r)
```

Unlike `ImplicitPlots`, inequalities may be displayed:


```{julia}
#| hold: true
f(x,y) = (x^4 + y^4 - 1) * (x^2 + y^2 - 2) + x^5 * y
r = Lt(f, 0) # the inequality f(x,y) < 0
plot(r; M=10, N=10) # less blocky
```

The rendered plots look "blocky" due to the algorithm used to plot the equations. As there is no rule defining $(x,y)$ pairs to plot, a search by regions is done. A region is initially labeled undetermined. If it can be shown that for any value in the region the equation is true (equations can also be inequalities), the region is colored black. If it can be shown it will never be true, the region is dropped. If a black-and-white answer is not clear, the region is subdivided and each subregion is similarly tested. This continues until the remaining undecided regions are smaller than some threshold. Such regions comprise a boundary, and here are also colored black. Only regions are plotted - not $(x,y)$ pairs - so the results are blocky. Pass larger values of $N=M$ (with defaults of $8$) to `plot` to lower the threshold at the cost of longer computation times, as seen in the last example.


### The IntervalConstraintProgramming package


The `IntervalConstraintProgramming` package also can be used to graph implicit equations. For certain problem descriptions it is significantly faster and makes better graphs. The usage is slightly more involved. We show the commands, but don't run them here, as there are minor conflicts with the `CalculusWithJulia`package.


We specify a problem using the `@constraint` macro. Using a macro allows expressions to involve free symbols, so the problem is specified in an equation-like manner:


```{julia}
#| eval: false
S = @constraint x^2 + y^2 <= 2
```

The right hand side must be a number.


The area to plot over must be specified as an `IntervalBox`, basically a pair of intervals. The interval $[a,b]$ is expressed through `a..b`:


```{julia}
#| eval: false
J = -3..3
X = IntervalArithmetic.IntervalBox(J, J)
```

The `pave` command does the heavy lifting:


```{julia}
#| eval: false
region = IntervalConstraintProgramming.pave(S, X)
```

A plot can be made of either the boundary, the interior, or both.


```{julia}
#| eval: false
plot(region.inner) # plot interior; use r.boundary for boundary
```
5 changes: 3 additions & 2 deletions quarto/derivatives/curve_sketching.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This section uses the following add-on packages:
```{julia}
using CalculusWithJulia
using Plots
plotly()
using SymPy
using Roots
using Polynomials # some name clash with SymPy
Expand Down Expand Up @@ -38,7 +39,7 @@ With these, a sketch fills in between the points/lines associated with these val
#| echo: false
#| cache: true
### {{{ sketch_sin_plot }}}
gr()
function sketch_sin_plot_graph(i)
f(x) = 10*sin(pi/2*x) # [0,4]
Expand Down Expand Up @@ -84,7 +85,7 @@ end
imgfile = tempname() * ".gif"
gif(anim, imgfile, fps = 1)
plotly()
ImageFile(imgfile, caption)
```

Expand Down
5 changes: 5 additions & 0 deletions quarto/derivatives/derivatives.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This section uses these add-on packages:
```{julia}
using CalculusWithJulia
using Plots
plotly()
using SymPy
```

Expand Down Expand Up @@ -218,6 +219,7 @@ The slope of the secant line represents the average rate of change over a given
#| hold: true
#| echo: false
#| cache: true
gr()
function secant_line_tangent_line_graph(n)
f(x) = sin(x)
c = pi/3
Expand Down Expand Up @@ -251,6 +253,7 @@ end
imgfile = tempname() * ".gif"
gif(anim, imgfile, fps = 1)
plotly()
ImageFile(imgfile, caption)
```

Expand All @@ -268,6 +271,7 @@ We will define the tangent line at $(c, f(c))$ to be the line through the point
#| hold: true
#| echo: false
#| cache: true
gr()
function line_approx_fn_graph(n)
f(x) = sin(x)
c = pi/3
Expand Down Expand Up @@ -296,6 +300,7 @@ end
imgfile = tempname() * ".gif"
gif(anim, imgfile, fps = 1)
plotly()
ImageFile(imgfile, caption)
```

Expand Down
1 change: 1 addition & 0 deletions quarto/derivatives/first_second_derivatives.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This section uses these add-on packages:
```{julia}
using CalculusWithJulia
using Plots
plotly()
using SymPy
using Roots
```
Expand Down
79 changes: 1 addition & 78 deletions quarto/derivatives/implicit_differentiation.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This section uses these add-on packages:
```{julia}
using CalculusWithJulia
using Plots
plotly()
using Roots
using SymPy
```
Expand Down Expand Up @@ -993,81 +994,3 @@ choices = ["concave up", "concave down", "both concave up and down"]
answ = 3
radioq(choices, answ, keep_order=true)
```

## Appendix


There are other packages in the `Julia` ecosystem that can plot implicit equations.


### The ImplicitEquations package


The `ImplicitEquations` packages can plot equations and inequalities. The use is somewhat similar to the examples above, but the object plotted is a predicate, not a function. These predicates are created with functions like `Eq` or `Lt`.


For example, the `ImplicitPlots` manual shows this function $f(x,y) = (x^4 + y^4 - 1) \cdot (x^2 + y^2 - 2) + x^5 \cdot y$ to plot. Using `ImplicitEquations`, this equation would be plotted with:


```{julia}
#| hold: true
using ImplicitEquations
f(x,y) = (x^4 + y^4 - 1) * (x^2 + y^2 - 2) + x^5 * y
r = Eq(f, 0) # the equation f(x,y) = 0
plot(r)
```

Unlike `ImplicitPlots`, inequalities may be displayed:


```{julia}
#| hold: true
f(x,y) = (x^4 + y^4 - 1) * (x^2 + y^2 - 2) + x^5 * y
r = Lt(f, 0) # the inequality f(x,y) < 0
plot(r; M=10, N=10) # less blocky
```

The rendered plots look "blocky" due to the algorithm used to plot the equations. As there is no rule defining $(x,y)$ pairs to plot, a search by regions is done. A region is initially labeled undetermined. If it can be shown that for any value in the region the equation is true (equations can also be inequalities), the region is colored black. If it can be shown it will never be true, the region is dropped. If a black-and-white answer is not clear, the region is subdivided and each subregion is similarly tested. This continues until the remaining undecided regions are smaller than some threshold. Such regions comprise a boundary, and here are also colored black. Only regions are plotted - not $(x,y)$ pairs - so the results are blocky. Pass larger values of $N=M$ (with defaults of $8$) to `plot` to lower the threshold at the cost of longer computation times, as seen in the last example.


### The IntervalConstraintProgramming package


The `IntervalConstraintProgramming` package also can be used to graph implicit equations. For certain problem descriptions it is significantly faster and makes better graphs. The usage is slightly more involved. We show the commands, but don't run them here, as there are minor conflicts with the `CalculusWithJulia`package.


We specify a problem using the `@constraint` macro. Using a macro allows expressions to involve free symbols, so the problem is specified in an equation-like manner:


```{julia}
#| eval: false
S = @constraint x^2 + y^2 <= 2
```

The right hand side must be a number.


The area to plot over must be specified as an `IntervalBox`, basically a pair of intervals. The interval $[a,b]$ is expressed through `a..b`:


```{julia}
#| eval: false
J = -3..3
X = IntervalArithmetic.IntervalBox(J, J)
```

The `pave` command does the heavy lifting:


```{julia}
#| eval: false
region = IntervalConstraintProgramming.pave(S, X)
```

A plot can be made of either the boundary, the interior, or both.


```{julia}
#| eval: false
plot(region.inner) # plot interior; use r.boundary for boundary
```
5 changes: 3 additions & 2 deletions quarto/derivatives/lhospitals_rule.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ This section uses these add-on packages:
```{julia}
using CalculusWithJulia
using Plots
plotly()
using SymPy
```


Expand Down Expand Up @@ -245,6 +245,7 @@ A first proof of L'Hospital's rule takes advantage of Cauchy's [generalization](
#| echo: false
#| cache: true
using Roots
gr()
let
## {{{lhopitals_picture}}}
Expand Down Expand Up @@ -293,7 +294,7 @@ imgfile = tempname() * ".gif"
gif(anim, imgfile, fps = 1)
plotly()
plotly()
ImageFile(imgfile, caption)
end
```
Expand Down
1 change: 1 addition & 0 deletions quarto/derivatives/linearization.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This section uses these add-on packages:
```{julia}
using CalculusWithJulia
using Plots
plotly()
using SymPy
using TaylorSeries
using DualNumbers
Expand Down
Loading

0 comments on commit 55f37a6

Please sign in to comment.