# NonlinearModelFit failing because varible range [closed]

I'm trying to fit a specific equation to a plot of points but its failing because the equation has a square root in it and I need to keep the values of x between a certain range. I know how to do this for the constants but how do I do this for a variable?

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)]))) +
c*(x - g)^4 +
p, {{a, 0.00032}, {c, 16.456}, {k, 0.65}, {g, 1.6}, {p, .6554}},
x, MaxIterations -> 100]

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


I'm telling it exactly where to start and it still can't find the fit.

## closed as off-topic by Bob Hanlon, JimB, C. E., MarcoB, m_goldbergSep 18 at 18:26

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – Bob Hanlon, JimB, C. E., MarcoB
If this question can be reworded to fit the rules in the help center, please edit the question.

• Please provide a concrete example including the code and data that can be used to reproduce the problem. Specify clearly what the problem is. – Bob Hanlon Sep 17 at 0:32
• Post a reduced form of your data points as well. Then we can play ourselves – morbo Sep 17 at 9:59
• Thank you for adding in the example. You have an error in the formula you placed in NonlinearModelFit. You have + c*(x-g)^4 when it should be + a*(x-g)^4. After making that change everything works fine. So I would bet money that the question will be closed. I wish I could say I've never made such a mistake (more than once). – JimB Sep 17 at 20:21
• @JimB Thanks, that fixed that problem but if I change the values to other random values then I get is not a list of real numbers with dimensions {4501} at {a,c,k,g,p} = \ {1.,1.,1.,1.,1.}. So what if I don't know where the constants should start at? – unseenmisfit Sep 17 at 22:10
• You'll get imaginary numbers anytime $\frac{(k+1) (x-g)^2}{c^2} > 1$ so your starting values should certainly be consistent with that. $g$ seems to be near where the minimum occurs and $p$ might be the minimum. Happy guessing! – JimB Sep 17 at 22:33

Welcome to MSE.
I believe the issue is that a is not defined in your model.

I have corrected, so the following should work:

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, {{a, 0.00032}, {c, 16.456}, {k, 0.65}, {g, 1.6}, {p, .6554}}, x,
MaxIterations -> 100]

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