kuki
kuki

Reputation: 433

How to restrict yrange for fit in gnuplot

I used the following scripts for plotting and fitting.

Data set:

2.474   2.659
0.701   2.637
0.582   2.643
0.513   2.666
0.403   2.639
0.308   2.615
0.218   2.561
0.137   2.537

Script:

reset
set key bottom right
f(x) = a*atan(x/b); a = 2.65; b = 2.5 
fit f(x) 'test.txt' u 1:2 via a,b
plot 'test.txt' u 1:2 w p not, f(x) t 'f(x)' 

The plot looks like this:

enter image description here

I am trying to restrict it between min_y and max_y. The following intuitive code failed horribly,

fit [y=2.537:2.659] f(x) 'test.txt' u 1:2 via a,b

Any suggestion on restriction would be highly appreciated! Thanks!

Upvotes: 0

Views: 1497

Answers (3)

theozh
theozh

Reputation: 25734

You should be able also to do it simply by defining a fit range [][]. The following code works also with gnuplot 4.6 which was the version in 2014.

Update:

I guess it's better to give initial values for the fitting parameters a and b which are different from 0 . I noticed when starting the script again in a gnuplot session and a was close to 0 after the third fit. Then after restarting, the first two fits will be not be as expected. Furthermore, I noticed that this script will work for gnuplot versions >=4.6.0 and >5.0.0, but doesn't work for gnuplot 5.0.0 (I don't know why not).

Data: SO20878693.dat

1 2
2 3
3 4

1 9
2 8
3 7

Script: (works for gnuplot>=4.6.0, March 2012; but doesn't work for gnuplot=5.0.0)

### fit with limited y-range
reset

FILE = "SO20878693.dat"

f(x) = a*x + b

set xrange[0:10]
set yrange[0:10]
set key noautotitle

set multiplot layout 3,1

    a = 1
    b = 1
    fit [*:*][0:5] f(x) FILE u 1:2 via a,b
    plot FILE u 1:2 w p pt 7 lc rgb "red",\
        f(x) ti sprintf("Fitrange: [*:*][0:5]\nf(x) = %g*x + %g",a,b)

    a = 1
    b = 1
    fit [*:*][5:10] f(x) FILE u 1:2 via a,b
    plot FILE u 1:2 w p pt 7 lc rgb "red",\
        f(x) ti sprintf("Fitrange: [*:*][5:10]\nf(x) = %g*x + %g",a,b)

    a = 1
    b = 1
    fit [*:*][0:10] f(x) FILE u 1:2 via a,b
    plot FILE u 1:2 w p pt 7 lc rgb "red" not,\
        f(x) ti sprintf("Fitrange: [*:*][*:*]\nf(x) = %g*x + %g",a,b)

unset multiplot
### end of script

Result:

enter image description here

Upvotes: 2

Bob Hockney
Bob Hockney

Reputation: 1

This is an old question, but I arrived here looking for a solution to a similar problem. The answer is to use the stats command:

stats 'test.txt'

This will analyze, by default, the y data and set a bunch of STATS_* variables, which you can use in your fit statement along with the ternary operator:

fit f(x) 'test.txt' u 1:($2 >= STATS_min && $2 <= STATS_max ? $2 : NaN) via a,b

You can also add a using clause to the stats statement to further filter the data to match your fit statement, if needed.

Upvotes: -1

tabstop
tabstop

Reputation: 1761

The range option only specifies which input points should be used, not restricting the output. So far as I can see from the manual, restrictions on the output value of f(x) aren't really possible (and so far as I can see from the problem, not really desirable).

Upvotes: 0

Related Questions