Hi ;

how to implement for loop in AMPL ?

can we implement the equation below

for j=1:m

var fx {i in 0…n, j in 0…m} =0.5+0.25*(x[i,j]*U[i,j]+y[i,j]*U2[i,j]+z[i,j]*U3[i,j])-x[i,j];

end

Kind regards

Hi ;

how to implement for loop in AMPL ?

can we implement the equation below

for j=1:m

var fx {i in 0…n, j in 0…m} =0.5+0.25*(x[i,j]*U[i,j]+y[i,j]*U2[i,j]+z[i,j]*U3[i,j])-x[i,j];

end

Kind regards

Hi,

Bidimensional parameters can be defined without using for loops. For example:

```
param fx {i in 0..n, j in 0..m} default 0.5+0.25*x[i,j];
```

You need to declare “n”, “m”, and “x” previously. You could use “:=” instead of “default” in case you are not going to modify the value of fx. You can also define more complex formulas:

```
param fx {i in 0..n} = if i = 0 then 0 else fx[i-1] + i;
```

Equivalent to:

```
param fx {i in 0..n} = sum{j in 0..i} j;
```

I would not recommend using imperative paradigm to assign values to parameters, as they are more lines of code and might be less efficient. However, this would be a way using “for” and “let” inside a script:

```
param fx {i in 0..n, j in 0..m};
for{i in 0..n, j in 0..m} {
let fx[i,j] := 0.5+0.25*x[i,j];
}
```

It is not clear what you wanted to do in your example (j index is used in the loop and also in the variable subscript). Hope this helps!

You do not say whether x, y, z, U, U2, and U3 are variables (defined in AMPL var statements) or parameters (defined in param statements). If any of them are variables, then you are trying to define the variable fx[i,j] by an expression involving other variables. In that case, you must use an = in the var statement:

```
var fx {i in 0..n, j in 0..m} =
0.5+0.25*(x[i,j]*U[i,j]+y[i,j]*U2[i,j]+z[i,j]*U3[i,j])-x[i,j];
```

If you only want to define fx for j from 1 to m, you can write `j in 1..m`

in the var statement instead of `j in 0..m`

. Or, as Marcos mentions, you can give a different expression for the case where j equals 0; for example:

```
var fx {i in 0..n, j in 0..m} =
if j = 0 then 0 else
0.5+0.25*(x[i,j]*U[i,j]+y[i,j]*U2[i,j]+z[i,j]*U3[i,j])-x[i,j];
```