gnuplot / intro / plotcalc
gnuplot 入門 — 数値計算編まずはデータファイルの準備 計算結果をプロットするためには,結果をファイルに落しておく必要が #include <stdio.h> #include <math.h> int main(void); int main(void) { int i; double x,y,z1,z2,d; d = 0.1; x = 0.0; for(i=0;i<50;i++){ x += d; y = exp(-x); z1 = (6 - 2*x)/(6 + 4*x + x*x); z2 = (6 - 4*x + x*x)/(6 + 2*x); printf("% 6.2f % 11.4e % 11.4e % 11.4en", x,y,z1,z2); } return 0; } INTEGER I REAL*8 X,Y,Z1,Z2,D D = 0.1 X = 0.0 DO 10 I=1,50 X = X+D; Y = EXP(-X) Z1 = (6 - 2*X)/(6 + 4*X + X*X) Z2 = (6 - 4*X + X*X)/(6 + 2*X) WRITE(6,20) X,Y,Z1,Z2 10 CONTINUE 20 FORMAT(F6.2,3(1X,1PE11.4)) STOP END このプログラムの出力の最初と最後の部分は,次のようになります.左端 0.10 9.0484E-01 9.0484E-01 9.0484E-01 0.20 8.1873E-01 8.1871E-01 8.1875E-01 0.30 7.4082E-01 7.4074E-01 7.4091E-01 0.40 6.7032E-01 6.7010E-01 6.7059E-01 0.50 6.0653E-01 6.0606E-01 6.0714E-01 .... 4.60 1.0052E-02 -7.0237E-02 5.7632E-01 4.70 9.0953E-03 -7.2510E-02 6.0325E-01 4.80 8.2297E-03 -7.4627E-02 6.3077E-01 4.90 7.4466E-03 -7.6597E-02 6.5886E-01 5.00 6.7379E-03 -7.8431E-02 6.8750E-01 (X,Y)の組をプロット このデータをプロットしましょう.計算結果の出力をファイルに書き出し gnuplot> plot "output.dat" using 1:2 with lines ![]() 上の図は,説明をしやすい様にウィンドウを少し小さくしています. プロットするカラムは using で指定します.using X:Y の様にコロンの プロットのスタイルには,点を打つ,記号を描く,線で結ぶ,階段状の線
同時に複数の線を引く 次に,残りの2つのカラムを同時にプロットしてみます.複数の線を同時 gnuplot> plot "output.dat" using 1:2 with lines, > "output.dat" using 1:3 with lines, > "output.dat" using 1:4 with lines ![]() 2番目の緑色の線で描かれている数値がXの大きな所で負になるので,Y軸の 図の凡例には,プロットしたデータファイル名と使ったカラムが表示されて gnuplot> plot "output.dat" using 1:2 title "Analytical" with lines, > "output.dat" using 1:3 title "L=1, M=2" with lines, > "output.dat" using 1:4 title "L=2, M=1" with lines ![]() グラフのタイトルと軸名を入れる 次にXとY軸の名前を入れます.X軸には “x”を書き,Y軸には”y=exp(-x)” gnuplot> set xlabel "x" gnuplot> set ylabel "y=exp(-x)" gnuplot> set title "Pade approximation" gnuplot> replot ![]() 図の大きさが少し変わったのが分かりますか.gnuplotは全体が枠内(今の Y軸名の”y=exp(-x)”は,Y軸の横に入らず上に書かれています.これはXウィ XYの範囲を変更する 今度は,X軸とY軸の範囲を変更します.Y軸の範囲は-0.1から1になっています. gnuplot> set xrange [0:2] gnuplot> set yrange [0:1] gnuplot> replot ![]() 軸に目盛をいれる X軸の目盛は0から始まって0.5刻みで増えています.これを1刻みにし,その 目盛は set {x|y}tics で変えます. set xtics 1 のよう 小さい目盛は,set m{x|y}tics で,大きい目盛を何分割するかを与えま gnuplot> set xtics 1 gnuplot> set mxtics 5 gnuplot> set ytics 0.5 gnuplot> set mytics 5 gnuplot> replot 完成 これで完成です.結果をPostScriptにして,印刷してみましょう. gnuplot> set term postscript gnuplot> set output "output.ps" gnuplot> replot gnuplot> save "output.plt" gnuplot> quit 出来上がったoutput.psをPostScriptプリンタに送れば,印刷できます. ![]() 縮小しているので見えにくいとは思いますが,ウィンドウでは赤い線だっ gnuplot> set term x11 gnuplot> test ![]() |