Skip to content

Commit

Permalink
Merge pull request #68 from BeastyBlacksmith/bbs/adapt-endpoints
Browse files Browse the repository at this point in the history
Use adaptive_grid on endpoints
  • Loading branch information
mkborregaard authored Oct 8, 2019
2 parents 1e75b51 + 9193fa7 commit 53bc5c6
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.jl.cov
*.jl.*.cov
*.jl.mem
Manifest.toml
20 changes: 20 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name = "PlotUtils"
uuid = "995b91a9-d308-5afd-9ec6-746e21dbc043"
version = "0.6.0"

[deps]
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"

[compat]
julia = "≥ 0.7.0"

[extras]
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "Plots"]
11 changes: 5 additions & 6 deletions src/adapted_grid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ Computes a grid `x` on the interval [minmax[1], minmax[2]] so that
`plot(f, x)` gives a smooth "nice" plot.
The method used is to create an initial uniform grid (21 points) and refine intervals
where the second derivative is approximated to be large. When an interval
becomes "straight enough" it is no longer divided. Functions are never evaluated
exactly at the end points of the intervals.
becomes "straight enough" it is no longer divided. Functions are evaluated at the end points of the intervals.
The parameter `max_recusions` computes how many times each interval is allowed to
be refined.
Expand Down Expand Up @@ -39,8 +38,8 @@ function adapted_grid(f, minmax::Tuple{Real, Real}; max_recursions = 7)

n_tot_refinements = zeros(Int, n_intervals)

# We only evaluate the function on interior points
fs = [NaN; [f(x) for x in xs[2:end-1]]; NaN]
# We evaluate the function on the whole interval
fs = f.(xs)
while true
curvatures = zeros(n_intervals)
active = falses(n_intervals)
Expand All @@ -52,7 +51,7 @@ function adapted_grid(f, minmax::Tuple{Real, Real}; max_recursions = 7)
f_range = one(f_range)
end
# Skip first and last interval
for interval in 2:n_intervals-1
for interval in 1:n_intervals
p = 2 * interval
if n_tot_refinements[interval] >= max_recursions
# Skip intervals that have been refined too much
Expand Down Expand Up @@ -139,5 +138,5 @@ function adapted_grid(f, minmax::Tuple{Real, Real}; max_recursions = 7)
n_intervals = n_points ÷ 2
end

return xs[2:end-1]
return xs, fs
end
33 changes: 33 additions & 0 deletions test/adaptive_test_functions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This is not run by runtests.jl. Only intended to be checked manually.
using Plots
const test_funcs = [
(x -> 2, 0, 1),
(x-> x, -1, 1),
(x-> 5x, -1, 1),
(x-> 1/x, 0, 1),
(x-> 1/x, -1, 1),
(x->tan(x), 0, 4),
(x-> -1/x, 0, 1),
(x-> 1/abs(x),-1,1),
(x-> 1e6x, -1, 1),
(x-> 1e50x, -1, 1),
(x-> log(1+sin(cos(x))), -6, 6),
(x-> sin(x^3)+cos(x^3), 0, 6.28),
(x-> sin(x), -5, 200),
(x-> sin(1/x), -2, 2),
(x-> sin(x^4), -4, 4),
(x-> sin(300x), -4, 4),
(x-> 1 + x^2 + 0.0125log(abs(1-3(x-1))), -2, 2),
(x-> sin(exp(x)), -6, 6),
(x-> 1/sin(x), -10, 10),
(x-> sin(x)/x, -6, 6),
(x-> tan(x^3-x+1) + 1/(x+3exp(x)), -2, 2),
]
##
plots = [plot(func...) for func in test_funcs]
pitchfork = plot(x->sqrt(x),0,10)
plot!(pitchfork, x->-sqrt(x),0,10)
plot!(pitchfork, x->0,-10,0)
plot!(pitchfork, x->0,0,10, linestyle=:dash)
push!(plots, pitchfork)
display.(plots)

2 comments on commit 53bc5c6

@mkborregaard
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/4170

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.0 -m "<description of version>" 53bc5c6f28dbf5ff417ac5222d532f54ec46568d
git push origin v0.6.0

Please sign in to comment.