0
$\begingroup$

Hi I cannot figure out this from the documentation of this command. For example, take one of the examples in the documentation, where I also added initial guesses for the parameters.

data = {{6.47, 3.65}, {7.43, 
   3.45}, {3.9, -2.94}, {4.8, -1.29}, {2.48, -0.35}, {6.32, 
   3.16}, {2.59, -1.19}, {9.13, -2.}, {3.81, -3.04}, {3.33, -2.68}}; \
Clear[model];
model[a_?NumberQ, b_?NumberQ, 
  c_?NumberQ] :=  (model[a, b, c] = 
   Module[{y, x}, 
    First[y /. 
      NDSolve[{y''[x] + a y[x] == 0, y[0] == b, y'[0] == c}, 
       y, {x, 0, 10}]]])

nlm = NonlinearModelFit[data, 
  model[a, b, c][x], {{a, 1}, {b, 2}, {c, 2}}, x, 
  Method -> "Gradient"]; Show[ListPlot[data], 
 Plot[nlm[x], {x, 0, 10}, PlotStyle -> Orange]]

Next, suppose I want to change the initial guesses to constraints $a \in [1,1.5]$, etc . My first guess does not work

nlm1 = NonlinearModelFit[data, 
      model[a, b, c][x], {{a, 1,1.5}, {b, 2,2.5}, {c, 2,3}}, x, 
      Method -> "Gradient"];
$\endgroup$
2
  • 3
    $\begingroup$ Constraints are added in the "model" section of the command: NonlinearModelFit[data, {form, cons}, {β1, …}, {x1, …}]. cons are your constraints. It's the third headline example in the documentation. $\endgroup$
    – MarcoB
    Feb 14 at 14:53
  • 4
    $\begingroup$ As shown in the documentation, "NonlinearModelFit[data, {form, cons}, {[Beta]1, ...}, {x1, ...}] constructs a nonlinear model subject to the parameter constraints cons." Group the constraints with the model. $\endgroup$
    – Bob Hanlon
    Feb 14 at 14:53

1 Answer 1

4
$\begingroup$

A simple F1 would have shown the following.

enter image description here

Notice it reads

NonlinearModelFit[data,{form,cons},{β1,…},{x1,…}] constructs a nonlinear model subject to the parameter constraints cons.

With examples like

NonlinearModelFit[data, {Log[a + b x^2], a > 0 && b > 0}, {a, b}, x, 
 Method -> "InteriorPoint"]

In your case

nlm = NonlinearModelFit[
    data
    , {
        model[a, b, c][x],
        1 < a < 1.5
    }
    , {{a, 1}, {b, 2}, {c, 2}}
    , x
]; 

Beware that Method -> "Gradient" is incompatible with constrains. You may get the error

Gradient can only be used for unconstrained problems. NonlinearModelFit::ucmtd: Method -> Gradient can only be used for unconstrained problem

$\endgroup$

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