Ramsey and the steady state

Dear Professor Pfeifer and Dynare users,
I would like to compute the optimal ramsey policy of a planner choosing the instrument tau. I provide the initial condition for tau and I compute the steady state of the model conditional on tau, using the function find_climateram_ss, which in turn calls the function find_steady_final. I get this error:

Error using trustnleqn (line 28)
Objective function is returning undefined values at initial point. FSOLVE cannot continue.

If I run the model without the Ramsey options by setting RAM=0 on line 4 (i.e. just doing stoch_simul), I can find the steady state and everything goes smoothly. Any idea on what is happening? You can find attached the mod file and the two functions needed to compute the steady state.

Thank you so much for your time.

Valerio

climate_ramsey.mod (12.2 KB)
find_climateram_ss.m (1.5 KB)
find_steady_final.m (3.3 KB)

You need proper error handling around fsolve:

options = optimoptions('fsolve','MaxFunEvals',300000,'MaxIter',30000,'TolFun',1e-15,'Display','off');
try
    [X,~,EXITFLAG] = fsolve(@(XX) find_steady_final(XX,gamma,gammaz,eta,beta,betaz,alpha1,alpha2,alpha2bar,alpha1z,alpha2z,alpha2barz,kappaG,kappaGz,GAM,n,delta,iota,zeta,csi,kappaA,mu,muz,nu,tau,tauz,A,r,rz,v,g,gz,Az,psiG,psiB,kappaB,kappaBz,rho,deltax),X0,options);
    if EXITFLAG<1
        error_flag=1;
        X=X0;
    end
catch
    error_flag=1;
    X=X0;
end

and then return the error_flag as info:

steady_state_model;
... 
[info,yH, pH, pB, pBz, yFz, rkB, rkB_zd, rkB_zu, rkB_zz, rkG, rkG_zd, rkG_zu, rkG_zz] =find_climateram_ss(gamma,gammaz,eta,beta,betaz,alpha1,alpha2,alpha2bar,alpha1z,alpha2z,alpha2barz,kappaG,kappaGz,GAM,n,delta,iota,zeta,csi,kappaA,mu,muz,nu,tau,tauz,A,r,rz,v,g,gz,Az,psiG,psiB,kappaB,kappaBz,rho,deltax,yHss,pHss,pBss,pBzss,prBss,prBzss,prGss,prGzss,yFzss);

climate_ramsey.mod (12.2 KB)
find_climateram_ss.m (1.6 KB)

Thank you so much for your help.