I’m using DifferentialEquations.jl to solve a standard consumption-savings problem.
I wanna see if there is a way to solve these issues before I submit feature-requests.
using DifferentialEquations, BoundaryValueDiffEq, Plots
function DE!(du,u,p,t)
c, B = u
r,ρ,γ,y,B0,BT = p
du[1] = dc = ((r-ρ)/(γ))*(c)
du[2] = dB = r*B + y - c
end
function bc1!(residual, u, p, t)
r,ρ,γ,y,B0,BT = p
residual[1] = u[1][2] - B0
residual[2] = u[end][2] - BT
end
tspan = (0.0,60.0);
u0=[10, 10]; #initial guess. NOT boundary @ t=0. constant.
function Par(;r=0.03, ρ=0.02, γ=1.0, y=10.0, B0=5.0, BT=0)
(r,ρ,γ,y,B0,BT)
end
p1 =Par(r=0.01);
bvp1 = BVProblem(DE!, bc1!, u0, tspan, p1);
@time sol1 = solve(bvp1, GeneralMIRK4(), dt=0.5);
plot(sol1, lab=["consumption" "wealth"])
The code above works fine.
1 Can I transmit derivatives du
the same way as variables u
& parameters p
?
function DE!(du,u,p,t)
dc, dB = du #does not work
c, B = u
r,ρ,γ,y,B0,BT = p
dc = ((r-ρ)/(γ))*(c)
dB = r*B + y - c
end
2 In the boundary conditions function bc1!(residual, u, p, t)
I’m unable to transmit u
function bc1!(residual, u, p, t)
c, B = u #does not work!
r,ρ,γ,y,B0,BT = p
residual[1] = B[1] - B0 # Initial Condition
residual[2] = B[end] - BT # Terminal Condition
end
3 How do I include non-constant guesses in BVProblem(DE!, bc1!, u0, tspan, p1)
?
Currently
u0=[10, 10]; #Constant guess for c[t] and B[t]
bvp1 = BVProblem(DE!, bc1!, u0, tspan, p1);