Re: ListPlotFit
- To: mathgroup at smc.vnet.net
- Subject: [mg17231] Re: ListPlotFit
- From: Allan Hayes <hay at haystack.demon.co.uk>
- Date: Fri, 30 Apr 1999 02:34:30 -0400
- References: <7fp3um$31l@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Maarten Thanks for the information about Roman Maeder's approach. I would like to add localization of the variable default in > ListPlotFit[l_, start_:1, stop_:default] := > Module[{lp, fie, fiep}, > lp = ListPlot[l, DisplayFunction -> Identity, PlotStyle -> > PointSize[0.015], PlotRange -> All]; > If[stop===default, stop = Length[l]]; > 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]] > For extreme example of what might happen without this use the definition with the assignment default := (Print["Fail"];Throw) either before or after defining ListPlotFit. One way out is to use an outer Module: Module[{default}, ListPlotFit[l_, start_:1, stop_:default] := Module[{lp, fie, fiep}, lp = ListPlot[l, DisplayFunction -> Identity, PlotStyle -> PointSize[0.015], PlotRange -> All]; If[stop===default, stop = Length[l]]; 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]] ] Or, use one Module round the lot. A less secure way is to use the string "default" instead of the symbol default (this can be affected by replacement - "default" -> Throw. But then we can make a mess of anything with injudicious use of replacement. Allan --------------------- Allan Hayes Mathematica Training and Consulting Leicester UK www.haystack.demon.co.uk hay at haystack.demon.co.uk Voice: +44 (0)116 271 4198 Fax: +44 (0)870 164 0565 <Maarten.vanderBurgt at icos.be> wrote in message news:7fp3um$31l at smc.vnet.net... > Alan, > > Thanks for your help. > Bob Hanlon gave me a simalar answer. > Another approach I found in R. Maeder's book: > > ListPlotFit[l_, start_:1, stop_:default] := > Module[{lp, fie, fiep}, > lp = ListPlot[l, DisplayFunction -> Identity, PlotStyle -> > PointSize[0.015], PlotRange -> All]; > If[stop===default, stop = Length[l]]; > 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]] > > Maarten > > > > > > "Allan Hayes" <hay at haystack.demon.co.uk> on 19-04-99 05:53:52 PM > > cc: > > Subject: [mg17231] [mg17231] Re: ListPlotFit > > > > > Marrten, > > ListPlotFit[l_, start_:1, stop_] := > 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] > ] > > ListPlotFit[l_, start_:1]:= > ListPlotFit[l, start, Length[l]] > > Allan > > --------------------- > Allan Hayes > Mathematica Training and Consulting > Leicester UK > www.haystack.demon.co.uk > hay at haystack.demon.co.uk > Voice: +44 (0)116 271 4198 > Fax: +44 (0)870 164 0565 > > > <Maarten.vanderBurgt at icos.be> wrote in message > news:7fbnsj$957 at smc.vnet.net... > > Hello, > > > > > > 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]; > > > > > > Any help is greatly appreciated > > thanks > > > > Maarten > > > > > > _______________________________________________________________ > > > > Maarten van der Burgt > > R&D Department > > > > ICOS Vision Systems > > Esperantolaan 9 > > B-3001 Leuven, Belgium > > tel. + 32 16 398220; direct + 32 16 398316; fax. + 32 16 400067 > > e-mail: maarten.vanderburgt at icos.be > > _______________________________________________________________ > > > > > > > > > > > > > > > >