Using "for" and "fsolve"

1 回表示 (過去 30 日間)
Asdrubal
Asdrubal 2011 年 5 月 2 日
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

採用された回答

Andrei Bobrov
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
...

その他の回答 (2 件)

Yoav Livneh
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.

Asdrubal
Asdrubal 2011 年 5 月 3 日
Yes, I am trying to solve the problem 14 times for different values each time. Andrei's suggestion worked. thanks

カテゴリ

Help Center および File ExchangeParticle & Nuclear Physics についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by