Borrar filtros
Borrar filtros

Using "for" and "fsolve"

2 visualizaciones (últimos 30 días)
Asdrubal
Asdrubal el 2 de Mayo de 2011
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

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 3 de Mayo de 2011
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
...

Más respuestas (2)

Yoav Livneh
Yoav Livneh el 3 de Mayo de 2011
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 el 3 de Mayo de 2011
Yes, I am trying to solve the problem 14 times for different values each time. Andrei's suggestion worked. thanks

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by