How can I name “a[[i]]” the parts of Table “a”? Or how to make Table “a” grow inside FindMinimum?

Let's say I want to minimize a function that uses a Table named a with the Conjugate Gradient Method of FindMinimum.

Instinctively I do the following :

FindMinimum[(Sum[a[[i]]*Cos[1.3], {i, 1, 2}])^2, {a, {0, 0}}, Method -> "ConjugateGradient"]

But I obviously get the following messages and results :

During evaluation of In:= Part::partd: Part specification a[] is longer than depth of object. >>
During evaluation of In:= Part::partd: Part specification a[] is longer than depth of object. >>

{0., {a -> {0., 0.}}}

So, to overcome this little difficulty, I tried the following :

a = {a1, a2};
FindMinimum[(Sum[a[[i]]*Cos[1.3], {i, 1, 2}])^2, {a, {0, 0}}, Method -> "ConjugateGradient"]

During evaluation of In:= Part::partd: Part specification a[] is longer than depth of object. >>
During evaluation of In:= Part::partd: Part specification a[] is longer than depth of object. >>

{0., {{a1, a2} -> {0., 0.}}}

and again I obtain the same error messages. So I came down to the dirty manner:

FindMinimum[(a1*Cos[1.3] + a2*Cos[1.3])^2, {{a1, 2}, {a2, 0}},

which this time provided me with actual results :

{2.86222*10^-17, {a1 -> 1., a2 -> -1.}}

Now, while this last alternative works for this simple example and for a Table containing only two values, It will become impossible if I had to do for example:

FindMinimum[(Sum[a[[i]]*Cos[1.3], {i, 1, 1000}])^2, {a, {0, 0}},

How can I sort this out? I'm aware that there might something wrong with my approach, but I can't find another manner to set this up.

• Note that you did get correct results in all three attempts. (The constant Cos[1.3] can be factored out of your example, showing that any set of numbers summing to zero is a valid solution.) – whuber Mar 26 '13 at 22:29
• @whuber oops... you're right! In fact even with the warnings and stuff, it works! Just tested it with some other toy models and it gives the right solutions... Only problem is that it takes unexpectedly long time to converge... – jrojasqu Mar 26 '13 at 23:31

A solution (maybe to specific) to your example is to consider Sum[a[[i]]*Cos[1.3], {i, 1, 2}]
as the dot product a . {Cos[1.3],Cos[1.3]}.

Then the problem becomes :

FindMinimum[(a . {Cos[1.3], Cos[1.3]}) ^2, {a, {2, 0}},Method -> "ConjugateGradient"]

which is a syntax Mathematica accepts. It gives :

{1.,-1.}

Of course, it can be extended to more variables

• (+1) that's the cleanest solution (I think Mathematica used to be unable to do this, but it got cleverer...) – Jens Mar 26 '13 at 22:43

This kind of question gets asked very often. You can do this:

up = 5;
varlst = {a@#, 0.1} & /@ Range[up];
FindMinimum[(Total@Table[a[i]*Cos[1.3], {i, 1, up}])^2, varlst, does the job.

The reason your attempt does not work is that, if a is not defined, a[] attempts to take the 4th part of the symbol a; but a has length 1, and its only part is a[] which is Symbol, its head... So I replaced a[[i]] by a[i], which creates an expression with head a and i on the branch (the important point is that a is a perfectly normal variable, for purposes of this discussion).

I also replaced Sum by Total@Table[] because Mathematica tries to do clever things with Sum, while Total@Table[] just constructs the list and adds the terms.

The varlst = {a@#, 0.1} & /@ Range[up]; could also have been written varlst=Table[{a[i], 0.1}, {i, 1, up}]; it just constructs the last argument of FindRoot before the options.

• Actually a[] is not a, it's Symbol, which is really the Head of a – RunnyKine Jul 18 '14 at 3:24
• @RunnyKine right, fixed – acl Jul 18 '14 at 8:41

Another way to get your code to work is this:

a = Array["a", 2];

Clear[a, c];
a = Array[c, 2];
FindMinimum[(Sum[Evaluate@a[[i]]*Cos[1.3], {i, 1, 2}])^2,