Skip to main content
2 of 3
added 2 characters in body

Error of ReplaceAll while using non linear fit model

while trying to fit a function with nonlinear fit model I get the error of ReplaceAll. The function itself works (checked it while printing the function with specified arguments).

I have no idea why I get this message, or to who variable it means but I know that the problem occurs when I try to extract the BestFitParameters, In the return of the function PhaseShift.

I think I should note that the function itself also uses the nonlinear fit model.

energy = (*some list of data, it is below*)
momeuntum = (*some list of data, it is below*)
phase = (*some list of data, it is below*)


mass = 1/2 LinearModelFit[Transpose[{energy, momentum^2}], x, x][
    "BestFitParameters"][[2]];


PhaseShift[a_?NumberQ, b_?NumberQ, L_, e_?NumberQ] := 
 Module[{y, end, begin, nlm},
  end = 5/L;
  begin = 4/L;
  y = y /. 
    NDSolve[{y''[r] == 
    -2 mass (e - E^- (L r)^2 (a + b r^2) ) y[r], y[0] == 0, y'[0] == 1}
    , y, {r, 0, end}][[1]];
  
  nlm = NonlinearModelFit[
    Transpose[{Table[i, {i, begin, end, (end - begin)/100}], 
      y[Table[i, {i, begin, end, (end - begin)/100}]]}],
    A Sin[Sqrt[2 mass e]r + d], {A, d}, r];

  Return[180/\[Pi] Mod[d /. nlm["BestFitParameters"], 2 \[Pi]]];
  ];


nnlm = NonlinearModelFit[
  Transpose[{energy[[1 ;; 40]], phase[[1 ;; 40]]}], 
  PhaseShift[a, b, 0.3, e], {a, b}, e];

Print/@{nnlm["BestFitParameters"]};
Show[ListPlot[Transpose[{energy[[1;;40]],phase[[1;;40]]}]],Plot[nnlm[x],{x,energy[[1]],energy[[40]]},PlotStyle\[Rule]Orange]]
(*Print[phaseShift[100, -65, 10, 10]];*)

And if one needs the data

energy = {0.0001, 0.01, 0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6, 
 0.7000000000000001, 0.8, 0.9, 1., 1.1, 1.2000000000000002, 
 1.3000000000000003, 1.4000000000000001, 1.5000000000000002, 1.6, 
 1.7000000000000002, 1.8000000000000003, 1.9000000000000001, 2., 2.1, 2.2, 
 2.5, 3., 3.5, 4., 4.5, 5., 5.5, 6., 6.5, 7., 7.5, 8., 8.5, 9., 9.5, 10., 
 10.5, 11., 11.5, 12., 12.5, 13., 13.5, 14., 14.5, 15., 15.5, 16., 16.5, 17., 
 17.5, 18., 18.5, 19., 19.5, 20., 20.5, 21., 21.5, 22., 22.5, 23., 23.5, 24., 
 24.5, 25., 25.5, 26., 26.5, 27., 27.5, 28., 28.5, 29., 29.5, 30., 30.5, 31., 
 31.5, 32., 32.5, 33., 33.5, 34., 34.5, 35., 35.5, 36., 36.5, 37., 37.5, 38., 
 38.5, 39., 39.5, 40., 40.5, 41., 41.5, 42., 42.5, 43., 43.5, 44., 44.5, 45., 
 45.5, 46., 46.5, 47., 47.5, 48., 48.5, 49., 49.5, 50., 50.5, 51., 51.5, 52., 
 52.5, 53., 53.5, 54., 54.5, 55., 55.5, 56., 56.5, 57., 57.5, 58., 58.5, 59., 
 59.5, 60., 60.5, 61., 61.5, 62., 62.5, 63., 63.5, 64., 64.5, 65., 65.5, 66., 
 66.5, 67., 67.5, 68., 68.5, 69., 69.5, 70., 70.5, 71., 71.5, 72., 72.5, 73., 
 73.5, 74., 74.5, 75., 75.5, 76., 76.5, 77., 77.5, 78., 78.5, 79., 79.5, 80., 
 80.5, 81., 81.5, 82., 82.5, 83., 83.5, 84., 84.5, 85., 85.5, 86., 86.5, 87., 
 87.5, 88., 88.5, 89., 89.5, 90., 90.5, 91., 91.5, 92., 92.5, 93., 93.5, 94., 
 94.5, 95., 95.5, 96., 96.5, 97., 97.5, 98., 98.5, 99., 99.5, 100., 100.5};


momentum = {0.3157, 3.0582, 9.6874, 13.6926, 16.7902, 19.3749, 21.6635, 23.7352, 
 25.6293, 27.405, 29.0623, 30.6407, 32.1402, 33.5607, 34.9418, 36.244, 
 37.5265, 38.7497, 39.9533, 41.0976, 42.2222, 43.3271, 44.3925, 45.4382, 
 48.4372, 53.0737, 57.3156, 61.2814, 64.9906, 68.5026, 71.8567, 75.0529, 
 78.1111, 81.0508, 83.9117, 86.6542, 89.3177, 91.9221, 94.4278, 96.894, 
 99.2814, 101.6095, 103.8982, 106.1277, 108.3177, 110.4683, 112.5794, 
 114.6313, 116.6635, 118.6562, 120.6292, 122.543, 124.4568, 126.3312, 
 128.1661, 129.9812, 131.7767, 133.5524, 135.2886, 137.0249, 138.7216, 
 140.3987, 142.056, 143.6936, 145.3312, 146.9293, 148.5274, 150.0861, 
 151.6448, 153.1837, 154.7029, 156.2221, 157.7216, 159.2014, 160.6614, 
 162.1214, 163.5617, 164.9823, 166.4028, 167.8037, 169.2045, 170.5856, 
 171.947, 173.3083, 174.6697, 175.9916, 177.3332, 178.6551, 179.9573, 
 181.2595, 182.542, 183.8244, 185.1069, 186.3696, 187.6126, 188.8556, 
 190.0986, 191.3218, 192.5451, 193.7683, 194.9719, 196.1754, 197.3592, 
 198.5627, 199.7268, 200.9106, 202.0747, 203.219, 204.3831, 205.5274, 
 206.652, 207.7964, 208.921, 210.0456, 211.1505, 212.2553, 213.3602, 
 214.4651, 215.5503, 216.6354, 217.7206, 218.8057, 219.8711, 220.9365, 
 221.9822, 223.0477, 224.0933, 225.139, 226.1847, 227.2107, 228.2366, 
 229.2626, 230.2886, 231.3145, 232.3208, 233.327, 234.3332, 235.3394, 
 236.3259, 237.3124, 238.2989, 239.2854, 240.2719, 241.2387, 242.2055, 
 243.1722, 244.139, 245.1058, 246.0528, 246.9999, 247.9469, 248.894, 249.841, 
 250.788, 251.7153, 252.6427, 253.57, 254.4973, 255.4246, 256.3322, 257.2397, 
 258.1473, 259.0549, 259.9625, 260.8701, 261.7579, 262.6655, 263.5533, 
 264.4412, 265.329, 266.2169, 267.085, 267.9729, 268.841, 269.7091, 270.5772, 
 271.4453, 272.3135, 273.1816, 274.03, 274.8784, 275.7465, 276.5949, 
 277.4433, 278.2719, 279.1203, 279.9687, 280.7974, 281.626, 282.4547, 
 283.3031, 284.112, 284.9407, 285.7693, 286.598, 287.4069, 288.2158, 
 289.0248, 289.8534, 290.6624, 291.4516, 292.2605, 293.0694, 293.8586, 
 294.6676, 295.4567, 296.246, 297.0352, 297.8244, 298.6136, 299.4028, 
 300.192, 300.9614, 301.7506, 302.5201, 303.2896, 304.0788, 304.8482, 
 305.6177, 306.3674, 307.1369};

phase = {179.1029, 171.0757, 152.9846, 143.3802, 136.7981, 131.7598, 127.6793, 
 124.2547, 121.3079, 118.7245, 116.4264, 114.358, 112.4783, 110.7561, 
 109.1674, 107.693, 106.3177, 105.0289, 103.8163, 102.6714, 101.5867, 
 100.5562, 99.5747, 98.6374, 96.0547, 92.3488, 89.1896, 86.429, 83.9717, 
 81.7534, 79.7283, 77.8627, 76.1313, 74.5146, 72.9968, 71.5659, 70.2112, 
 68.9248, 67.6992, 66.5289, 65.4087, 64.3338, 63.3012, 62.3069, 61.3483, 
 60.4231, 59.5284, 58.6625, 57.824, 57.0105, 56.2206, 55.4538, 54.708, 
 53.982, 53.2756, 52.5874, 51.9161, 51.2613, 50.6228, 49.999, 49.3893, 
 48.7938, 48.2118, 47.6421, 47.0844, 46.539, 46.0051, 45.4816, 44.9684, 
 44.466, 43.9736, 43.4902, 43.0158, 42.5508, 42.0947, 41.6465, 41.206, 
 40.7738, 40.3497, 39.9328, 39.5226, 39.1194, 38.7236, 38.3346, 37.9516, 
 37.5745, 37.2038, 36.8397, 36.4813, 36.1281, 35.7801, 35.438, 35.1016, 
 34.7703, 34.4434, 34.1212, 33.8043, 33.4926, 33.1853, 32.8819, 32.5827, 
 32.2882, 31.9984, 31.7127, 31.4305, 31.1519, 30.8774, 30.6072, 30.341, 
 30.078, 29.818, 29.5615, 29.309, 29.0602, 28.8147, 28.5718, 28.3316, 
 28.0949, 27.8618, 27.632, 27.4048, 27.1799, 26.9576, 26.7385, 26.5227, 
 26.3098, 26.0991, 25.8904, 25.6841, 25.4807, 25.2804, 25.0827, 24.887, 
 24.693, 24.5011, 24.3118, 24.1254, 23.9414, 23.7593, 23.5787, 23.3998, 
 23.2232, 23.0492, 22.8776, 22.7079, 22.5395, 22.3725, 22.2074, 22.0446, 
 21.8842, 21.7257, 21.5686, 21.4126, 21.258, 21.1053, 20.9548, 20.8065, 
 20.6597, 20.514, 20.3692, 20.2258, 20.0843, 19.9449, 19.8073, 19.6711, 
 19.5357, 19.4011, 19.2678, 19.1363, 19.0068, 18.879, 18.7524, 18.6264, 
 18.5012, 18.377, 18.2545, 18.1339, 18.0149, 17.897, 17.7798, 17.6631, 
 17.5472, 17.4328, 17.3201, 17.209, 17.0992, 16.9899, 16.8811, 16.7729, 
 16.6658, 16.5602, 16.4563, 16.3537, 16.2519, 16.1504, 16.0493, 15.949, 
 15.8498, 15.7522, 15.656, 15.5609, 15.4664, 15.372, 15.278, 15.1848, 
 15.0928, 15.0023, 14.9131, 14.8248, 14.7368, 14.649, 14.5615, 14.4748, 
 14.3893, 14.3051, 14.2222, 14.1401, 14.0582, 13.9765, 13.8949, 13.814};
saar
  • 11
  • 1