Solving N non-linear equations using fsolve. How do I pass these equations into my function without typing them out individually?
Show older comments
Hello all,
I am currently working with the Eaton-Kortum Trade Model in MATLAB. In this model we have N countries, and wish to solve 2N + N^2 non-linear equations for equilibrium outcomes. I am working currently on an example with four countries, which means I will need to use fsolve to solve 24 equations. I understand that I could type all 24 equations individually, but what happens when we allow N to grow in the model (to better reflect what the world looks like)? If I wanted to consider trade between 10 countries I would have to type 120 equations seperatley! Luckily these equations take one of three forms.
N of the equations take the form: (gam.*((sum(Ti.*(dni.*(w(i).^(beta)).*(p(i))).^(1-beta)).^(-theta)).^(-1./theta))) - p(n);
N^2 of the equations take the form: Ti.*(gam.*dni.*(w(i).^(beta))*(p(i).^(1-beta))*(1./p(i))).^(theta) - (x(i));
N of the equations take the form: ((beta).*(sum(Ti.*(gam.*dni.*(w(i).^(beta))*(p(i).^(1-beta))*(1./p(i))).^(theta)*x(i)))) - (w(i).*Li)
Where our unknowns are w's, p's, and x's and everything else is given.
Is there a way for me to iteratively feed these equations into fsolve?
For example:
F(1) = (gam.*((sum(Ti.*(dni.*(w(i).^(beta)).*(p(i))).^(1-beta)).^(-theta)).^(-1./theta))) - p(1);
F(2) = (gam.*((sum(Ti.*(dni.*(w(i).^(beta)).*(p(i))).^(1-beta)).^(-theta)).^(-1./theta))) - p(2);
F(3) = (gam.*((sum(Ti.*(dni.*(w(i).^(beta)).*(p(i))).^(1-beta)).^(-theta)).^(-1./theta))) - p(3);
F(4) = (gam.*((sum(Ti.*(dni.*(w(i).^(beta)).*(p(i))).^(1-beta)).^(-theta)).^(-1./theta))) - p(4);
If there is not a way to do what I suggest how should I attempt to implement this?
15 Comments
darova
on 30 Jun 2020
What about for loop?
Ethan Goode
on 30 Jun 2020
Edited: Ethan Goode
on 30 Jun 2020
Walter Roberson
on 30 Jun 2020
Build a cell array of function handles.
fsolve(@(x) arrayfun(@(F)F(x), CellOfHandles), x0)
Ethan Goode
on 30 Jun 2020
Walter Roberson
on 30 Jun 2020
F(1) = (gam.*(((Ti.*(dni.*(x(21).^(beta))*(x(1).^(beta))).^(-theta)))...
+ ((Ti.*(dni.*(x(22).^(beta))*(x(2).^(beta))).^(-theta)))...
+ ((Ti.*(dni.*(x(23).^(beta))*(x(3).^(beta))).^(-theta)))...
+ ((Ti.*(dni.*(x(24).^(beta))*(x(4).^(beta))).^(-theta)))))-x(1);
F(2) = (gam.*(((Ti.*(dni.*(x(21).^(beta))*(x(1).^(beta))).^(-theta)))...
+ ((Ti.*(dni.*(x(22).^(beta))*(x(2).^(beta))).^(-theta)))...
+ ((Ti.*(dni.*(x(23).^(beta))*(x(3).^(beta))).^(-theta)))...
+ ((Ti.*(dni.*(x(24).^(beta))*(x(4).^(beta))).^(-theta)))))-x(2);
F(3) = (gam.*(((Ti.*(dni.*(x(21).^(beta))*(x(1).^(beta))).^(-theta)))...
+ ((Ti.*(dni.*(x(22).^(beta))*(x(2).^(beta))).^(-theta)))...
+ ((Ti.*(dni.*(x(23).^(beta))*(x(3).^(beta))).^(-theta)))...
+ ((Ti.*(dni.*(x(24).^(beta))*(x(4).^(beta))).^(-theta)))))-x(3);
F(4) = (gam.*(((Ti.*(dni.*(x(21).^(beta))*(x(1).^(beta))).^(-theta)))...
+ ((Ti.*(dni.*(x(22).^(beta))*(x(2).^(beta))).^(-theta)))...
+ ((Ti.*(dni.*(x(23).^(beta))*(x(3).^(beta))).^(-theta)))...
+ ((Ti.*(dni.*(x(24).^(beta))*(x(4).^(beta))).^(-theta)))))-x(4);
The only difference I can see between those four is what is being subtracted at the very end. I re-checked several times, but that is the only difference I can find. If I have not missed anything, then the only way those can be true is if x(1) and x(2) and x(3) and x(4) are all equal to each other. If that is what is desired, then just force them to be equal by substitution.
Walter Roberson
on 30 Jun 2020
Example:
K3 = 5;
for K1 = 21:24
for K2 = 1:4
F{K3} = @(x) Ti.*(gam.*dni.*(x(K1).^(beta))*(x(K2).^(1-beta))*(1./x(K2))).^(theta) - (x(K3));
K3 = K3 + 1;
end
end
for K = 1 : 4
F{20+K} = @(x) ((x(4*K+1).*x(21).*Li)+(x(4*K+2).*x(22).*Li)+(x(4*K+3).*x(23).*Li)+(x(4*K+4).*x(24).*Li))...
-(x(20+K).*Li);
end
solution = fsolve(@(x) arrayfun(@(f)f(x), F), x0);
Ethan Goode
on 1 Jul 2020
Ethan Goode
on 1 Jul 2020
Edited: Ethan Goode
on 1 Jul 2020
Walter Roberson
on 1 Jul 2020
Edited: Walter Roberson
on 1 Jul 2020
F(i,1) = {@(x) (gam.*(sum(Ti.*(dni.*((x(k).^(beta).*(x(j).^(1-beta)))).^(-theta)))))-x(i)};
That code is overwriting F(i,1) for each for j for k iteration, so the result will be n cell array in which only the last j and last k value "count". The wage equations have the same problem.
You could consider having the code generate a 3D cell array of only that kind of equation, and then reshape it into a vector at the end, and then
handles = [price_equations; wage_equations; tradeshare_equations];
sol = fsolve(@() arrayfun(@(F)F(x), handles), x0);
Ethan Goode
on 1 Jul 2020
Walter Roberson
on 1 Jul 2020
Edited: Walter Roberson
on 1 Jul 2020
function F = price_equations()
%%Put parameters here
Ti= 1;
Li= 1;
dni = 2;
theta = 4;
gam = 1;
beta = 0.5;
n = 4;
F = cell(n, n, n^2);
for i = 1:n
for j = 1:n
for k = 1 : n^2
F{i,j,k} = @(x) (gam.*(sum(Ti.*(dni.*((x(k+n).^(beta).*(x(j).^(1-beta)))).^(-theta)))))-x(i);
end
end
end
F = F(:);
Notice by the way that you do not pass x to this, as you are dynamically generating functions that will have some x passed to them.
Walter Roberson
on 1 Jul 2020
function F = wage_equations()
%%Put parameters here
Ti= 1;
Li= 1;
dni = 2;
theta = 4;
gam = 1;
beta = 0.5;
n = 4;
F = cell(n, n, n);
ibase = n + n^2;
kbase = n + n^2;
jbase = n;
for i = 1 : n
for k = 1 : n
for j = 1 : n
F{i, j, k} = @(x)(sum(x(j+jbase).*x(k+kbase).*Li))-(x(i+ibase).*Li);
end
end
end
F = F(:);
This is not 4 (n) equations, this is n^3 equations.. provided that you coded correctly earlier.
Celestine Siameh
on 21 Aug 2021
Please was this question answered, as I am solving a similar code but in my case is multi country and multi sector, armington model. The above as guided me but I want to know if the final code is correct. Thanks.
Walter Roberson
on 21 Aug 2021
It looks to me as if perhaps the poster deleted at least one of their comments, so I am not sure whether the code ended up working for them.
Celestine Siameh
on 21 Aug 2021
Thanks Walter. Let me check with him then. Hello Ethan Goode, did the code worked for you.
Answers (0)
Categories
Find more on Solver Outputs and Iterative Display in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!