필터 지우기
필터 지우기

Using "for" and "fsolve"

조회 수: 2 (최근 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 CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by