PGPLOTでグラフを描いてみる
をテンプレートにして作成
home
>
サイトマップ
開始行:
インストールが無事終了したら,簡単なグラフを表示させてみ...
#contents
* 描きたいグラフ [#k97ebe40]
ここで描くのは中学数学でおなじみの
#ref(eq1.png,nolink)
という2次関数(y = x^2)のグラフです.できあがりは
#ref(fig1.png,nolink)
になります.では行ってみましょう.
* グラフ描画の手順 [#cd7d137d]
PGPLOTがグラフを描く手順はおおまかに
+ デバイスオープン
+ 大きさ,縦横比,座標軸の決定
+ 線種,線色,線の太さの決定
+ ペン初期位置の決定
+ ペンから指定した座標まで線を描く
+ 手順4から5を繰り返す
+ デバイスクローズ
です.PGPLOTライブラリの提供する関数名は cpg からはじまっ...
* インクルード文 [#yb71d567]
ソースを書いて行きます.最初に書くのはインクルード文です.
#include <math.h>
#include "cpgplot.h"
数学関数を使うための <math.h> をインクルードし, PGPLOT ...
* main文 [#y062c11f]
プログラムの入口であるmain文を書きます.
#include <math.h>
#include "cpgplot.h"
int main(void){
return 0;
}
特に引数を与える必要はないので,main関数の引数は void と...
* デバイスオープン [#ff31bfa5]
いよいよ PGPLOT の関数を使って行きます.最初にやるのはデ...
#include <math.h>
#include "cpgplot.h"
int main(void){
cpgopen("/xserv");
cpgclos();
return 0;
}
cpgclos は開いたデバイスを閉じる命令です.オープンしたら...
* 大きさ,縦横比,座標軸 [#k89c7fea]
ウィンドウの大きさと縦横比を決める命令が cpgpap です.第1...
座標軸を決める命令は cpgenv で,第1引数にx座標の最小値,...
#include <math.h>
#include "cpgplot.h"
int main(void){
cpgopen("/xserv");
cpgpap(5.0, 1.0);
cpgenv(0, 5, 0, 5, 0, 1);
cpgclos();
return 0;
}
ここでコンパイル,実行するとつぎのグラフになります.
#ref(fig2.png,nolink)
* ためしに線を引く [#w3a16416]
PGPLOT で線を引く際「ペン」という概念があります.ペンをあ...
原点 (0, 0) から (1, 1) に線を引く場合,つぎのようにしま...
#include <math.h>
#include "cpgplot.h"
int main(void){
cpgopen("/xserv");
cpgpap(5.0, 1.0);
cpgenv(0, 5, 0, 5, 0, 1);
cpgmove(0, 0);
cpgdraw(1, 1);
cpgclos();
return 0;
}
ここでコンパイル,実行するとつぎのグラフになります.
#ref(fig3.png,nolink)
しかしこうやっていちいちペン位置を指定していたのでは大変...
* 関数 [#te3219f0]
&ref(eq1.png); の関数を用意します.関数名は function とで...
#include <math.h>
#include "cpgplot.h"
double function(double x){
return pow(x,2);
}
int main(void){
cpgopen("/xserv");
cpgpap(5.0, 1.0);
cpgenv(0, 5, 0, 5, 0, 1);
cpgclos();
return 0;
}
数学関数 pow を使うとこんな感じになります.さっき書いた c...
* 関数を描く [#fc29a539]
上で作った関数をfor文で呼び出し,0.1刻みで線を描きます.
#include <math.h>
#include "cpgplot.h"
double function(double x){
return pow(x,2);
}
int main(void){
double x;
cpgopen("/xserv");
cpgpap(5.0, 1.0);
cpgenv(0, 5, 0, 5, 0, 1);
cpgmove(0, function(0));
for (x=0.1; x<=5; x+=0.1) {
cpgdraw(x, function(x));
}
cpgclos();
return 0;
}
こんな感じです.まず cpgmove(0, function(0)); で原点にペ...
#ref(fig4.png,nolink)
というグラフができます.関数のグラフ自体はこれで完成です.
* 軸にラベルを付ける [#yf34da31]
グラフの横軸,縦軸には何を表すのかというラベルを付ける必...
#include <math.h>
#include "cpgplot.h"
double function(double x){
return pow(x,2);
}
int main(void){
double x;
cpgopen("/xserv");
cpgpap(5.0, 1.0);
cpgenv(0, 5, 0, 5, 0, 1);
cpglab("x","y","y=x*x");
cpgmove(0, function(0));
for (x=0.1; x<=5; x+=0.1) {
cpgdraw(x, function(x));
}
cpgclos();
return 0;
}
コンパイル,実行すると
#ref(fig5.png,nolink)
となり,ラベルが付きました.
* 線の色の変更 [#e3fc6d2e]
線色を変更してみます.命令は cpgsci です.引数には1つの整...
#include <math.h>
#include "cpgplot.h"
double function(double x){
return pow(x,2);
}
int main(void){
double x;
cpgopen("/xserv");
cpgpap(5.0, 1.0);
cpgenv(0, 5, 0, 5, 0, 1);
cpglab("x","y","y=x*x");
cpgsci(2);
cpgmove(0, function(0));
for (x=0.1; x<=5; x+=0.1) {
cpgdraw(x, function(x));
}
cpgclos();
return 0;
}
コンパイル,実行すると
#ref(fig1.png,nolink)
です.これで完成としておきましょう.
- &ref(plot1.c);
- &ref(Makefile);
終了行:
インストールが無事終了したら,簡単なグラフを表示させてみ...
#contents
* 描きたいグラフ [#k97ebe40]
ここで描くのは中学数学でおなじみの
#ref(eq1.png,nolink)
という2次関数(y = x^2)のグラフです.できあがりは
#ref(fig1.png,nolink)
になります.では行ってみましょう.
* グラフ描画の手順 [#cd7d137d]
PGPLOTがグラフを描く手順はおおまかに
+ デバイスオープン
+ 大きさ,縦横比,座標軸の決定
+ 線種,線色,線の太さの決定
+ ペン初期位置の決定
+ ペンから指定した座標まで線を描く
+ 手順4から5を繰り返す
+ デバイスクローズ
です.PGPLOTライブラリの提供する関数名は cpg からはじまっ...
* インクルード文 [#yb71d567]
ソースを書いて行きます.最初に書くのはインクルード文です.
#include <math.h>
#include "cpgplot.h"
数学関数を使うための <math.h> をインクルードし, PGPLOT ...
* main文 [#y062c11f]
プログラムの入口であるmain文を書きます.
#include <math.h>
#include "cpgplot.h"
int main(void){
return 0;
}
特に引数を与える必要はないので,main関数の引数は void と...
* デバイスオープン [#ff31bfa5]
いよいよ PGPLOT の関数を使って行きます.最初にやるのはデ...
#include <math.h>
#include "cpgplot.h"
int main(void){
cpgopen("/xserv");
cpgclos();
return 0;
}
cpgclos は開いたデバイスを閉じる命令です.オープンしたら...
* 大きさ,縦横比,座標軸 [#k89c7fea]
ウィンドウの大きさと縦横比を決める命令が cpgpap です.第1...
座標軸を決める命令は cpgenv で,第1引数にx座標の最小値,...
#include <math.h>
#include "cpgplot.h"
int main(void){
cpgopen("/xserv");
cpgpap(5.0, 1.0);
cpgenv(0, 5, 0, 5, 0, 1);
cpgclos();
return 0;
}
ここでコンパイル,実行するとつぎのグラフになります.
#ref(fig2.png,nolink)
* ためしに線を引く [#w3a16416]
PGPLOT で線を引く際「ペン」という概念があります.ペンをあ...
原点 (0, 0) から (1, 1) に線を引く場合,つぎのようにしま...
#include <math.h>
#include "cpgplot.h"
int main(void){
cpgopen("/xserv");
cpgpap(5.0, 1.0);
cpgenv(0, 5, 0, 5, 0, 1);
cpgmove(0, 0);
cpgdraw(1, 1);
cpgclos();
return 0;
}
ここでコンパイル,実行するとつぎのグラフになります.
#ref(fig3.png,nolink)
しかしこうやっていちいちペン位置を指定していたのでは大変...
* 関数 [#te3219f0]
&ref(eq1.png); の関数を用意します.関数名は function とで...
#include <math.h>
#include "cpgplot.h"
double function(double x){
return pow(x,2);
}
int main(void){
cpgopen("/xserv");
cpgpap(5.0, 1.0);
cpgenv(0, 5, 0, 5, 0, 1);
cpgclos();
return 0;
}
数学関数 pow を使うとこんな感じになります.さっき書いた c...
* 関数を描く [#fc29a539]
上で作った関数をfor文で呼び出し,0.1刻みで線を描きます.
#include <math.h>
#include "cpgplot.h"
double function(double x){
return pow(x,2);
}
int main(void){
double x;
cpgopen("/xserv");
cpgpap(5.0, 1.0);
cpgenv(0, 5, 0, 5, 0, 1);
cpgmove(0, function(0));
for (x=0.1; x<=5; x+=0.1) {
cpgdraw(x, function(x));
}
cpgclos();
return 0;
}
こんな感じです.まず cpgmove(0, function(0)); で原点にペ...
#ref(fig4.png,nolink)
というグラフができます.関数のグラフ自体はこれで完成です.
* 軸にラベルを付ける [#yf34da31]
グラフの横軸,縦軸には何を表すのかというラベルを付ける必...
#include <math.h>
#include "cpgplot.h"
double function(double x){
return pow(x,2);
}
int main(void){
double x;
cpgopen("/xserv");
cpgpap(5.0, 1.0);
cpgenv(0, 5, 0, 5, 0, 1);
cpglab("x","y","y=x*x");
cpgmove(0, function(0));
for (x=0.1; x<=5; x+=0.1) {
cpgdraw(x, function(x));
}
cpgclos();
return 0;
}
コンパイル,実行すると
#ref(fig5.png,nolink)
となり,ラベルが付きました.
* 線の色の変更 [#e3fc6d2e]
線色を変更してみます.命令は cpgsci です.引数には1つの整...
#include <math.h>
#include "cpgplot.h"
double function(double x){
return pow(x,2);
}
int main(void){
double x;
cpgopen("/xserv");
cpgpap(5.0, 1.0);
cpgenv(0, 5, 0, 5, 0, 1);
cpglab("x","y","y=x*x");
cpgsci(2);
cpgmove(0, function(0));
for (x=0.1; x<=5; x+=0.1) {
cpgdraw(x, function(x));
}
cpgclos();
return 0;
}
コンパイル,実行すると
#ref(fig1.png,nolink)
です.これで完成としておきましょう.
- &ref(plot1.c);
- &ref(Makefile);
ページ名:
home
>
Modified by
物理のかぎプロジェクト
PukiWiki 1.4.5_1
Copyright © 2001-2005
PukiWiki Developers Team
. License is
GPL
.
Based on "PukiWiki" 1.3 by
yu-ji
Powered by PHP 5.3.29HTML convert time to 0.002 sec.