Kodama's home / tips.
次の6行のスクリプトを実行すると, y=x**2 と y=x**3 のグラフが 5秒間ずつ表示される.
#!/usr/local/bin/ruby gp=IO.popen("gnuplot","w"); gp.puts 'plot x**2'; sleep 5; gp.puts 'plot [-2:2] x**3'; sleep 5; gp.close;'plot x**2' の部分の文字列は "x**2 のグラフをプロットしろ" という gnuplot のコマンドだ. 'plot [-2:2] x**3' の "[-2:2]" の部分は, x軸の範囲を指定する書き方だ.
gp.puts 'コマンド文字列' のように書き出すことで, gnuplot を通じてグラフを描くことができる. 使えるコマンドに関しては gnuplot を調べてもらいたい.
多項式クラスで計算した結果をこの方法で表示するには, 多項式を文字列に直すときに gnuplot に適合した形式にする必要がある. それには to_s("prog") を使う.
#!/usr/local/bin/ruby require "polynomial" x=Poly("x"); p=(x+1)*(x-2); gp=IO.popen("gnuplot","w"); gp.puts 'plot [-1:3] '+p.to_s("prog"); sleep 5; gp.close;
Kodama's home / tips.