Search code examples
gnuplot

Semicircles from semicircle functions have gaps


I've got this code

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
plot f(x) with lines ls 1, \
     g(x) with lines ls 2

which produces enter image description here

Not sure why it didn't complete the whole semicircle, i.e., why the gaps? It's just an analytic geometry attempt to create a circle of radius 5 centered at (1,3). Other approaches welcome, but I'd prefer direct from these functions.


Solution

  • Gnuplot draws a function by sampling at a fixed number of x value across the current range of x. The default is samples=100. Your x range is [-10:10], so that means that each function is sampled at intervals of 0.1 along x.

    Actually it's worse than that because the 100 samples includes the endpoints, so the samples are actually taken at multiples of 20./99, which means there isn't a sample at exactly at +/-4.00000 where your curves would meet. You can fix that by set samples 101. The curves will then meet, but it will not produce a smooth circle because the both the upper and lower arcs will be sloping at the join rather than vertical.

    A better way to approach this is to take the sample around the circle rather than along the x axis, particularly since most of the samples along x do not even contribute. The syntax for doing this in gnuplot is:

    set angle degrees
    plot sample [t=0:360] '+' using (1+5*cos(t)):(3+5*sin(t)) with lines
    

    To draw the top arc and the bottom arc separately

    plot sample [t=0:180] '+' using (1+5*cos(t)):(3+5*sin(t)) with lines, \
                [t=180:360] '+' using (1+5*cos(t)):(3+5*sin(t)) with lines