Using "for" and "fsolve"
1 次查看(过去 30 天)
显示 更早的评论
I am trying to solve a system of two equations - two unknowns for several locations; 14 total; I tried to use for command but I only get an answer for the first element; any idea how to get answers for all locations? My code is below:
.......
function F = countries(x)
global beta qsi tau rho gamma theta g A B
F = [(beta*(1-x(1)-x(2))^-1)-(1-gamma)*A*[(theta*((A*x(1)*(1-gamma)+B*(x(2)^qsi)*qsi*(1-rho*gamma))^-1))+((1-theta)*(A*x(1)*(1-gamma)+B*(x(2)^qsi)*qsi)^-1)];
(beta*(1-x(1)-x(2))^-1)-(B*(x(2)^(qsi-1))*qsi)*[((1-rho*gamma)*theta*((A*x(1)*(1-gamma)+B*(x(2)^qsi)*qsi*(1-rho*gamma))^-1))+((1-theta)*(A*x(1)*(1-gamma)+B*(x(2)^qsi)*qsi)^-1)]];
.........
global beta qsi tau rho gamma theta g A B
% x(1) ............. one for each country
% x(2) ............. one for each country
beta = 1.924;
qsi = 0.71;
theta = 0.01;
g = 0.2;
A = 1;
B = 0.4505;
%load data
dataset = 'dataApr11.txt';
load(dataset);
for i = 1:14
itau(i,1) = dataApr11(i,3);
irho(i,1) = dataApr11(i,4);
igamma(i,1) = dataApr11(i,2);
x0 = [0.2; 0.25]*ones(1,14);
options=optimset('Display','iter');
[x,fval] = fsolve(@countries,x0,options)
W(i,1) = x(1,i)
Y(i,1) = x(2,i)
end
0 个评论
采纳的回答
Andrei Bobrov
2011-5-3
can so
...
W = zeros(14,1);
Y = zeros(14,1);
x0 = [0.2; 0.25]*ones(1,14);
for i = 1:14
tau = dataApr11(i,3);
rho = dataApr11(i,4);
gamma = dataApr11(i,2);
[x,fval] = fsolve(@countries,x0,optimset('Display','iter'));
W(i) = x(1)
Y(i) = x(2)
end
...
0 个评论
更多回答(2 个)
Yoav Livneh
2011-5-3
Your initial guess x0 is the same for all iterations. I dont know what the "countries" function does, and how the other variables itau, irho and igamma are connected, but it looks like you're solving the same problem 14 times.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Particle & Nuclear Physics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!