Search code examples
gnuplot

gnuplot: cap f(x) at integer after certain value passed


I'm plotting a function with gnuplot:

round(x) = x - floor(x) < 0.5 ? floor(x) : ceil(x)
f(x) =  round((sqrt(((x * 0.4)+1.0)) - 1.0) * 10)

set terminal svg size 1920,1080
set output 'testplot.svg'
plot f(x) title 'testplot'

After this reaches an x-value of 300, the y-value will be 100. Any x-value greater than 300 should be capped at y=100. I'm not sure how to accomplish this in gnuplot; I tried using if statements but because x isn't defined until the plot command is run, I was getting undefined variable x errors.

I know I could simply create a table with these values, but this is a problem I'd rather learn a gnuplot solution for, if such a thing exists.

Any tips are greatly appreciated!


Solution

  • Use the ternary operator:

    set xrange [0:1000]
    set yrange [0:150]
    capF(x) = (x > 300) ? 100. : f(x)
    plot capF(x) title "f(x) capped at y=100 for x>300"
    

    enter image description here