function run()

%addpath C:\dynare\4.4.3\matlab
%addpath /Applications/Dynare/4.5.7/matlab
addpath C:\dynare\4.5.7\matlab

clear all
close all
clc

n = 0.5 ;
beta = 0.9915;
alpha = 0.35;
theta = 0.13;
%theta  = 0.06;
A = 1.168;
%mu = 1.5;
%mu = 0.1;
%mu = 0.04;
mu = 0.04;
%omega = 0.104;

%%FSolve block
%rho r w h b m c1 c2 s y k

 
P=[n, beta, alpha, theta, A, mu];

Xorig=x0input();
 
    options=optimset('Display','iter','Tolfun',1.0e-26,'MaxFunEvals',5000000 ,'MaxIter',50000);
    %   [Xout,F,exitflag]= fsolve(@calibration,Xorig,options,Pin,t);
    [Xout,Fval, exitflag]= fsolve(@calibration,Xorig,options,P);
 
 Xout
 
 
 xCell = num2cell(Xout);
[rho,...
 r,...
 w,...
 h,...
 b,...
 m,... 
 c1,... 
 c2,...
 s,...
 y,...
 k,...
 r_bond,...
 b_gov]=xCell{:}; 
 


clear Xout xCell Pin Xorig t  options exitflag Xout F

 %delete initial_values.mat;
 save initial_values.mat;
 
 

 
 end
 
%% Fsolve
 function F = calibration(X,P) 
 
 % Convert arrays to cell arrays
xCell = num2cell(X);
pCell = num2cell(P);

% Choose the parameters
[n, beta, alpha, theta, A, mu] = pCell{:};

 
[rho,...
 r,...
 w,...
 h,...
 b,...
 m,... 
 c1,... 
 c2,...
 s,...
 y,...
 k,...
 r_bond,...
 b_gov]=xCell{:}; 
% ,...
% h1,...
% h2,...
% w_sharp]=xCell{:}; 
 

F = [

	rho - 1/(1 + exp(h)) ;                                        							%death rate
	
	r - A * alpha * k^(alpha - 1) + 1; 														%saving rate
	
	w - A * k^alpha * (1 - alpha);															%wage rate
	
	h - mu * y * (1+n)/(1-rho) ;															%fiscal rule for government expenditure
	
	b_gov - b;
	
	h + (( 1 + r_bond)*b_gov/(1 - rho)) - b_gov * (1+n)/(1-rho) - theta * w * (1+n)/(1-rho);				%government budger constraint 
	
	m - (((1 + r) * k * (1+n) + (1+r_bond) * b)*rho)/(1+n);																%bequest
	
	c1 - (c2)/(beta * (1-rho) * (1 + r));														% Euler equation
	
	c1 - (c2)/(beta * (1-rho) * (1 + r_bond));
	
	c1  + k*(1+n) + b - w*(1-theta) - m;																%first budger constraint from util maximizing problem of an individual 
	
	c2 - (1 + r)*k*(1+n) - (1+r_bond)*b	;																		%second budger constraint from util maximizing problem of an individual 
	
	s - k*(1+n) - b ;																		%savings equation
	
	y - A*k^alpha ;																			%ooutput per capita
	
	%u_lifetime - log(c1) - beta*(1-rho)*log(c2);

];  
 end



function x0=x0input()


 
%x0= 0.5*ones(103,1);
 
x0 = [	%0.4845800923
		%2.773355339
		%0.3445107695
		%0.06169919639
		%0.01556228599
		%0.2102439373
		%0.3374951680
		%0.6508024389
		%0.1724731384
		%0.5300165685
		%0.1046072352
		%2.773355339
		%0.01556228599

	0.4710
    1.0893
    0.3154
    0.1163
    0.0000
    0.0800
    0.2325
    0.2547
    0.1219
    0.4852
    0.0813
    1.0893
    0.0000
	%0.4644				% rho 
    %0.6684					% r
    %0.3560				% w
    %0.1427				% h
    %-0.0415				% b
   %0.0676				% m
    %0.2465					% c1
    %0.2184			% c2
    %0.1309					% s
    %0.5477					% y
    %0.1149				% k
	%-2.1696				% lifetime u
	%0.6684					% r_bond
	%-0.0415					%b_gov
    ];  
 
end

                                                                             
 
        




