Perplexed by the behavior of NonlinearFit in Mathematica ver 4.2 vs 5.1
- To: mathgroup at smc.vnet.net
- Subject: [mg54224] Perplexed by the behavior of NonlinearFit in Mathematica ver 4.2 vs 5.1
- From: ellisg at rogers.com (G Ellis)
- Date: Sun, 13 Feb 2005 22:17:12 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Please note that my questions are at the end of this Post. What follows is some necessary background: I include below a NonlinearFit function ("nlr[t]"), which I created in ver 4.2 of Mathematica. It is VERY simple. I plotted the function and the graph was what I expected. However, I never looked at tabulated results of the function and therefore I did not notice a peculiar issue with my syntax. It was only when I ran the code in ver 5.1 that I saw the odd output. (The code below includes tabulations that I created after I noticed the different behavior between ver 4.2 and ver 5.) In ver 4.2 Mathematica does not "complain" about the function "nlr[t]" in the following. It appears to plot the result correctly; but it returns different "Table" results without and with "Evaluate". I believe the correct results are those returned with "Evaluate" or the use of the "ReplaceAll" function. In[1]:= << "Statistics`" << "Graphics`" In[3]:= $Version Out[3]= "4.2 for Microsoft Windows \ (June 5, 2002)" In[4]:= pop = {1650, 1750, 1860, 2070, 2300, 2560, 3040, 3710, 4450, 5280, 6080}; nlr[t_] := NonlinearFit[pop, E^(theta*t)*k, {t}, {theta, k}] t1 = Table[nlr[t], {t, 0, 3}] t1 = Table[Evaluate[nlr[t]], {t, 0, 3}] t2 = Table[nlr[t] /. t -> i, {i, 0, 3}] Out[6]= {537.6363636363639, 3158.987930628625, 1531.0837992793613, 1782.0476907177406} Out[7]= {1130.2076401682348, 1315.4628872314652, 1531.0837992793613, 1782.0476907177406} Out[8]= {1130.2076401682348, 1315.4628872314652, 1531.0837992793613, 1782.0476907177406} p1 = Plot[nlr[t], {t, 0, 11}, PlotStyle -> IndianRed]; p2 = Plot[nlr[t] /. t -> i, {i, 0, 11}, PlotStyle -> EmeraldGreen]; In ver 5.1, Mathematica will not let me evaluate "nlr[t]" directly. (This is good, I think). It forces the use of "Evaluate". $Version "5.1 for Microsoft Windows \ (October 25, 2004)" pop = {1650, 1750, 1860, 2070, 2300, 2560, 3040, 3710, 4450, 5280, 6080}; nlr[t_] := NonlinearFit[pop, E^(theta*t)*k, {t}, {theta, k}] Table[nlr[t], {t, 0, 3}] Table[Evaluate[nlr[t]], {t, 0, 3}] Table[nlr[t] /. t -> i, {i, 0, 3}] \!\(\* RowBox[{\(General::"ivar"\), ":", "\<\"\\!\\(0\\) is not a valid variable. \\!\\(\\*ButtonBox[\\\"More\ \[Ellipsis]\\\", ButtonStyle->\\\"RefGuideLinkText\\\", ButtonFrame->None, \ ButtonData:>\\\"General::ivar\\\"]\\)\"\>"}]\) NonlinearFit::fitfail:The fitting algorithm failed. \!\(\* RowBox[{\(General::"ivar"\), ":", "\<\"\\!\\(1\\) is not a valid variable. \\!\\(\\*ButtonBox[\\\"More\ \[Ellipsis]\\\", ButtonStyle->\\\"RefGuideLinkText\\\", ButtonFrame->None, \ ButtonData:>\\\"General::ivar\\\"]\\)\"\>"}]\) NonlinearFit::fitfail:The fitting algorithm failed. \!\(\* RowBox[{\(General::"ivar"\), ":", "\<\"\\!\\(2\\) is not a valid variable. \\!\\(\\*ButtonBox[\\\"More\ \[Ellipsis]\\\", ButtonStyle->\\\"RefGuideLinkText\\\", ButtonFrame->None, \ ButtonData:>\\\"General::ivar\\\"]\\)\"\>"}]\) \!\(\* RowBox[{\(General::"stop"\), ":", "\<\"Further output of \\!\\(General :: \\\"ivar\\\"\\) will be \ suppressed during this calculation. \ \\!\\(\\*ButtonBox[\\\"More\[Ellipsis]\\\", ButtonStyle->\\\"RefGuideLinkText\ \\\", ButtonFrame->None, ButtonData:>\\\"General::stop\\\"]\\)\"\>"}]\) NonlinearFit::fitfail:The fitting algorithm failed. \!\(\* RowBox[{\(General::"stop"\), ":", "\<\"Further output of \\!\\(NonlinearFit :: \\\"fitfail\\\"\\) will \ be suppressed during this calculation. \\!\\(\\*ButtonBox[\\\"More\[Ellipsis]\ \\\", ButtonStyle->\\\"RefGuideLinkText\\\", ButtonFrame->None, \ ButtonData:>\\\"General::stop\\\"]\\)\"\>"}]\) {NonlinearFit[{1650, 1750, 1860, 2070, 2300, 2560, 3040, 3710, 4450, 5280, 6080}, k, {0}, {theta, k}], NonlinearFit[ {1650, 1750, 1860, 2070, 2300, 2560, 3040, 3710, 4450, 5280, 6080}, E^theta*k, {1}, {theta, k}], NonlinearFit[ {1650, 1750, 1860, 2070, 2300, 2560, 3040, 3710, 4450, 5280, 6080}, E^(2*theta)*k, {2}, {theta, k}], NonlinearFit[ {1650, 1750, 1860, 2070, 2300, 2560, 3040, 3710, 4450, 5280, 6080}, E^(3*theta)*k, {3}, {theta, k}]} {1130.2091592378029, 1315.4644493997696, 1531.0853778619419, 1782.049249124357} {1130.2091592378029, 1315.4644493997696, 1531.0853778619419, 1782.049249124357} So my questions are: 1)Why does "nlr[t]" appear to work in ver 4.2; but return apparently invalid results for zero and one, but correct results for higher values? 2) Why, in ver 4.2, does "Plot" return the (apparently) correct values with "nlr[t]" without the need for "Evaluate"; whereas "Table" does not? 3) What changed between versions 4.2 and 5?