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!
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
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!
>
>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.
> 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
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]
fit = Fit[Select[data,(xmin<=First[#1]<=xmax) &],{1,x,x^2},x]
??
Regards
Jens