0
$\begingroup$

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.

$\endgroup$

closed as off-topic by Bob Hanlon, JimB, C. E., MarcoB, m_goldberg Sep 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.

  • 1
    $\begingroup$ Please provide a concrete example including the code and data that can be used to reproduce the problem. Specify clearly what the problem is. $\endgroup$ – Bob Hanlon Sep 17 at 0:32
  • $\begingroup$ Post a reduced form of your data points as well. Then we can play ourselves $\endgroup$ – morbo Sep 17 at 9:59
  • $\begingroup$ 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). $\endgroup$ – JimB Sep 17 at 20:21
  • $\begingroup$ @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? $\endgroup$ – unseenmisfit Sep 17 at 22:10
  • $\begingroup$ 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! $\endgroup$ – JimB Sep 17 at 22:33
1
$\begingroup$

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]
$\endgroup$

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