Why is ww[[1]] not a valid variable when given to FindMinValue and Plot, but is a variable in Minimize? How can I fix the problem?

ww = Table[w[i], {i, 1, 6}];
a1[y1_, x_] = Function[#1*Cos[#1*0.1] + #2*2][y1, x];

Minimize[a1[ww[[1]], 3], ww[[1]]]
FindMinValue[a1[ww[[1]], 3], {ww[[1]], 1}]
Plot[a1[ww[[1]], 3], {ww[[1]], 0, 1}]

{0.389037, {w[1] -> -8.60334}}

FindMinValue::vloc: The variable ww[[1]] cannot be localized so that it can 
be assigned to numerical values. >>

FindMinValue[a1[ww[[1]], 3], {ww[[1]], 1}]

Plot::write: Tag Part in ww[[1]] is Protected. >>

Plot[a1[ww[[1]], 3], {ww[[1]], 0, 1}]
  • 1
    $\begingroup$ Plot has the attribute HoldAll, but you may enforce evaluation with Evaluate: Plot[Evaluate[a1[ww[[1]], 3]], Evaluate[{ww[[1]], 0, 1}]]. Similarly, you may use FindMinValue[a1[ww[[1]], 3], Evaluate@{ww[[1]], 1}]. $\endgroup$ – Henrik Schumacher Feb 4 at 18:47
  • $\begingroup$ Thanks a lots. But it is not uncomfortable to use "Evaluate" all time. Maybe is there other way how make function with different amount of variables defined by array ? $\endgroup$ – ILIa Feb 5 at 8:37

This is not so much an answer as some advice on how you can avoid all the issues that are causing you problems by getting rid of unnecessary complications in your code. You will do better by abandoning the array ww and Function.

a1[y_, x_] := y Cos[.1 y] + 2 x
min = Minimize[a1[w[1], 3], w[1]]

{0.389037, {w[1] -> -8.60334}}

FindMinValue[a1[w[1], 3], {w[1], -1}]


To make a plot showing the minimum, extract the coordinates of the minimum

minPt = {min[[2, 1, 2]], min[[1]]}

{-8.60334, 0.389037}

and add it to a plot of a with Epilog.

Plot[a1[w[1], 3], {w[1], -15, 1},
  Epilog -> {Red, AbsolutePointSize[5], Point[minPt]}]


  • $\begingroup$ Thanks. But my general goal is to learn how make function with different amount of variables defined by array (Table). $\endgroup$ – ILIa Feb 5 at 8:25
  • $\begingroup$ @ILIa. In general you don't have make indexed variables with Table, you can just refer to as I did in the code above and there will be automatically created. If for some strange reason you really do have make them, Do is better than Table $\endgroup$ – m_goldberg Feb 5 at 18:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.