Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Fit data with range

610 views
Skip to first unread message

Ivan

unread,
May 3, 2008, 6:15:03 AM5/3/08
to
Hi

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}]

thanks!

Jean-Marc Gulliet

unread,
May 5, 2008, 6:11:28 AM5/5/08
to
Ivan wrote:

You could use *Select[]* to massage your data. For instance,

data = {{0, 1}, {1, 0}, {3, 2}, {5, 4}, {7, 8}, {9, 11}};
xmin = 1; xmax = 7;
pts = Select[data, xmin <= #[[1]] <= xmax &]
fit = Fit[pts, {1, x, x^2}, x]
Show[ListPlot[pts, PlotStyle -> {Red, PointSize -> Large}],
Plot[fit, {x, xmin, xmax}, PlotStyle -> {Blue, Thick}]]

{{1, 0}, {3, 2}, {5, 4}, {7, 8}}


2
-0.325 + 0.3 x + 0.125 x

Regards,
-- Jean-Marc

Bob Hanlon

unread,
May 5, 2008, 6:13:02 AM5/5/08
to
data = Table[{x, x^2 - 11 x + 24 + 6 RandomReal[]}, {x, 0, 10, .25}];

pltRng = {{0, 10}, 1.2 {Min[data[[All, 2]]], Max[data[[All, 2]]]}};

fit = Fit[data, {1, x, x^2}, x]

xmin = 4; xmax = 7;

fit2 = Fit[Select[data, xmin <= #[[1]] <= xmax &], {1, x, x^2}, x]

Plot[{fit, fit2}, {x, 0, 10},
PlotStyle -> {Green, Blue},
Epilog -> {Red, Point[data]},
PlotRange -> pltRng]


Bob Hanlon

---- Ivan <dark...@gmail.com> wrote:
> Hi


>
> 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}]
>
>
>

> thanks!
>


Bill Rowe

unread,
May 5, 2008, 6:14:48 AM5/5/08
to
On 5/3/08 at 6:15 AM, dark...@gmail.com (Ivan) wrote:

>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]

Simply select just the points between xmin and xmax then do the
fitting. Assuming your data set consists of {x,y} pairs, then either:

Cases[data,{_?(xmin < # < xmax&),_}]

or

Select[data, xmin < First[#] < xmax&]

will do the selection

Note, in general it isn't a good idea to use powers of a single
variable as a set of basis functions. This will be numerically
unstable in general, becoming increasingly unstable for higher powers.

There are packages available on Wolfram's web site for doing
polynomial fits that avoid this problem.

David Annetts

unread,
May 5, 2008, 6:15:21 AM5/5/08
to
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


Szabolcs Horvát

unread,
May 5, 2008, 6:17:28 AM5/5/08
to

Filter the data with Select or Case! The exact command to use depends
on the shape of the data. E.g. Case[data, {x_, _} /; xmin <= x <= xmax]

Jens-Peer Kuska

unread,
May 5, 2008, 6:18:41 AM5/5/08
to
Hi,

fit = Fit[Select[data,(xmin<=First[#1]<=xmax) &],{1,x,x^2},x]

??

Regards
Jens

0 new messages