Array indices must be positive integers or logical values
Show older comments
'Deleted'
1 Comment
Original question by James Carter retrieved from Google Cache:
Array indices must be positive integers or logical values
I know that when you create this sort of loop your arrays have to be positive integers, however in this assignement we have to loop for values that are not integers such as 0. The important part of this loop is to allow for theta and alpha to have different values between and including 0 and 1. One of the main points is for theta to be 1 in order for X3d to get the lowest vaue equals to e3 = 2 and for theta to be 0/eps in order for X3u to become the highest possible. Any help for how to fix the code and enable the loop are highly appreciated.
%% Step 1: define all variables
e1 = 1;
e2 = 3;
e3 = 2;
Prob = 0.5;
Ru = 1.1;
Rd = 0.90;
gamma = 1.5;
eps = 10^-10
X1 = e1;
X2 = linspace(e2,e1*Ru+e2,100);
theta = linspace(eps,1,100);
alpha = linspace(0,1,100);
v2_MAX = -10^20;
IVector = linspace(1,100,100);
KVector = linspace(eps,1,100);
JVector = linspace(0,1,100);
%% Step 2: Create the loop for obtaining value function in the period 2
for i = IVector
for k = KVector
con(2,k) = theta(k)*X2(i);
for j = JVector
X3u = max(X2(i)*(1-theta(k))*(alpha(j)*Ru+(1-alpha(j)))+e3);
X3d = min(X2(i)*(1-theta(k))*(alpha(j)*Rd+(1-alpha(j)))+e3);
u3u = (X3u^(1-gamma))/(1-gamma);
u3d = (X3d^(1-gamma))/(1-gamma);
u2 = (con(2,k)^(1-gamma))/(1-gamma);
v2 = u2 + (Prob*u3u+(1-Prob)*u3d);
if v2 > v2_MAX
v2_MAX = v2;
alpha(2,i) = alpha(j);
theta(2,i) = theta(k);
end
end
end
end
Answers (1)
for i = 1:100
for k = 1:100
Theta=theta(1,k);
con(2,k) = Theta*X2(i);
for j = 1:100
Alpha=alpha(1,j);
X3u = max(X2(i)*(1-Theta)*(Alpha*Ru+(1-Alpha))+e3);
X3d = min(X2(i)*(1-Theta)*(Alpha*Rd+(1-Alpha))+e3);
...
Categories
Find more on Loops and Conditional Statements 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!