MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Fit data with range

  • To: mathgroup at smc.vnet.net
  • Subject: [mg88423] RE: [mg88364] Fit data with range
  • From: "David Annetts" <davidannetts at aapt.net.au>
  • Date: Mon, 5 May 2008 06:12:06 -0400 (EDT)
  • References: <200805031015.GAA03918@smc.vnet.net>

Hi Ivan, 

> I want to fit a polynomial function to a set of data, only 
> between say xmin to xmax. How can I do that?
> I know only how to fit the whole range.
> 
> For example:
> 
> data = ReadList["file.dat",{Number,Number}];
> 
> fit = Fit[data,{1,x,x^2},x]
> 
> Plot[fit,{x,0,10}]

Select[] is probably the easiest way of isolating a subset of data.  Take[]
is very brute force.

Example:
data = {#,  # + #^2 +  1.1 RandomReal[]} & /@ Range[-5, 5, .25]; (* generate
data *)
{dmin, dmax} = {Min@data[[All, 1]], Max@data[[All, 1]]}; (* find Min & Max
coords *)
ListPlot[data] (* always plot experimental data before fitting *)

sdata = Select[data, (-dmin / 2 <= #[[1]] <= dmax /3) &]; (* select data
using criterion *)

sfit = Fit[sdata, {1, x, x^2}, x]   (* fit subset *)
afit = Fit[data, {1, x, x^2}, x]	(* fit all data -- slight
differences as expected *)
Show[{
  Plot[sfit, {x, dmin, dmax}],
  Plot[fit, {x, dmin, dmax}, PlotStyle -> Green],
  ListPlot[sdata, PlotStyle -> Red]
  }] (* compare both fits with experimental data *)

Unfortunately, to get an objective degree of fit, we still need a package.
Why this can't be an option to Fit[] is anyone's guess.  Perhaps in 6.1?

Needs["LinearRegression`"]
rfit = Regress[sdata, {1, x, x^2}, x]

You can see effects of fitting a subset using slightly different data eg.

data = {#,  (# RandomReal[]) + (# RandomReal[])^2 +  
      1.5 RandomReal[]} & /@ Range[-5, 5, .25];

Regards,

Dave.

No virus found in this outgoing message.
Checked by AVG. 
Version: 7.5.524 / Virus Database: 269.23.8/1412 - Release Date: 2/05/2008
16:34
 



  • Prev by Date: Re: list of dates
  • Next by Date: Re: Fit data with range
  • Previous by thread: Fit data with range
  • Next by thread: Re: Fit data with range