# Nminimize seems not work

I have to adjust a set of experimental data to a relatively complex nonlinear model. For this example I do a simulation of the data with a simplified function:

ClearAll["Global*"];
fun[a_?NumberQ, Vb_List] := Module[{H, pH}, {pHc = {};
For[j = 1, j <= l, j++,
x = Vb[[j]];
Hc = x/a + a^-x 2 a x^2;
AppendTo[pHc, Hc]]
}]


Then I calcule "experimental" data :

Clear[pHc]
a = 2 ;
Va = 50; Vb = Range[0, 20, 0.2];l = Length[Vb];
fun[a, Vb] // Quiet;
pHexp = pHc;
data2 = Transpose[{Vb, Flatten[pHexp]}];


And finally I want to minimize the value of the Norm[pHc-pHexp] to obtain the best value of the parameter "a":

(*Minimization*)
Clear[a]
a = 1.8 (*Included for obtain a "calculated"*)

sseval[a_?NumericQ] := {fun[a, Vb] // Quiet; sse = Norm[pHc -
pHexp]}
NMinimize[sseval[a], {a, 1}]

datacalc = Transpose[{Vb, Flatten[pHc]}];

(*PLOT*)
estil = {PlotStyle -> {Dashing[Tiny], Thickness[0.001]},
ImageSize -> {600}, AspectRatio -> 1/GoldenRatio, Frame -> True,
FrameStyle ->
Directive[FontFamily -> "Bookman Old Style", 18, Black],
RotateLabel -> False, FrameLabel -> {{"Hc", "" }, {"H", Title}},
GridLines -> Automatic, PlotMarkers -> "o"};

ListLinePlot[{data2, datacalc}, Evaluate[estil]]
Compara = TableForm[Transpose[{Vb, pHexp, Flatten[pHc]}]]


It seems that the Nminimize command doesn't send any value of "a" to the function sseval, and do not run for search the minimun...

Could you help me? Thanks

• Don't define a = 1.8. Use parentheses, not braces, in defining sseval[]. May 3, 2019 at 23:51

Your code works if, as mentioned in the comments, a is maintained as a symbol.

I've cleaned it up a bit here:

fun2[a_, Vb_List] := Module[{},
Table[x/a + a^-x 2 a x^2, {x, Vb}]]

Vb = Range[0, 20, 0.2];
pHc = fun2[2, Vb];

data2 = Transpose[{Vb, pHc}];
ListPlot[data2]


If the thing you want to minimize is Norm[pHc - fun2[a, Vb]], it will obviously have the minimum at a=2, since that is what was used to make pHc.

Plot[Norm[pHc - fun2[a, Vb]], {a, 1, 3}]


Clear[a]
NMinimize[Norm[pHc - fun2[a, Vb]], {a, 1, 3}]
`

{7.05466*10^-7, {a -> 2.}}