如何在gnuplot中对函数f(x)在x>300时设置y值上限为100?
解决方案
直接修改函数f(x),利用gnuplot原生的条件判断或内置函数限制最大值,无需额外生成数值表格,两种实现方式如下:
方式一:三元表达式判断x范围
round(x) = x - floor(x) < 0.5 ? floor(x) : ceil(x) f(x) = x > 300 ? 100 : 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'
方式二:使用min()函数取最小值
round(x) = x - floor(x) < 0.5 ? floor(x) : ceil(x) f(x) = min(round((sqrt(((x * 0.4)+1.0)) - 1.0) * 10), 100) set terminal svg size 1920,1080 set output 'testplot.svg' plot f(x) title 'testplot'
说明
gnuplot函数定义中可直接对参数x做条件判断,函数会在plot命令执行时逐点计算每个x对应的取值,不会出现“变量x未定义”的问题——此前的错误大概率是在函数外部使用了未定义的x,而非函数内部逻辑问题。
内容的提问来源于stack exchange,提问作者MrAureliusR
相关产品推荐
相关产品推荐

