gnuplot で楕円や回転楕円体を描く

理工系の数学 B の授業で,楕円の周長や面積,回転楕円体の表面積や体積を求めているので。

Jupyter Notebook における size 設定

In [1]:
%gnuplot inline svg size 640,384 fixed enhanced font ',14'

塗りつぶした楕円

gnuplot で長半径 $a$, 短半径 $b$ の楕円を描く例。

$$\frac{x^2}{a^2} + \frac{y^2}{b^2} = 1, \quad \therefore\ \
y = \pm b \sqrt{1 – \frac{x^2}{a^2}} \equiv \pm f(x)$$

塗りつぶしがなければ,媒介変数表示での plot がシンプルだが,ここでは2本の陽関数で挟まれた領域を塗りつぶすので $y = f(x)$ を使う。

In [2]:
reset
a = 2
b = 1
f(x) = b*sqrt(1 - x**2/a**2)

alim = a + 0.5
blim = b + 0.5

# x, y の表示範囲
set xrange [-alim:alim]
set yrange [-blim:blim]
# x, y の目盛の長さを同じに
set size ratio -1

# x 軸 y 軸の表示
set zeroaxis
set grid front
# 目盛設定
set xtics ("-a" -a, "0" 0, "a" a)
set ytics ("-b" -b, "0" 0, "b" b)

# 楕円の端がカクカクしないように
set samples 500

# set xrange と異なる範囲でグラフを描く際は sample と唱える
# y = f(x) と y = -f(x) の間を塗りつぶす
plot sample \
  [-a:a] "+" u 1:(f($1)):(-f($1)) with filledc fc "yellow" notitle, \
  [-a:a] f(x) lw 3 lc "dark-blue" notitle,\
  [-a:a] -f(x) lw 3 lc "dark-blue" notitle

塗りつぶし無しの楕円

In [3]:
plot sample  \
  [-a:a] f(x) lw 3 lc "dark-blue" notitle,\
  [-a:a] -f(x) lw 3 lc "dark-blue" notitle

長軸の周りに回転した回転楕円体

$\displaystyle y = f(x) = b \sqrt{1-\frac{x^2}{a^2}} $ を $x$ の周りに回転してできる回転楕円体の表面。

In [4]:
# set xrange と異なる範囲でグラフを描く際は sample と唱える
# y=f(x) と y=0 で挟まれた領域を塗りつぶす
plot sample [-a:a] f(x) with filledc y=0 fc "light-blue" notitle, \
            [-a:a] f(x) lw 3 lc "dark-blue" notitle, \
            [-alim:alim] 0 lw 2 lc "blue" notitle

$y = f(x)$ を $x$ 軸の周りに $\theta$ だけ回転させると,

\begin{eqnarray}
x &=& x \\
y &=& f(x) \cos\theta \\
z &=& f(x) \sin\theta
\end{eqnarray}

となることに留意。

In [5]:
%gnuplot inline svg size 640,640 fixed enhanced font ',14'
In [6]:
reset
set xrange [-alim:alim]
set yrange [-alim:alim]
set zrange [-alim:alim]

unset zeroaxis
unset xtics
unset ytics
unset ztics
unset border
set isosample 30
set hidden3d
set palette defined (0 "dark-blue", 1 "blue", 2 "cyan")
unset colorbox

set parametric
splot [x=-a:a] [th=0:2*pi] \
      x, f(x)*cos(th), f(x)*sin(th) notitle lc palette lw 0.5, \
      x, 0, f(x) notitle lc "dark-blue" lw 3, \
      x+a, 0, 0 notitle lc "blue" lw 2, \
      x-0.3, 0, 0 notitle lc "blue" lw 2

Out[6]:
	dummy variable is t for curves, u/v for surfaces

短軸の周りに回転した回転楕円体

$\displaystyle x = g(y) = a \sqrt{1-\frac{y^2}{b^2}} $ を $y$ の周りに回転してできる回転楕円体の表面。

In [7]:
%gnuplot inline svg size 640,384 fixed enhanced font ',14'
In [8]:
reset
# x, y の表示範囲
set xrange [-alim:alim]
set yrange [-blim:blim]
# x, y の目盛の長さを同じに
set size ratio -1

# x 軸 y 軸の表示
set zeroaxis
set grid front
# 目盛設定
set xtics ("-a" -a, "0" 0, "a" a)
set ytics ("-b" -b, "0" 0, "b" b)

# 楕円の端がカクカクしないように
set samples 300

# 短軸は plot f(x) では引けないので
set arrow 1 from 0,-blim to 0, blim nohead lw 2 lc "red" front

unset parametric
# set xrange と異なる範囲でグラフを描く際は sample と唱える
# y = f(x) と y = -f(x) の間を塗りつぶす
plot sample \
    [0:a] "+" u 1:(f($1)):(-f($1)) with filledc fc "light-pink" notitle, \
    [0:a] f(x) lw 3 lc "dark-red" notitle, \
    [0:a] -f(x) lw 3 lc "dark-red" notitle

In [9]:
# いったん定義した arrow はいつまでもつきまとうので...
unset arrow

$x = g(z)$ を $z$ 軸の周りに $\theta$ だけ回転させると,

\begin{eqnarray}
x &=& g(z) \cos\theta \\
y &=& g(z) \sin\theta \\
z &=& z
\end{eqnarray}

となることに留意。

In [10]:
%gnuplot inline svg size 640,640 fixed enhanced font ',14'
In [11]:
g(z) = a* sqrt(1-z**2/b**2) 

set xrange [-a:a]
set yrange [-a:a]
set zrange [-a:a]

unset zeroaxis
unset xtics
unset ytics
unset ztics
unset border
set isosample 40
set hidden3d
set palette defined (0 "dark-red", 1 "light-pink")
unset colorbox
set view 70,20

set parametric
splot [z=-b:b] [th=0:2*pi] \
      g(z)*cos(th), g(z)*sin(th), z notitle lc palette lw 0.5, \
      g(z), 0, z notitle lc "dark-red" lw 3, \
      0, 0, z+b notitle lc "red" lw 2, \
      0, 0, z-1 notitle lc "red" lw 2

Out[11]:
	dummy variable is t for curves, u/v for surfaces