This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 26
Add Muller's method #139
Open
fgittins
wants to merge
6
commits into
SciML:main
Choose a base branch
from
fgittins:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Muller's method #139
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
29d63c5
Added Muller's method
fgittins 5ddd166
Changed name of algorithm to SimpleMuller
fgittins bad6a77
Fixed tests using Floats
fgittins 873a6d8
Make docstring more detailed
fgittins 5d5f6ee
Add check for three guesses
fgittins 9bfaee4
Fix type-instability
fgittins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
""" | ||
Muller() | ||
|
||
Muller's method. | ||
""" | ||
struct Muller <: AbstractSimpleNonlinearSolveAlgorithm end | ||
fgittins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
function SciMLBase.solve(prob::NonlinearProblem, alg::Muller, args...; | ||
abstol = nothing, maxiters = 1000, kwargs...) | ||
@assert !isinplace(prob) "`Muller` only supports OOP problems." | ||
xᵢ₋₂, xᵢ₋₁, xᵢ = prob.u0 | ||
@assert xᵢ₋₂ ≠ xᵢ₋₁ ≠ xᵢ ≠ xᵢ₋₂ | ||
avik-pal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
f = Base.Fix2(prob.f, prob.p) | ||
fxᵢ₋₂, fxᵢ₋₁, fxᵢ = f(xᵢ₋₂), f(xᵢ₋₁), f(xᵢ) | ||
|
||
abstol = __get_tolerance(nothing, abstol, | ||
promote_type(eltype(fxᵢ₋₂), eltype(xᵢ₋₂))) | ||
|
||
for _ ∈ 1:maxiters | ||
q = (xᵢ - xᵢ₋₁)/(xᵢ₋₁ - xᵢ₋₂) | ||
A = q*fxᵢ - q*(1 + q)*fxᵢ₋₁ + q^2*fxᵢ₋₂ | ||
B = (2*q + 1)*fxᵢ - (1 + q)^2*fxᵢ₋₁ + q^2*fxᵢ₋₂ | ||
C = (1 + q)*fxᵢ | ||
|
||
denom₊ = B + √(B^2 - 4*A*C) | ||
denom₋ = B - √(B^2 - 4*A*C) | ||
|
||
if abs(denom₊) ≥ abs(denom₋) | ||
xᵢ₊₁ = xᵢ - (xᵢ - xᵢ₋₁)*2*C/denom₊ | ||
else | ||
xᵢ₊₁ = xᵢ - (xᵢ - xᵢ₋₁)*2*C/denom₋ | ||
end | ||
|
||
fxᵢ₊₁ = f(xᵢ₊₁) | ||
|
||
# Termination Check | ||
if abstol ≥ abs(fxᵢ₊₁) | ||
return build_solution(prob, alg, xᵢ₊₁, fxᵢ₊₁; | ||
retcode = ReturnCode.Success) | ||
fgittins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
xᵢ₋₂, xᵢ₋₁, xᵢ = xᵢ₋₁, xᵢ, xᵢ₊₁ | ||
fxᵢ₋₂, fxᵢ₋₁, fxᵢ = fxᵢ₋₁, fxᵢ, fxᵢ₊₁ | ||
end | ||
|
||
return build_solution(prob, alg, xᵢ₊₁, fxᵢ₊₁; | ||
retcode = ReturnCode.MaxIters) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
@testitem "Muller" begin | ||
@testset "Quadratic function" begin | ||
f(u, p) = u^2 - p | ||
|
||
u0 = (10, 20, 30) | ||
p = 612 | ||
prob = NonlinearProblem{false}(f, u0, p) | ||
sol = solve(prob, Muller()) | ||
|
||
@test sol.u ≈ √612 | ||
|
||
u0 = (-10, -20, -30) | ||
prob = NonlinearProblem{false}(f, u0, p) | ||
sol = solve(prob, Muller()) | ||
|
||
@test sol.u ≈ -√612 | ||
end | ||
|
||
@testset "Sine function" begin | ||
f(u, p) = sin(u) | ||
|
||
u0 = (1, 2, 3) | ||
prob = NonlinearProblem{false}(f, u0) | ||
sol = solve(prob, Muller()) | ||
|
||
@test sol.u ≈ π | ||
|
||
u0 = (2, 4, 6) | ||
prob = NonlinearProblem{false}(f, u0) | ||
sol = solve(prob, Muller()) | ||
|
||
@test sol.u ≈ 2*π | ||
end | ||
|
||
@testset "Exponential-sine function" begin | ||
f(u, p) = exp(-u)*sin(u) | ||
|
||
u0 = (-2, -3, -4) | ||
prob = NonlinearProblem{false}(f, u0) | ||
sol = solve(prob, Muller()) | ||
|
||
@test sol.u ≈ -π | ||
|
||
u0 = (-1, 0, 1/2) | ||
prob = NonlinearProblem{false}(f, u0) | ||
sol = solve(prob, Muller()) | ||
|
||
@test sol.u ≈ 0 | ||
|
||
u0 = (-1, 0, 1) | ||
prob = NonlinearProblem{false}(f, u0) | ||
sol = solve(prob, Muller()) | ||
|
||
@test sol.u ≈ π | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make the documentation a bit more detailed. References, restrictions like only scalars and such