Re: ListPlotFit
- To: mathgroup at smc.vnet.net
- Subject: [mg17155] Re: ListPlotFit
- From: BobHanlon at aol.com
- Date: Sun, 18 Apr 1999 00:59:42 -0400
- Sender: owner-wri-mathgroup at wolfram.com
In a message dated 4/17/99 4:48:55 AM, Maarten.vanderBurgt at icos.be writes: >With the function ListPlotFit below I combine ListPlot with Fit and Plot >in >order to show the data from list l together with the straight line fitted >through a part of the data points from list l. The list l is of form >{{x1,y1},{x2,y2}, ...}. > >The part of the data points I want to fit a line through is determined >by >start and stop. The default value for the optional argument stop is 10. >This is not really what I want: I want the default value for stop to be >stop = Length[l]. How can I achieve this? > >I tried ListPlotFit[l_, start_:1, stop_:Hold[Lenght[l]] := Module[{},stop >= >ReleaseHold[stop];...], but this did not work (I get an error: "Hold is >Protected..."). I think I need Hold or something of the sort otherwise >Lenght[l] gets evaluated to 1 immediately. > >I guess I can phrase my question more general: how do I define a function >F >with an optional argument v which has as default value a function g of >a >non-optional argument u (i.e. F[u_, v_:g[u]])? > > >In[1]:= ListPlotFit[l_, start_:1, stop_:10] := > Module[ > {lp, fie, fiep}, > lp = ListPlot[l, DisplayFunction -> Identity, PlotStyle -> >PointSize[0.015], PlotRange -> All]; > fie = Fit[l[[Table[j, {j, start, stop}]]], {1, x}, x]; > fiep = Plot[fie, {x, 1, Length[l]}, DisplayFunction -> Identity]; > Show[lp, fiep, DisplayFunction -> $DisplayFunction] > ] > >In[2]:= dat= Table[{i,3*i^2+5+2*(Random[]-0.5)},{i,1,15}]; >In[3]:= ListPlotFit[dat, 2, 12]; > Maarten, Try this. I take a slightly different approach than yours, but believe that the behavior is consistent with what you want. I made some minor mods to your function. ListPlotFit[l_List, start_Integer:1] := ListPlotFit[l, start, Length[l]] ; ListPlotFit[l_List, start_Integer, stop_Integer] := Module[ {lp, fie, fiep}, lp = ListPlot[l, DisplayFunction -> Identity, PlotStyle -> PointSize[0.015], PlotRange -> All]; fie = Fit[l[[Range[start, stop]]], {1, x}, x]; fiep = Plot[fie, {x, 1, Length[l]}, DisplayFunction -> Identity]; Show[lp, fiep, DisplayFunction -> $DisplayFunction] ]/; stop > start dat= Table[{i,3*i^2+5+2*(Random[]-0.5)},{i,1,15}]; ListPlotFit[dat, 2, 12]; ListPlotFit[dat, 2]; ListPlotFit[dat]; Bob Hanlon