• 検索結果がありません。

9 応用 : 関数のグラフ

N/A
N/A
Protected

Academic year: 2021

シェア "9 応用 : 関数のグラフ"

Copied!
3
0
0

読み込み中.... (全文を見る)

全文

(1)

応用

関数のグラフ   

繰り返しを使って,曲線のグラフを書いてみる. 

 

1. 

曲線は折れ線として近似する.例えば,関数f

(

x

を, 

0  ≤ x ≤  1 

の範囲で表示する には,x

= 0

, x

= 0

.

01

, x

= 0

.

02

, ・ ・ ・ のように, の値を少しずつ増やし ながら, f

(

x

を計算する.点列

(

xi, f

(

xi

)) 

を結ぶ折れ線が,関数f

(

x

の近似曲 線になっている. 

2. 

グラフを画面に表示するには,gnuplot

 

というツールを使う.これは,点列を読み込 むと,自動的に座標軸などをつけてグラフを書いてくれるツールである. 

3. 

作成するプログラムは,関数

f

(

x

に対して, xi, yの値を次々と計算して出力 するようにし,その出力をgnuplot

 

に入力する.プログラムは図

21

のようになる. 

4. graph.c 

で計算した結果は,そのまま実行すると画面に表示されるだけである。図

22

のように, ./a.out > ファイル名 という形で実行すると計算結果をファイルに保存 することができる。これをgnuplot

 を起動しplot

コマンドで表示する. 

(端末によってはグラフィック表示ができないかもしれない。その場合はgnuplotを起動後 に、 set term dumb というコマンドを入力してからplotコマンドを実行すると、テキ ストでグラフが表示される) 

 

̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲  

1 /* graph.c ‑‑ drawing a graph for sin function */ 

2 #include <stdio.h> 

3 #include <math.h> 

4 #define PI 3.1415926  5 int main(void) 

6 { 

7   double x = 0.0; 

8   while (x < 2*PI)  9   { 

10     printf("%e %e¥n", x, sin(x)); 

11     x += 0.1; 

12   }  13 } 

̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲  

21: graph.c   

 

 

 

(2)

̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲  

> gcc graph.c 

> ./a.out      計算結果は画面に表示されるだけで保存されない  0.000000e+00 0.000000e+00 

1.000000e‑01 9.983342e‑02  2.000000e‑01 1.986693e‑01  

・・・ 

> ./a.out > graph.dat    graph.datに結果が保存される 

> cat graph.dat   catコマンドでファイルの内容を表示し、出力結果が  保存されていることを確認する. 

0.000000e+00 0.000000e+00  1.000000e‑01 9.983342e‑02  2.000000e‑01 1.986693e‑01  

・・・ 

> gnuplot 

        G N U P L O T 

        Version 4.0 patchlevel 0 

        last modified Thu Apr 15 14:44:22 CEST 2004          System: Darwin 7.3.0 

 

        Copyright (C) 1986 ‑ 1993, 1998, 2004 

        Thomas Williams, Colin Kelley and many others   

        This is gnuplot version 4.0.  Please refer to the documentation          for command syntax changes.  The old syntax will be accepted          throughout the 4.0 series, but all save files use the new syntax. 

 

        Type ̀help̀ to access the on‑line reference manual. 

        The gnuplot FAQ is available from        http://www.gnuplot.info/faq/ 

 

        Send comments and requests for help to        <gnuplot‑[email protected]

        Send bugs, suggestions and mods to 

      <gnuplot‑[email protected]

   

Terminal type set to 'aqua' 

gnuplot>plot  graph.dat     グラフが画面に表示される. 

gnuplot>exit    gnuplotの終了 

̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲̲  

22: graph.c 

の実行例 

(3)

 

<参考:CSV ファイル形式による EXCEL への読み込み> 

 

graph.c において、 printf("%e %e¥n", x, sin(x)); の行を printf("%e, %e¥n", x,  sin(x)); と修正する(%e の間をカンマに変える)コンパイル後、 ./a.out > graph.csv として計算結果を保存する。ここで指定するファイル名は .csv で終わるものにする。

こうして作成したファイルは EXCEL に直接読み込むことが出来る(Finder からダブルクリ ックして見よ)。このように、値をカンマで区切って並べるファイル形式を CSV(Comma  Separated Value)と呼び、テキスト形式のデータファイルでよく利用される。 

参照

関連したドキュメント

[r]

THEOREM 4.1 Let X be a non-empty convex subset of the locally convex Hausdorff topological vector space E, T an upper hemicontinuous mapping of X into 2 E’, T(x) is a non-empty

(It is a standard convention to denote the unique line on two distinct collinear points x and y of a partial linear space by the symbol xy.) A linear space ðP ; LÞ with all lines

The construction of homogeneous statistical solutions in [VF1], [VF2] is based on Galerkin approximations of measures that are supported by divergence free periodic vector fields

Some of the known oscillation criteria are established by making use of a technique introduced by Kartsatos [5] where it is assumed that there exists a second derivative function

OFFI CI AL SCORE CERTI FI CATE GTEC (4技能) (CBT可). Test Repor t For m I ELTS™(Academi c

[r]

Q is contained in the graph of a