Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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}; 
Total[({
  a+b,
  a^2+b^2,
  ...
  })^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:

func0[a_?NumericQ,b_?NumericQ,c_?NumericQ,d_?NumericQ,e_?NumericQ,f_?NumericQ]:=Module[{},...]

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.

share|improve this question
1  
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... –  Oleksandr R. Apr 24 at 11:34
    
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. –  Oleksandr R. Apr 24 at 11:39
    
@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? –  The Quantum Physicist Apr 24 at 11:45
    
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. –  Oleksandr R. Apr 24 at 12:03
    
@OleksandrR. OK, I added a clean, small example. Please check it out. –  The Quantum Physicist Apr 24 at 12:18

1 Answer 1

up vote 5 down vote accepted

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.

Try:

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

and...

NMinimize[{func1[a], a > 0}, {a}, Method -> "NelderMead"]
share|improve this answer
3  
+1 for patience... –  Oleksandr R. Apr 24 at 13:23
    
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. –  The Quantum Physicist Apr 24 at 13:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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