Problem:
I'm trying to plot an exponential decay curve on a set of integer data, and print the various parameters and their uncertainties on the plot itself. Having adapted code from another question, this worked successfully with non-integer values with uncertainties in the data set. Here the data set is integer only and does not contain uncertainties and my attempt at adapting it has been unsuccessful.
The target output is something like this:
Code: (contains data)
dataHist5 = {{18, 74}, {36, 64}, {54, 62}, {72, 54}, {90, 47}, {108,
39}, {126, 40}, {144, 35}, {162, 34}, {180, 29}, {198, 34}, {216,
30}, {234, 31}, {252, 22}, {270, 14}, {288, 14}, {306, 13}, {324,
25}, {342, 18}, {360, 11}, {378, 13}, {396, 16}, {414, 13}, {432,
12}, {450, 10}, {468, 12}, {486, 11}, {504, 13}, {522, 9}, {540,
8}, {558, 7}, {576, 5}, {594, 5}, {612, 4}, {630, 5}, {648,
2}, {666, 1}, {684, 3}, {702, 2}, {720, 2}, {738, 1}, {756,
1}, {774, 1}, {792, 0}, {810, 0}, {828, 1}, {846, 2}, {864,
0}, {882, 1}, {900, 1}};
fitData = dataHist5;
Clear[A, k]
uncertainties = sqrt[dataHist5[[2]]];
fit = NonlinearModelFit[fitData, A Exp[-k t], {A, k}, t,
Weights -> 1/dataHist5[[2]]]
{A, k} = {A, k} /. fit["BestFitParameters"];
{\[Sigma]A, \[Sigma]k} = fit["ParameterErrors"];
hLife = Log[2]/Around[k, \[Sigma]k];
halfLife = hLife[[1]];
seA = Around[A, \[Sigma]A];
sehalfLife = hLife[[2]];
Show[Plot[fit[x], {x, 0, 900}, PlotRange -> All,
PlotTheme -> "Detailed", PlotStyle -> Red, Axes -> False,
Frame -> {{True, False}, {True, False}},
FrameLabel -> {"Time /s",
"Counts Recorded in the Previous 15 seconds"},
ImageSize -> Large], ListPlot[dataHist5, ImageSize -> Large],
Graphics[Inset[
Framed[Column[{Style["Run 0", Bold],
Row[{"Data Points = ", Length[dataHist5], "/50"}],
Row[{Subscript[t, Style["1/2", FontSize -> 10]], " = ",
PlusMinus[NumberForm[halfLife, 4],
NumberForm[sehalfLife, 3]]}],
Row[{"A = " PlusMinus[Round[A], Round[\[Sigma]A]]}],
Row[{"\[Lambda] = " PlusMinus[NumberForm[k, 3],
NumberForm[\[Sigma]k, 2]]}],
Row[{Superscript[\[Chi], 2], "= ",
NumberForm[fit["ANOVATableSumsOfSquares"][[2]], 4]}],
Row[{"Reduced " Superscript[\[Chi], 2], "= ",
NumberForm[fit["ANOVATableMeanSquares"][[2]], 3]}]}],
Background -> White, RoundingRadius -> 5], {Right, Top},
Scaled[{1.1, 1.2}]]],
PlotLabel ->
Style["Decay Curve of Phosphorus-30 by \[Beta]+ Emission", Bold]]
Current Output:
Among many errors are 'NonlinearModelFit::wtsln: The number of weights 2 specified by Weights->{0.0277778,0.015625} is not the same as the number of data points 50.' and 'Set::shape: Lists {A,k} and {A,k}/. NonlinearModelFit[<<1>>][BestFitParameters] are not the same shape.'(though there are other errors as well)
Attempted Solution (Following JimB's answer):
The ParameterTable elements are seemingly being pulled, but not the DevianceTable ones, and I'm not super sure I've worked out the uncertainty in A and t correctly. Also the axes labels aren't appearing.
glm = GeneralizedLinearModelFit[dataHist5, t, t,
ExponentialFamily -> "Poisson"]
halfLife = (Log[E, 2]/glm["ParameterTableEntries"][[2, 1]])
sehalfLife = (((glm["ParameterTableEntries"][[2, 2]])/(glm[
"ParameterTableEntries"][[2, 1]])))*halfLife
k = glm["ParameterTableEntries"][[2, 1]]
\[Sigma]k = glm["ParameterTableEntries"][[2, 2]]
edp = glm["DevianceTableEntries"][[4, 2]]
redp = (glm["DevianceTableEntries"][[4, 2]])/(glm[
"DevianceTableEntries"][[3, 2]])
A = E^(glm["ParameterTableEntries"][[1, 1]])
\[Sigma]A = (A*(glm["ParameterTableEntries"][[1, 1]])/E)
Show[ListPlot[dataHist5],
Plot[glm[t], {t, 0, 900}, PlotRange -> All, PlotTheme -> "Detailed",
PlotStyle -> Red, Axes -> False,
Frame -> {{True, False}, {True, False}},
FrameLabel -> {"Time /s",
"Counts Recorded in the Previous 15 seconds"},
ImageSize -> Large],
Graphics[Inset[
Framed[Column[{Style["Run 0", Bold],
Row[{"Data Points = ", Length[dataHist5], "/50"}],
Row[{Subscript[t, Style["1/2", FontSize -> 10]], " = ",
PlusMinus[NumberForm[halfLife, 4],
NumberForm[sehalfLife, 3]]}],
Row[{"A = " PlusMinus[Round[A], Round[\[Sigma]A]]}],
Row[{"\[Lambda] = " PlusMinus[NumberForm[k, 3],
NumberForm[\[Sigma]k, 2]]}],
Row[{Superscript[\[Chi], 2], "= ", NumberForm[edp, 4]}],
Row[{"Reduced " Superscript[\[Chi], 2], "= ",
NumberForm[redp, 3]}]}], Background -> White,
RoundingRadius -> 5], {Right, Top}, Scaled[{1.1, 1.2}]]],
PlotLabel ->
Style["Decay Curve of Phosphorus-30 by \[Beta]+ Emission", Bold]]
Weights -> 1/dataHist5[[All, 2]]
? Also, you have a lower case s inSqrt’ for your uncertainties and you have
[[2]]` there instead of[[All, 2]]
. $\endgroup$1/0
complexiinfinity type errors. Would you want to post it as an answer so it can be accepted / upvoted? $\endgroup$Weights->1/(eps+dataHist5[[All, 2]])
witheps
a small number, you do not need to delete points. $\endgroup$