function [Pis] = pis(wis)
global Ais beta IOcoef Nsec Ncoun
IOmat_cs_s = nan(Ncoun*Nsec,Nsec);
IOmat_cs_s(:,t) = sum(IOcoef(:,((t-1)*Ncoun+1):Ncoun*t),2);
IOmat_cs = sum(IOmat_cs_s,2);
pimat = zeros(Nsec*Ncoun,1);
pimat(i) = ((beta(i)).^(-beta(i)) .* (1-beta(i)).^(-1+beta(i)))...
.*(wis(i).^beta(i) .* (IOmat_cs(i).*PMis(pij(pis(i)))).^(1-beta(i)))./Ais(i);
function [Pij] = pij(pis)
pmat = zeros(Nsec*Ncoun,Ncoun);
pmat(k,n) = pis(k) .* tau(k,n);
function [Fij] = fij(pis,wis,mij)
global alpha sigma Nsec Ncoun
idx = 1+(s-1)*Ncoun:1:Ncoun*s;
SG(idx,1) = ones(Ncoun,1)*sigma(s);
fmat = zeros(Nsec*Ncoun,Ncoun);
fmat(:,:) = alpha(i,j) .* (pij(pis(i,1))).^(-(SG*ones(1,Ncoun)))...
.*(PFis(pij(pis(i,1)))).^((SG*ones(1,Ncoun))-1) .* Yis(wis,pis,mij);
function [Mij] = mij(pis,mij)
global gama rho Nsec Ncoun
idx = 1+(s-1)*Ncoun:1:Ncoun*s;
SG(idx,1) = ones(Ncoun,1)*rho(s);
mmat = zeros(Nsec*Ncoun,Nsec*Ncoun);
mmat(i,j) = gama(i,j) .* (pij(pis(i,1))).^(-(SG*ones(1,Ncoun)))...
.* (PMis(pij(pis(i,1)))).^((SG*ones(1,Ncoun))-1) .* IMis(pis,mij);
function [Pfij] = pfij(pis,wis,mij)
global alpha sigma Nsec Ncoun
idx = 1+(s-1)*Ncoun:1:Ncoun*s;
SG(idx,1) = ones(Ncoun,1)*sigma(s);
pfmat = zeros(Nsec*Ncoun,Ncoun);
pfmat(:,:) = alpha(i,j) .* (pij(pis(i,1))).^(1-(SG*ones(1,Ncoun)))...
.*(PFis(pij(pis(i,1)))).^(1-(SG*ones(1,Ncoun))) .* Yis(wis,pis,mij);
function [Pmij] = pmij(pis,mij)
global gama rho Nsec Ncoun
idx = 1+(s-1)*Ncoun:1:Ncoun*s;
SG(idx,1) = ones(Ncoun,1)*rho(s);
pmmat = zeros(Nsec*Ncoun,Nsec*Ncoun);
pmmat(i,j) = gama(i,j) .* (pij(pis(i,1))).^(1-(SG*ones(1,Ncoun)))...
.* (PMis(pij(pis(i,1)))).^(1-(SG*ones(1,Ncoun))) .* IMis(pis,mij);
function syseqn = GEsol(x)
global Ais Lis beta IOmat_cs Ncoun
fij = x(:,Ncoun+1:end-2);
eq1 = pij(pis).*fij - pfij(pis,wis,mij);
eq2 = pij(pis).*mij - pmij(pis,wis,mij);
eq3 = pis - (beta.^(-beta).*(1-beta).^(-1+beta))...
.* (wis.^beta .* (IOmat_cs.*PMis(pij(pis))).^(1-beta))./Ais;
eq4 = Lis - (((sum(fij,2) + sum(mij,2)))./Ais) * ((beta.*PMis(pij(pis)))./...
((1-beta)*wis)).^(1-beta);
syseqn = [eq1, eq2, eq3, eq4];
global alpha gama sigma rho tau Ais Lis beta IOcoef IOmat_cs Nsec Ncoun
beta = 0.6*ones(Nsec*Ncoun,1);
Lis = 1*ones(Nsec*Ncoun,1);
Ais = 1*ones(Nsec*Ncoun,1);
tau = 1.5*ones(Nsec*Ncoun,Ncoun);
alpha = 0.6*ones(Nsec*Ncoun,Ncoun);
gama = 0.4*ones(Nsec*Ncoun,Ncoun);
IOcoef = importdata('IOcoef.txt');
IOmat_cs_s = nan(Ncoun*Nsec,Nsec);
IOmat_cs_s(:,t) = sum(IOcoef(:,((t-1)*Nsec+1):Nsec*t),2);
IOmat_cs = sum(IOmat_cs_s,2);
options = optimset('Display', 'iter', 'MaxFunEvals', 20000, 'MaxIter', 20000, 'TolFun', 1e-10, 'TolX', 1e-10);
x = fsolve('GEsol', x0, options);
fij = x(:,Ncoun+1:end-2);