I want to code the following BASIC program into GAMS. This program computes variable Z based on variable L and assumes that L in the last period is zero (T= 0,…,10). Reversely, it computes L variable up to the first time. Below please find my loop command but it doesn’t give me the correct loop answer.
Thanks for the help,
0 REM PROGRAM 1.1: MINE PROBLEM
20 DIM X(11},Y(11).Z(11).L(11}
30 L(10) = 0
40 FOR T = 9 TO 0 STEP -1
50 Z(T} = (1- L(T+1))
60 L(T) = L(T+1) + Z(T)^2
70 NEXT T
80 X(0) = 1000
90 FOR T = 0 TO 9
100 Y(T) = X(T} * Z(T)
110 X(T+1) = X(T) - Y(T}
120 NEXT T
130 LPRINT " T X(T} Y(T) L(T)"
140 LPRINT “------------------------------------------------”
150 LPRINT 0,X(0),Y(0)
160 FOR T = 1 TO 10
170 LPRINT T,X(T),Y(T),L(T}
180 NEXT T
190 END
Have you had a chance to look over the rules for this forum (see the Rules tab)? It’s good form to provide your name and affiliation. That increases the chance you’ll get a helpful answer.
In this case, divide and conquer is a good strategy. The first loop in the BASIC code computes L and Z, but it does so with a backwards loop. Your loop is forwards, so that won’t work. Also, you can just use parameters here, since you are doing computations. Some example code is below. If you run it in the IDE with gdx=lookAtMe and then view the lookAtMe.gdx file in the GDX browser of the IDE you can have a good look at not only the output L and Z but also the interesting sets here: tLast, revt, etc.
-Steve
$ontext
0 REM PROGRAM 1.1: MINE PROBLEM
20 DIM X(11},Y(11).Z(11).L(11}
30 L(10) = 0
40 FOR T = 9 TO 0 STEP -1
50 Z(T} = (1- L(T+1)) / 2
60 L(T) = L(T+1) + Z(T)^2
70 NEXT T
$offtext
set t / 0 * 10 /;
singleton set tLast(t);
tLast(t) = card(t) eq ord(t);
alias (t,rt);
set revt(t,rt) 'reverse t';
revt(t,t+[card(t)-2*ord(t)+1]) = yes;
display revt;
parameters L(t), Z(t);
L(t) = na;
L(tLast) = 0;
loop {revt(t,rt)$[not tLast(rt)],
Z(rt) = (1 - L(rt+1)) / 2;
L(rt) = L(rt+1) + sqr(Z(rt));
};
display L;