質問

I have a periodic system and I want to fit some data for this system to a function, but I want to constrain to a range. Usually, if your range is from x_min to x_max this can be achieved with fit [x_min:x_max] f(x) "data" via a,b,c,.... However, because my system is periodic, I want to constrain to a range that contains a segment at the end of the array of x values and also at the beginning. That is, if x values are given within the interval [0:L], I want to include in the fit both [0:x_max] and [x_min:L], where in this case x_max < x_min. How can this be done?

役に立ちましたか?

解決

How about this?

fit [min1:max2] f(x) "data" u 1:($1 <= max1 || $1 >= min2 ? $2 : 1/0) via a,b,c,...

That is, make use of the [:] for the outer range.

I'm not sure it's much nicer but it is a little bit shorter.

Alternatively, you could use set table in the following way:

set table 'newdata'
p [0:x_max] 'data'
p [x_min:L] 'data'
unset table

fit f(x) 'newdata' via a,b,c,...

Basically this writes a new file containing only the data between the two ranges. Arguably nicer, definitely not shorter!

他のヒント

I figured an ugly way to do it, but if anyone comes up with a nicer way I will accept that as the answer.

Imagine the two ranges where you want to fit (or plot) the data are given by [min1:max1] and [min2:max2], then using a conditional fit and the "ignore" function 1/0 you can do the following:

fit f(x) "data" u 1:( ($1 >= min1 && $1 <= max1) || ($1 >= min2 && $1 <= max2) ? $2 : 1/0) via a,b,c,...

That is, if x is within the first interval or the second one, then use y as a value for the fit, otherwise ignore those data points.

This approach should also work with plot, but I think something like a composite range operator [min1:max1 && min2:max2] (which does not work) would be more desirable.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top