Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

First of all, a bit of background, I am using Mathematica to fit angular distributions of gamma rays, so I apologize for some of the serious messiness in the formulas.

The data is simple I have three lists. The first is dataWithErrors = {{x1,y1,dy1}, {x2, y2, dy2}, ... }. The second, rawData = {{x1,y1}, {x2,y2}, ... } is what actually goes to the model fit function. The third is ptWeights={1/dy1^2, 1/dy2^2, ... } Now the code:

P[m_, J1_, sig_]:=E^(-m^2/(2*sig^2))/Sum[E^(-mp^2/(2*sig^2)),{mp,-J1,J1}]
(*Constants to account for the detectors not being points*)
saCorr2 = 0.987751
saCorr4 = 0.959561

Now to perform the fit I use the following command:

TransitionFit = NonlinearModelFit[rawData,a0(1 + saCorr2*ForTransition[ 2, delt, sig]*LegendreP[2, Cos[th] ] + saCorr4*ForTransition[ 4, delt, sig]*LegendreP[4, Cos[th] ],{{a0,12544.9},{delt,-1.2395},{sig,1.983}}, th,Weights -> ptWeights, VarianceEstimatorFunction -> (1 &)]

I calculate the reduced chi^2 using:


Dividing by 16-3 because I have 16 points and 3 parameters.

Here is the problem, if I run the fit with sig as a free parameter I get a0=12567.8, delt=-1.28286, sig=1.94335, and a reduced chi^2 of 2.912.

All of which looks somewhat reasonable. But if I fix sig = 1.983 (and calculate reduced chi^2 exactly the same way) I get a0=12576.4, delt=-1.25549, and a reduced ch^2 of 2.901.

If I also fix delt = -1.23947... I get a0=12568.1 and a reduced chi^2 of 2.89344.

Finally if I simply calculate the chi^2 with a0 = 12544.9 and delt and sig as above I get a reduced chi^2 of 2.88363.

As you can see in the NonlinearModelFit call I give it the ideal values, and it still is not giving those values

I realize that these are small differences in the chi^2, but I don't understand why mathematica is doing this. Why is this happening? Are there any suggestions to fix it? Things I can do to improve how my code?

share|improve this question

closed as off-topic by Louis, MarcoB, m_goldberg, Kuba, Yves Klett Jun 28 at 7:29

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

  • "The question is out of scope for this site. The answer to this question requires either advice from Wolfram support or the services of a professional consultant." – Louis, MarcoB
If this question can be reworded to fit the rules in the help center, please edit the question.

I'm voting to close this question as off-topic because it is too localized; i.e, it applies only to the local situation and needs of its poster and answers will not benefit others. – m_goldberg Jun 27 at 1:08

Browse other questions tagged or ask your own question.