# How do I set a multi “pars” constraint for a NonlinearModelFit?

data = Table[{x, (((x - 1.6)^2)/(16.456*(1 +
Sqrt[1 - (1 + 0.65)*((x - 1.6)^2)/(16.456^2)]))) +
0.00032*(x - 1.6)^4 + .6554}, {x, -0.65, 4.073, .001}];
fit = NonlinearModelFit[
data, {(((x - g)^2)/(c*(1 + Sqrt[1 - (1 + k)*((x - g)^2)/(c^2)]))) +
a*(x - g)^4 +
p,((1 + k)*((x - g)^2)/(c^2)) < 1}, {a, c, k, g, p},
x]

Show[ListPlot[data],
Plot[fit[x], {x, -0.65, 4.073}, PlotStyle -> Red], Frame -> True]


Is my input and I get no answer because the square root making imaginary values so i tried to add

((1 + k)*((x - g)^2)/(c^2)) < 1


but it doesn't seem to be fixing the problem.

• You'll get more targeted help if you also show the error messages you're getting. – JimB Sep 19 at 3:10

The error message you get is the following: That error message concerns the restriction on the parameters when the parameters are replaced with the initial values. The restrictions must be just about the parameters (and any constants you throw in) so NonlinearModelFit doesn't know what x is in your restriction.

Because of the form of the desired restriction you can use the following:

Max[((1 + k)*((Min[data[[All, 1]]] - g)^2)/(c^2)), (1 + k)*((Max[data[[All, 1]]] - g)^2)/(c^2)] < 1


But you'll also need to supply better starting values. The following works:

data = Table[{x, (((x - 1.6)^2)/(16.456*(1 + Sqrt[1 - (1 + 0.65)*((x - 1.6)^2)/(16.456^2)]))) +
0.00032*(x - 1.6)^4 + .6554}, {x, -0.65, 4.073, .001}];
fit = NonlinearModelFit[data, {(((x - g)^2)/(c*(1 + Sqrt[1 - (1 + k)*((x - g)^2)/(c^2)]))) +
a*(x - g)^4 + p,
Max[((1 + k)*((Min[data[[All, 1]]] - g)^2)/(c^2)), (1 +
k)*((Max[data[[All, 1]]] - g)^2)/(c^2)] < 1}, {{a, 0.001}, {c,
10}, {k, 1}, {g, 1}, {p, 1}}, x]
fit["BestFitParameters"]
(* {a -> 0.000580301, c -> 16.43, k -> -9.50327, g -> 1.60003, p -> 0.655387} *)


You'll note that the estimates for a and k are not what you used to generate the data with essentially no noise. That's because the estimators of a and k are nearly perfectly correlated with each other. That's a sign of either an overparameterized model or of a model that could use some reparameterization. One can see that by displaying the parameter correlation matrix:

fit["CorrelationMatrix"] // TableForm You'll still get appropriate predictions. It's just that you can't really get good separate estimates of a and k.