After I had some problems in the beginning, I sort of gave up. Now I want to give it another try and it would be really nice to get to the solution of the problem this time.
I have a basic root-finding problem. It is an economics problem and broadly speaking I want to know what “optimal” consumption is depending on two state variables, capital and productivity.
Here is the code:
# This example: Neoclassical Growth Model using QuantEcon # Parameters global γ = 2; # Risk aversion global α = 0.3; # Capital share global β = 0.98; # Discount factor # First, create a grid using the QuantEcon toolbox K_grid = collect(linspace(0.1, 10, 5)) z_grid = collect(linspace(0.9, 1.1, 3)) p_grid = [1/3 1/3 1/3] # iid shocks and they are equally probable grid=gridmake(K_grid,z_grid) # First, Rootfinding and linear policy function for c. using NLsolve f! = function(α_N,real_res,grid) # we want to find roots for f global γ,α,β K_next = grid[:,2].*grid[:,1].^α - (grid*α_N) next_grid = gridmake(K_next,z_grid) c_next = (next_grid*α_N) RHS = β*reshape(c_next.^(-γ).*α.*(next_grid[:,1]).^(α-1).*next_grid[:,2],15,3)*p_grid' res = (grid*α_N).^(-γ) - RHS real_res = grid'*res end g!(α_N,real_res) = f!(α_N,real_res,grid) guess = [0.01, 0.1] res = nlsolve(g!,guess) g!(guess,[0,0])
The output from the second to last line is:
nlsolve claims that the residual f(x) is already very small and convergence has been achieved. However, if I execute
g!(guess,[0,0]), I get the following:
So the residual is by no means close to zero.
Does anyone know how I can go about it?