I have a function that I want to minimize numerically through an NMinimize command (because it's very complicated analytically). The minimizer call looks as follows:

NMinimize[{mat = func0[a, b, c, d, e, f]; Print[mat]; 
{ac0t, ac1t, ac2t, ac3t} = mat.{Ac0, Ac1, Ac2, Ac3}; 
{as0t, as1t, as2t, as3t} = mat.{As0, As1, As2, As3}; 
  })^2], constraints}, {a, b, c, d, e, f},  Method -> "NelderMead"]

So notice that I'm minimizing a sum of squares, but before calculating the sum of squares, I have to calculate stuff using a function func0. func0 is defined as a numerical function:


However, when it gets evaluated in the minimizer, it gets evaluated and held. So Print[mat] will result in func0[a, b, c, d, e, f], and not in a numerical value. While if I calculate this function outside NMinimze, it works fine. I can imagine that NMinimize is calling it with variables, and that's why it isn't working.

How can I tell NMinimize to take this only numerically?

Please advise. Thank you.

EDIT: As requested in comments, I'm adding an executable example of the problem:

func0[a_?NumericQ] := {{Sin[a Degree], Cos[a Degree]},{Sin[a Degree], Cos[a Degree]}}
NMinimize[{var = func0[a]; 
Print[var]; {aa, bb} = var.{1/Sqrt[2], 1/Sqrt[2]}; 
Total[{aa - bb, aa + bb
 }^2], a > 0}, {a}, Method -> "NelderMead"] 

If you execute this code, you'll see that an error will be thrown:

Set::shape: Lists {aa,bb} and func0[a].{1/Sqrt[2],1/Sqrt[2]} are not the same shape. >>

Which happens because func0[a] is being evaluated symbolically, while I must evaluate it numerically. Please note this is a simple example for the sake of argument. My func0 is very complicated and cannot be evaluated symbolically.

  • 1
    $\begingroup$ Could you please include complete code to demonstrate the issue? Call me lazy if you like, but I really can't be bothered to make up definitions for all your undefined symbols and insert filler code in place of your ellipses... $\endgroup$ Apr 24, 2014 at 11:34
  • $\begingroup$ Incidentally, if we just take mat = func0[a, b, c, d, e, f]; Print[mat]; and give func0 some definition that doesn't match for six symbolic arguments, we will get the same result as you report here. So it isn't clear what, if anything, this has to do with NMinimize. $\endgroup$ Apr 24, 2014 at 11:39
  • $\begingroup$ @OleksandrR. The code is really huge... I can't just put it here. What you're saying in your second comment is only correct if func0 is not defined or if you put parameters that are symbolic while you defined it to be numerical. But if you define func0 as numerical function and give it numerical values, then it'll give you a number definintely (or a matrix of numbers for that matter). I think the solution should be in a way to force NMinimize to call func0 numerically rather than symbolically. What do you think? $\endgroup$ Apr 24, 2014 at 11:45
  • $\begingroup$ You don't need to put your complete code in the post, only something that makes clear the point of the question--a simplified version of your real problem, if necessary. Otherwise we are all left guessing over what your actual issue might be. Regarding your last comment, well, NMinimize doesn't have HoldFirst, so you certainly are calling it with symbolic parameters. $\endgroup$ Apr 24, 2014 at 12:03
  • $\begingroup$ @OleksandrR. OK, I added a clean, small example. Please check it out. $\endgroup$ Apr 24, 2014 at 12:18

1 Answer 1


Its not func0[a] that is the problem; it's the compound expression you've given NMinimize to chew (look up the true meaning of ;). This compound expression is not a _?NumericQ function of a, so Mathematica can't tell that it is only valid for numeric values of a.


func1[a_?NumericQ] := (var = func0[a];   
   Print[var]; {aa, bb} = var.{1/Sqrt[2], 1/Sqrt[2]};
   Total[{aa - bb, aa + bb}^2]);


NMinimize[{func1[a], a > 0}, {a}, Method -> "NelderMead"]
  • $\begingroup$ I see, so the whole formula inside NMinimize must be numerical. A part of it won't be taken numerical. Either all or nothing. THank you for your help. $\endgroup$ Apr 24, 2014 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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