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:

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 :

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":

a = 1.8 (*Included for obtain a "calculated"*)

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

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

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

  • $\begingroup$ Don't define a = 1.8. Use parentheses, not braces, in defining sseval[]. $\endgroup$
    – Michael E2
    May 3, 2019 at 23:51

1 Answer 1


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}];

enter image description here

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}]

enter image description here

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

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


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.