I have created this function which calculate the figure of merit of data acquired. The Loading variable is feed with some random numbers for the forum. In real life, I have real data.
DD = 20; Days = 3; Peak = 1;
Loading = Table[Table[{RandomReal[{-1500, 1500}], RandomReal[{-1, 1}/100]}, {i, 1, 150}], {j, 1, Days}];
coo = Loading[[All, All, 1]];
TimeSwapFunction[lst_] :=
Norm[{Temp =
Sort[Partition[
Flatten[Riffle[
Flatten[Table[coo[[i]] + lst[[i]], {i, 1, Days}]],
Flatten[Loading[[1 ;; Days, All, Peak + 1]]]]],
2], #1[[1]] < #2[[1]] &];
Mean[Select[
Partition[
Riffle[Mean[Transpose[Partition[Temp[[All, 1]], DD]]],
Abs[Mean[
Transpose[
Partition[Temp[[All, 2]], DD]]]/(StandardDeviation[
Transpose[Partition[Temp[[All, 2]], DD]]]/Sqrt[DD])]],
2], #1[[1]] > 0 &][[All, 2]]]}]
The function is working now. As you can see, if we plot the TimeSwapFunction (see below), there is some maxima. The aim is to use NMaximize to find the maxima with the algorithms already coded in mathematica like a Genetic Algorithm.
But if I use directly NMaximize (with only one variable), it is not working. You can see it by looking at the inputs 19th and 21st. The output is some value that I don't understand from where it comes from.
If I allow two variables, that is the same story (input 25th). If i put the third one, it is not working anymore.
I would be really glad if someone can help me to go through this thing.
This is fixed by defining your TimeSwapFunction so that it takes only numeric arguments:
Clear[TimeSwapFunction]
TimeSwapFunction[lst_ /; (And @@ ( NumericQ /@ lst))] := (*the rest the same*)
NMaximize[TimeSwapFunction[{c, b, a}], {c, b, a}]
{0.804359, {c -> 7.35047, b -> -4.85534, a -> 3.51407}}
This is a commonly encountered issue with many of the N
numeric functions (NIntegrate
,etc)
You should not use variables starting with upper case letters by the way, to avoid conflict with built-ins (not apparently an issue in the case however )
As another aside be aware with such a highly nonlinear function you are likely to find local rather than global extremes.