Hello I have this model script.

```
set INPUT_ROW;
set INPUT_TRIANGLES;
set DIM = 1..3;
param data_points {INPUT_ROW, DIM};
param bary_centrics {INPUT_ROW, DIM};
param triangle_points {INPUT_TRIANGLES, DIM};
var move_points {INPUT_ROW, DIM};
minimize energy: sum{i in INPUT_ROW}
(bary_centrics[i, 1] * move_points[i, 1] +
bary_centrics[i, 2] * triangle_points[i * 2 - 1, 1] +
bary_centrics[i, 3] * triangle_points[i * 2, 1] - data_points[i, 1]) *
(bary_centrics[i, 1] * move_points[i, 1] +
bary_centrics[i, 2] * triangle_points[i * 2 - 1, 1] +
bary_centrics[i, 3] * triangle_points[i * 2, 1] - data_points[i, 1]) ;
```

This script works well.

However when I add one term at the end of the minimize function like this:

```
set INPUT_ROW;
set INPUT_TRIANGLES;
set DIM = 1..3;
param data_points {INPUT_ROW, DIM};
param bary_centrics {INPUT_ROW, DIM};
param triangle_points {INPUT_TRIANGLES, DIM};
var move_points {INPUT_ROW, DIM};
minimize energy: sum{i in INPUT_ROW}
(bary_centrics[i, 1] * move_points[i, 1] +
bary_centrics[i, 2] * triangle_points[i * 2 - 1, 1] +
bary_centrics[i, 3] * triangle_points[i * 2, 1] - data_points[i, 1]) *
(bary_centrics[i, 1] * move_points[i, 1] +
bary_centrics[i, 2] * triangle_points[i * 2 - 1, 1] +
bary_centrics[i, 3] * triangle_points[i * 2, 1] - data_points[i, 1]) + bary_centrics[i, 1];
```

I got this error.

```
i is undefined
```

I don’t know what causes this error. My actual script should be like this:

```
set INPUT_ROW;
set INPUT_TRIANGLES;
set DIM = 1..3;
param data_points {INPUT_ROW, DIM};
param bary_centrics {INPUT_ROW, DIM};
param triangle_points {INPUT_TRIANGLES, DIM};
var move_points {INPUT_ROW, DIM};
minimize energy: sum{i in INPUT_ROW}
(bary_centrics[i, 1] * move_points[i, 1] +
bary_centrics[i, 2] * triangle_points[i * 2 - 1, 1] +
bary_centrics[i, 3] * triangle_points[i * 2, 1] - data_points[i, 1]) *
(bary_centrics[i, 1] * move_points[i, 1] +
bary_centrics[i, 2] * triangle_points[i * 2 - 1, 1] +
bary_centrics[i, 3] * triangle_points[i * 2, 1] - data_points[i, 1]) +
(bary_centrics[i, 1] * move_points[i, 2] +
bary_centrics[i, 2] * triangle_points[i * 2 - 1, 2] +
bary_centrics[i, 3] * triangle_points[i * 2, 2] - data_points[i, 2]) *
(bary_centrics[i, 1] * move_points[i, 2] +
bary_centrics[i, 2] * triangle_points[i * 2 - 1, 2] +
bary_centrics[i, 3] * triangle_points[i * 2, 2] - data_points[i, 2]) +
(bary_centrics[i, 1] * move_points[i, 3] +
bary_centrics[i, 2] * triangle_points[i * 2 - 1, 3] +
bary_centrics[i, 3] * triangle_points[i * 2, 3] - data_points[i, 3]) *
(bary_centrics[i, 1] * move_points[i, 1] +
bary_centrics[i, 2] * triangle_points[i * 2 - 1, 3] +
bary_centrics[i, 3] * triangle_points[i * 2, 3] - data_points[i, 3]);
```

Is it because my objective function is too long? If so, is there a way to make a function and use it in the objective function?

My objective function is getting one vertex position with barycentric coordinates and 3 points and then minimizing the length of the two vertices between the evaluated vertex position and the data points. It’s just a simple linear least squares problem. Anyone know how to write this easily? I think it would be better to use a function that can be used in the objective function.

I changed the minimize function like this, and it works. I don’t know why it works. Can anyone let me know the reason? :

```
minimize energy: sum{i in INPUT_ROW}
(bary_centrics[i, 1] * move_points[i, 1] +
bary_centrics[i, 2] * triangle_points[i * 2 - 1, 1] +
bary_centrics[i, 3] * triangle_points[i * 2, 1] - data_points[i, 1]) *
(bary_centrics[i, 1] * move_points[i, 1] +
bary_centrics[i, 2] * triangle_points[i * 2 - 1, 1] +
bary_centrics[i, 3] * triangle_points[i * 2, 1] - data_points[i, 1]) +
sum{i in INPUT_ROW}
(bary_centrics[i, 1] * move_points[i, 2] +
bary_centrics[i, 2] * triangle_points[i * 2 - 1, 2] +
bary_centrics[i, 3] * triangle_points[i * 2, 2] - data_points[i, 2]) *
(bary_centrics[i, 1] * move_points[i, 2] +
bary_centrics[i, 2] * triangle_points[i * 2 - 1, 2] +
bary_centrics[i, 3] * triangle_points[i * 2, 2] - data_points[i, 2]) +
sum{i in INPUT_ROW}
(bary_centrics[i, 1] * move_points[i, 3] +
bary_centrics[i, 2] * triangle_points[i * 2 - 1, 3] +
bary_centrics[i, 3] * triangle_points[i * 2, 3] - data_points[i, 3]) *
(bary_centrics[i, 1] * move_points[i, 1] +
bary_centrics[i, 2] * triangle_points[i * 2 - 1, 3] +
bary_centrics[i, 3] * triangle_points[i * 2, 3] - data_points[i, 3]);
```