Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Hi I'm absolutely newbie in Mathematica and have following problem:

My list looks like {{50, 0.75}, {51, 0.76}, ..., and I want to choose a range out of my x-values (I don't want so say: from point 150 to ..., I want to choose with values like from x value 50 to 60), with only these points a linear regression has to be done.

I tried with LinearModelFit[data, x, x], and Mathematica made a linear regression with all values, now I want to choose a range but I can't find a solution =(

Has somebody an idea?

share|improve this question
4  
You can use Select to filter your data. –  b.gatessucks May 9 '13 at 18:43
add comment

2 Answers 2

up vote 6 down vote accepted

As suggested by @b.gatessucks you can use Select.

data = Table[{x, Sin[x] + RandomReal[{-0.5, 0.5}]}, {x, 0, 15, 0.25}];
pl = ListPlot[data];
ff = LinearModelFit[data, Sin[x], x]
Show[pl, Plot[ff[x], {x, 0, 15}]];

and after selection over the range [2,8]:

ff2 = LinearModelFit[Select[data, #[[1]] > 2 && #[[1]] < 8 &], Sin[x], x]
Show[pl, Plot[{ff[x],ff2[x]}, {x, 0, 15}]];

share|improve this answer
    
Thanks a lot, I will try it ;) –  Jacccy May 9 '13 at 19:33
1  
The Select[] looks a bit nicer if you use a compound inequality: 2 < #[[1]] < 8 &. –  J. M. May 10 '13 at 1:54
add comment

You can dynamically select the desired data range by using locators:

data = Table[{x, x x }, {x, 0, 100}]; 
Manipulate[((p1 = Sort[p][[All, 1]]; 
             lm = LinearModelFit[Select[data, (p1[[1]] < #[[1]] < p1[[2]] &)], x, x];
             Show[ListPlot[data, PlotRange -> 10^4 {-1, 1}], 
                      Plot[lm[x], {x, 0, 100},  PlotStyle -> Red]]), 
{{p, {{1, 0}, {100, 0}}}, {1, 0}, {100, 0}, Locator}]

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.