CalculusWithJulia.jl
Documentation for CalculusWithJulia.jl, a package to accompany the notes calculuswithjulia.github.io for using Julia
for Calculus.
Index
CalculusWithJulia.CalculusWithJulia
— ModuleCalculusWithJulia
A package to accompany notes on using Julia with calculus.
This package does two things:
It loads a few other packages making it easier to use (and install) the functionality provided by them and
It defines a handful of functions for convenience. The exported ones
are e
, unzip
, rangeclamp
tangent
, secant
, D
(and the prime notation), divergence
, gradient
, curl
, and ∇
, along with some plotting functions
Packages loaded by CalculusWithJulia
The
SpecialFunctions
is loaded giving access to a few special functions used in these notes, e.g.,airyai
,gamma
The
ForwardDiff
package is loaded giving access to itsderivative
,gradient
,jacobian
, andhessian
functions for finding automatic derivatives of functions. In addition, this package defines'
(for functions) to return a derivative (which commits type piracy),∇
to find the gradient (∇(f)
), the divergence (∇⋅F
). and the curl (∇×F
), along withdivergence
andcurl
.
The
LinearAlgebra
package is loaded for access to several of its functions for working with vectorsnorm
,cdot
(⋅
),cross
(×
),det
.The
PlotUtils
package is loaded so that itsadapted_grid
function is available.
Packages with extra features added when loaded
The Julia
package Requires
allows for additional code to be run when another package is loaded. The following packages have additional code to load:
SymPy
: for symbolic math.Plots
: thePlots
package provides a plotting interface.
Several plot recipes are provided to ease the creation of plots in the notes. plotif
, trimplot
, and signchart
are used for plotting univariate functions; plot_polar
and plot_parametric
are used to plot curves in 2 or 3 dimensions; plot_parametric
also makes the plotting og parameterically defined surfaces easier; vectorfieldplot
and vectorfieldplot3d
can be used to plot vector fields; and arrow
is a simplified interface to quiver
that also indicates 3D vectors.
The plot_implicit
function can plot 2D
implicit plots. (It is borrowed from ImplicitPlots.jl, which is avoided, as it has dependencies that hold other packages back.)
Other packages with a recurring role in the accompanying notes:
Roots
is used to find zeros of univariate functionsSymPy
for symbolic mathQuadGK
andHCubature
are used for numeric integration
CalculusWithJulia.D
— FunctionD(f)
Function interface to ForwardDiff.derivative
.
Also overrides f'
to take take a derivative.
CalculusWithJulia.arrow!
— Methodarrow(p, v)
Add vector, v
, to plot anchored at point p
.
Example
Fn = parametric(t -> [2cos(t), 3sin(t)])
Fnp = t -> ForwardDiff.derivative(Fn, t)
p = plot(Fn, 0, 2pi, legend=false)
for t in 0:pi/4:pi
arrow!(Fn(t), Fnp(t))
end
p
CalculusWithJulia.arrow!
— Methodarrow(p, v)
Add vector, v
, to plot anchored at point p
.
Example
Fn = parametric(t -> [2cos(t), 3sin(t)])
Fnp = t -> ForwardDiff.derivative(Fn, t)
p = plot(Fn, 0, 2pi, legend=false)
for t in 0:pi/4:pi
arrow!(Fn(t), Fnp(t))
end
p
CalculusWithJulia.arrow
— Methodarrow(p, v)
Add vector, v
, to plot anchored at point p
.
Example
Fn = parametric(t -> [2cos(t), 3sin(t)])
Fnp = t -> ForwardDiff.derivative(Fn, t)
p = plot(Fn, 0, 2pi, legend=false)
for t in 0:pi/4:pi
arrow!(Fn(t), Fnp(t))
end
p
CalculusWithJulia.curl
— Methodcurl(F)
Find curl of a 2 or 3-D vector field.
CalculusWithJulia.divergence
— Methoddivergence(F)
Find divergence of a 3-D vector vield.
CalculusWithJulia.plotif!
— Methodplotif(f, g, a, b)
Plot f colored depending on g < 0 or not.
CalculusWithJulia.plotif!
— Methodplotif(f, g, a, b)
Plot f colored depending on g < 0 or not.
CalculusWithJulia.plotif
— Methodplotif(f, g, a, b)
Plot f colored depending on g < 0 or not.
CalculusWithJulia.rangeclamp
— Functionrangeclamp(f, hi=20, lo=-hi; replacement=NaN)
Modify f
so that values of f(x)
outside of [lo,hi]
are replaced by replacement
.
Examples
f(x) = 1/x
plot(rangeclamp(f), -1, 1)
plot(rangeclamp(f, 10), -1, 1) # no `abs(y)` values exceeding 10
CalculusWithJulia.riemann
— Methodriemann: compute Riemann sum approximations to a definite integral. As well, implement trapezoid and Simpson's rule.
Example:
f(x) = exp(x^2)
riemann(f, 0, 1, 1000) # default right-Riemann sums
riemann(f, 0, 1, 1000, method="left") # left sums
riemann(f, 0, 1, 1000, method="trapezoid") # use trapezoid rule
riemann(f, 0, 1, 1000, method="simpsons") # use Simpson's rule
CalculusWithJulia.secant
— Methodsecant(f::Function, a, b)
Returns a function describing the secant line to the graph of f
at x=a
and x=b
.
Example. Where does the secant line intersect the y
axis?
f(x) = sin(x)
a, b = pi/4, pi/3
sl(x) = secant(f, a, b)(x) # or sl = sl(f, a, b) to use a non-generic function
sl(0)
CalculusWithJulia.sign_chart
— Methodsign_chart(f, a, b; atol=1e-4)
Create a sign chart for f
over (a,b)
. Returns a tuple with an identified zero or vertical asymptote and the corresponding sign change. The tolerance is used to disambiguate numerically found values.
Example
julia> sign_chart(x -> x/(x-1)^2, -5, 5)
2-element Vector{NamedTuple{(:∞0, :sign_change), Tuple{Float64, String}}}:
(∞0 = 0.0, sign_change = "- → +")
(∞0 = 1.0000000000000002, sign_change = "+ → +")
CalculusWithJulia.tangent
— Methodtangent(f::Function, c)
Returns a function describing the tangent line to the graph of f
at x=c
.
Example. Where does the tangent line intersect the y axis?
f(x) = sin(x)
tl(x) = tangent(f, pi/4)(x) # or tl = tangent(f, pi/3) to use a non-generic function
tl(0)
Uses the automatic derivative of f
to find the slope of the tangent line at x=c
.
CalculusWithJulia.trimplot!
— Methodtrimplot(f, a, b, c=20; kwargs...)
Plot f over [a,b] but break graph if it exceeds c in absolute value.
CalculusWithJulia.trimplot!
— Methodtrimplot(f, a, b, c=20; kwargs...)
Plot f over [a,b] but break graph if it exceeds c in absolute value.
CalculusWithJulia.trimplot
— Methodtrimplot(f, a, b, c=20; kwargs...)
Plot f over [a,b] but break graph if it exceeds c in absolute value.
CalculusWithJulia.unzip
— Method`unzip(vs)`
`unzip(v1, v2, ...)`
`unzip(r::Function, a, b)`
Take a vector of points described by vectors (as returned by, say r(t)=[sin(t),cos(t)], r.([1,2,3])
, and return a tuple of collected x values, y values, and optionally z values.
Wrapper around the invert
function of SplitApplyCombine
.
If the argument is specified as a comma separated collection of vectors, then these are combined and passed along.
If the argument is a function and two end points, then the function is evaluated at 100 points between a
and b
.
This is useful for plotting when the data is more conveniently represented in terms of vectors, but the plotting interface requires the x and y values collected.
Examples:
using Plots
r(t) = [sin(t), cos(t)]
rp(t) = [cos(t), -sin(t)]
plot(unzip(r, 0, 2pi)...) # calls plot(xs, ys)
t0, t1 = pi/6, pi/4
p, v = r(t0), rp(t0)
plot!(unzip(p, p+v)...) # connect p to p+v with line
p, v = r(t1), rp(t1)
quiver!(unzip([p])..., quiver=unzip([v]))
Based on unzip
from the Plots
package. Implemented through invert
of SplitApplyCombine
Note: for a vector of points, xs
, each of length 2
, a similar functionality would be (first.(xs), last.(xs))
. If each point had length 3
, then with second(x)=x[2]
, a similar functionality would be (first.(xs), second.(xs), last.(xs))
.
```
CalculusWithJulia.uvec
— Methoduvec(x)
Helper to find a unit vector.
CalculusWithJulia.vectorfieldplot!
— Methodvectorfieldplot(F; [xlim=(-5,5)], [ylim=(-5,5)], [nx=8], [ny=8])
Create a vector field plot using a grid described by xlim
, ylim
with nx
and ny
grid points in each direction.
F(x,y) = [-y, x]
vectorfieldplot(F, xlim=(-4,4), ylim=(-4,4))
CalculusWithJulia.vectorfieldplot!
— Methodvectorfieldplot(F; [xlim=(-5,5)], [ylim=(-5,5)], [nx=8], [ny=8])
Create a vector field plot using a grid described by xlim
, ylim
with nx
and ny
grid points in each direction.
F(x,y) = [-y, x]
vectorfieldplot(F, xlim=(-4,4), ylim=(-4,4))
CalculusWithJulia.vectorfieldplot
— Methodvectorfieldplot(F; [xlim=(-5,5)], [ylim=(-5,5)], [nx=8], [ny=8])
Create a vector field plot using a grid described by xlim
, ylim
with nx
and ny
grid points in each direction.
F(x,y) = [-y, x]
vectorfieldplot(F, xlim=(-4,4), ylim=(-4,4))
CalculusWithJulia.vectorfieldplot3d!
— Methodvectorfieldplot3d(F; [xlim=(-5,5)], [ylim=(-5,5)], [nx=5], [ny=5])
Create a 3 dimensional vector field plot using a grid described by xlim
, ylim
, zlim
with nx
, ny
, and nz
grid points in each direction.
Note: the vectors are represented with line, not arrow due to no implementation of :quiver3d
.
F(x,y,z) = [-y, x,z]
vectorfieldplot3d(F, xlims=(-4,4), ylims=(-4,4), zlims=(0,3))
CalculusWithJulia.vectorfieldplot3d!
— Methodvectorfieldplot3d(F; [xlim=(-5,5)], [ylim=(-5,5)], [nx=5], [ny=5])
Create a 3 dimensional vector field plot using a grid described by xlim
, ylim
, zlim
with nx
, ny
, and nz
grid points in each direction.
Note: the vectors are represented with line, not arrow due to no implementation of :quiver3d
.
F(x,y,z) = [-y, x,z]
vectorfieldplot3d(F, xlims=(-4,4), ylims=(-4,4), zlims=(0,3))
CalculusWithJulia.vectorfieldplot3d
— Methodvectorfieldplot3d(F; [xlim=(-5,5)], [ylim=(-5,5)], [nx=5], [ny=5])
Create a 3 dimensional vector field plot using a grid described by xlim
, ylim
, zlim
with nx
, ny
, and nz
grid points in each direction.
Note: the vectors are represented with line, not arrow due to no implementation of :quiver3d
.
F(x,y,z) = [-y, x,z]
vectorfieldplot3d(F, xlims=(-4,4), ylims=(-4,4), zlims=(0,3))