gnuplot使用半圆弧函数绘制圆形出现缺口问题求解
缺口出现原因
你拆分上下半圆的显式函数写法本身逻辑正确,出现缺口的核心原因是gnuplot默认对显式函数的采样点数仅为100,在[-10,10]的宽x范围下,采样点刚好没有命中上下半圆有效定义域的边界x=-4和x=6,边界处的无效值(根号内为负)会被gnuplot直接丢弃,导致两个半圆的端点无法衔接,就出现了缺口。
基于现有两个半圆弧函数的修复方案
有两种简单修改方式即可修复缺口:
方案1:增加函数采样点数
在plot命令前增加set samples 1000的配置,更高的采样密度可以让采样点命中有效定义域的边界,让两个半圆端点衔接。
修改后完整代码:
reset set terminal svg enhanced fname 'Times New Roman' rounded dashed standalone set size ratio 1.0 set style line 1 linecolor rgb '#0060ad' linetype 1 linewidth 1 # blue set style line 2 linecolor rgb '#dd181f' linetype 1 linewidth 1 # red set xrange[-10:10] set yrange[-10:10] set xzeroaxis linetype 2 linewidth 1 set yzeroaxis linetype 2 linewidth 1 set grid x,y front #set tics scale 0.75 set xtics 1 set ytics 1 set label at 1,3 point pt 7 ps 0.4 f(x) = -sqrt(25-(x-1)**2)+3 g(x) = sqrt(25-(x-1)**2)+3 set samples 1000 # 新增行,增加采样点数 plot f(x) with lines ls 1, \ g(x) with lines ls 2
方案2:限制绘图的x范围为半圆的有效定义域
在plot的时候指定x范围为[-4:6],刚好是该圆的x坐标有效区间,所有采样点都落在有效值范围内,不会出现端点被丢弃的情况:
修改后plot部分代码:
plot [-4:6] f(x) with lines ls 1, \ g(x) with lines ls 2
其他可选实现方案
如果不需要强制拆分为上下两个半圆弧函数,可以用更稳定的参数方程或者内置圆对象实现:
- 参数方程方案:
reset set terminal svg enhanced fname 'Times New Roman' rounded dashed standalone set size ratio 1.0 set style line 1 linecolor rgb '#0060ad' linetype 1 linewidth 1 set xrange[-10:10] set yrange[-10:10] set xzeroaxis linetype 2 linewidth 1 set yzeroaxis linetype 2 linewidth 1 set grid x,y front set xtics 1 set ytics 1 set label at 1,3 point pt 7 ps 0.4 set parametric # 开启参数方程模式 set trange [0:2*pi] x(t) = 1 + 5*cos(t) y(t) = 3 + 5*sin(t) plot x(t), y(t) with lines ls 1
- 内置圆对象方案:
set object 1 circle at 1,3 radius 5 fc none lw 1 lc rgb '#0060ad' plot NaN # 只显示对象,不需要绘制函数
内容的提问来源于stack exchange,提问作者147pm
相关产品推荐
相关产品推荐

