Last active
March 23, 2017 22:03
-
-
Save YingboMa/e0d0eb5130c3ff6cf648f37801e5f94a to your computer and use it in GitHub Desktop.
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
using DifferentialEquations | |
using Optim | |
@public_abstract_type AbstractBVProblem{dType,bType,isinplace,F} <: DEProblem | |
type BVProblem{dType,bType,initType,F} <: AbstractBVProblem{dType,bType,F} | |
f::F | |
domin::dType | |
bc::bType | |
init::initType | |
end | |
function BVProblem(f,domin,bc,init=nothing) | |
BVProblem{eltype(domin),eltype(bc),eltype(init),typeof(f)}(f,domin,bc,init) | |
end | |
function f(t, y, du) | |
(x, v) = y | |
du[1] = v | |
du[2] = -x | |
end | |
function solve_(prob::BVProblem) | |
bc = prob.bc | |
u0 = [bc[1], zero(bc[1])] | |
probIt = ODEProblem(prob.f, u0, prob.domin) | |
function loss(minimizer) | |
probIt.u0[2] = minimizer[1] | |
sol = solve(probIt) | |
abs(sol[end][1]-bc[2]) | |
end | |
probIt.u0[2] = optimize(loss, [1.], BFGS()).minimizer[1] | |
solve(probIt) | |
end | |
bc = [0.,1.] | |
domin = (0.,100.) | |
init = rand() | |
bvp = BVProblem(f, domin, bc) | |
solve_(bvp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment