# NMinimize "Function value is not a number" problem

**URL:** <https://community.wolfram.com/t/nminimize-function-value-is-not-a-number-problem/18022>\
**Category:** Questions\
**Tags:** optimization, numerical-computation\
**Created:** [April 26, 2021, 12:30pm UTC](https://community.wolfram.com/t/nminimize-function-value-is-not-a-number-problem/18022 "2021-04-26T12:30:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![riddhimansarkar](https://avatars.discourse-cdn.com/v4/letter/r/a9a28c/32.png) [@riddhimansarkar](https://community.wolfram.com/u/riddhimansarkar)\
**Post date:** [April 26, 2021, 12:30pm UTC](https://community.wolfram.com/t/nminimize-function-value-is-not-a-number-problem/18022/1 "2021-04-26T12:30:36Z")

</div>

I am trying to calculate the root mean squared deviation (RMSD) and minimise it using NMinimize between experimental data and a model. My model function has 2 parameters and the value of the function is an output from an external program which Mathematica calls and provides the inputs. The 2 parameters to be optimised by NMinimize are these inputs.

NMinimize runs for a while and complains that the function value is not a number at {x,y} (x, and y are the parameters). Whereas if I evaluate the function at those {x,y}, I do get an output which is a number.  
Any idea what is going wrong ? Below I paste part of my code:

```
simu[{x_?NumberQ, y_?NumberQ}] := (Interpreter["Number"][
   StringSplit[
    RunProcess[{"/program" , 
      "path/to/script/", ToString[x], ToString[y]]}, 
     "StandardOutput"]]])
   
RMSD[A_, B_, N_] := Sqrt[(Plus @@ ((A - B)^2)/N)]

NMinimize[RMSD[data, simu[{x, y}], 10], , {x, y}, MaxIterations -> 100]

```

Looking forward,

---

<div class="post-metadata">

**Author:** ![mfroeling](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mfroeling/32/23010_2.png) [@mfroeling](https://community.wolfram.com/u/mfroeling)\
**Post date:** [April 26, 2021, 3:15pm UTC](https://community.wolfram.com/t/nminimize-function-value-is-not-a-number-problem/18022/2 "2021-04-26T15:15:44Z")

</div>

NMinimize initializes with symbolic values, you want to prevent your functions evaluating with symbolic values.

so I guess making them conditional while only matching numerical inputs it will run.

```
In[1]:= (*Simulate some data and make a function to reproduce the data*)
n = 4;
data = Flatten[
   With[{x = 2, y = 4}, Table[(x i^2 + y j), {i, 1, n}, {j, 1, n}]] + 
    RandomReal[{-0.5, 0.5}, {n, n}]];

simu[x_, y_] := 
  Flatten[Table[(x i^2 + y j), {i, 1, n}, {j, 1, n}]] /; (NumberQ[x] &&
      NumberQ[y]);

In[4]:= (*always evaluates*)
RMSDA[A_, B_] := RootMeanSquare[A - B]
(*only evaluates numeric values*)
RMSDN[A_, B_] := RootMeanSquare[A - B] /; (ListQ[A] && ListQ[B] && AllTrue[A, NumericQ] && AllTrue[B, NumericQ])

In[6]:= NMinimize[RMSDA[data, simu[x, y]], {x, y}]

During evaluation of In[6]:= NMinimize::nnum: The function value {26.2704,25.6377,25.0096,24.3863,23.8636,23.2503,22.6432,22.0426,20.0527,19.4886,<<6>>} is not a number at {x,y} = {0.918621,0.716689}.

Out[6]= NMinimize[
 1/4 \[Sqrt]((5.52371 - simu[x, y])^2 + (10.3453 - 
       simu[x, y])^2 + (11.8308 - simu[x, y])^2 + (13.6971 - 
       simu[x, y])^2 + (15.5564 - simu[x, y])^2 + (18.3346 - 
       simu[x, y])^2 + (20.0972 - simu[x, y])^2 + (22.0655 - 
       simu[x, y])^2 + (24.0908 - simu[x, y])^2 + (25.778 - 
       simu[x, y])^2 + (29.8389 - simu[x, y])^2 + (33.9488 - 
       simu[x, y])^2 + (35.875 - simu[x, y])^2 + (40.0954 - 
       simu[x, y])^2 + (43.7572 - simu[x, y])^2 + (47.6609 - 
       simu[x, y])^2), {x, y}]

In[7]:= NMinimize[RMSDN[data, simu[x, y]], {x, y}]

Out[7]= {0.23916, {x -> 1.9883, y -> 4.00557}}

```

---

<div class="post-metadata">

**Author:** ![riddhimansarkar](https://avatars.discourse-cdn.com/v4/letter/r/a9a28c/32.png) [@riddhimansarkar](https://community.wolfram.com/u/riddhimansarkar)\
**Post date:** [April 28, 2021, 9:11am UTC](https://community.wolfram.com/t/nminimize-function-value-is-not-a-number-problem/18022/3 "2021-04-28T09:11:34Z")

</div>

Thanks a lot. I believe that was the issue.
