# Solving Boundary Value Differential Equation Problems for Economics

**URL:** https://discourse.julialang.org/t/solving-boundary-value-differential-equation-problems-for-economics/72871
**Category:** Finance and Economics
**Tags:** economics, differentialequation
**Created:** [December 10, 2021, 4:32am UTC](https://discourse.julialang.org/t/solving-boundary-value-differential-equation-problems-for-economics/72871 "2021-12-10T04:32:16Z")
**Posts on this page:** 1
**Showing post:** 17

<div class="post-metadata">

### Author: ![MainBatDealer](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@MainBatDealer](https://discourse.julialang.org/u/MainBatDealer)
#### Post date: [December 20, 2021, 3:30am UTC](https://discourse.julialang.org/t/solving-boundary-value-differential-equation-problems-for-economics/72871/17 "2021-12-20T03:30:49Z")

</div>

Just a quick update: `ApproxFun` works really well for this problem. Here’s the code I used to solve it. It’s very similar to @ptoche’s response using `DifferentialEquations`.

```julia
using ApproxFun
using Plots

maxt = 100 # Time periods

x=Fun(identity, 0..maxt) # Let x() be an empty Chebychev fit on the range
                         # [0, 100]

#Parameters
ρ = 0.10
δ = 0.10
α = 0.5
A = 0.4
σ = 0.5
p = [ρ, δ, α, A, σ]

Y(K) = A * K ^ α - δ * K # output 

#Steady-States
K_ss = ((δ + ρ) / (A * α)) ^ (1 / (α - 1)) 
C_ss = (A * K_ss ^ α - δ * K_ss)

#Starting from above steady-state
ramseyhigh = (K,C) -> [K' - (A * K^α - δ * K - C );
                        C' + C * σ * (ρ + δ - α * A * K ^ (α - 1));
                        K(0) - K_ss * 10.0;
                        K(maxt) - K_ss * 1.00001]

#Starting from below steady-state
ramseylow = (K,C) -> [K' - (A * K^α - δ * K - C );
                        C' + C * σ * (ρ + δ - α * A * K ^ (α - 1));
                        K(0) - K_ss * 0.01;
                        K(maxt) - K_ss * 0.99999]              

# Choosing a starting function for capital
# and consumption.
# The approximation would probably be 
# faster If I had a better starting function.
K10 = one(x) * K_ss
C10 = one(x) * C_ss

# Run the function approximation
Kh,Ch = newton(ramseyhigh, [K10,C10], maxiterations=50, tolerance = tolerance=1E-10)
Kl, Cl = newton(ramseylow, [K10,C10], maxiterations=50, tolerance = tolerance=1E-10)

#Recreating the plot from ptoche's post:

plt = plot(title="Policy Function in Ramsey-Cass-Koopmans Model", xlims=(0,2), ylims=(0,0.6), xlabel="K(t)", ylabel="C(t)", legend=false)
plot!(plt, x -> Y(x), 0, 2, color="black", linewidth=3, legend=false)
vline!(plt, [K_ss], color="black", linewidth=3, legend=false)
plot!(plt, Kl, Cl, xlims=(0,2), ylims=(0,0.6), color=:blue, linewidth=2)
scatter!((K_ss,C_ss), label=["Stationary State" "."], color=:red)
plot!(plt, Kh, Ch, xlims=(0,2), ylims=(0,0.6), color=:blue, linewidth=2)

savefig(plt, "ramsey-approx_fun.png")

```

![ramsey-approx_fun](https://global.discourse-cdn.com/julialang/original/3X/6/e/6e08262ba259bd7f25771950274f3dd9d2a770a5.png)

---

_[View the full topic](https://discourse.julialang.org/t/solving-boundary-value-differential-equation-problems-for-economics/72871)._
