もう少し複雑な,
setコマンドを駆使した例をリスト
4に示す.
1 #include <stdio.h>
2 #include <math.h>
3 void mk_triangle_data(char *a, double x1, double x2, int n);
4 void mk_graph(char *f, char *xlb, double x1, double x2,
5 char *ylb, double y1, double y2);
6
7 /*==========================================================*/
8 /* main function */
9 /*==========================================================*/
10 int main(void){
11
12 double pi = 4*atan(1);
13
14 mk_triangle_data("out.txt", -2*pi, 2*pi, 1000);
15 mk_graph("out.txt", "x", -2*pi, 2*pi, "y", -3, 3);
16
17 return 0;
18 }
19
20 /*==========================================================*/
21 /* make a data file */
22 /*==========================================================*/
23 void mk_triangle_data(char *a, double x1, double x2, int n){
24 double x, dx;
25 double y1, y2, y3;
26 int i;
27 FILE *out;
28
29 dx = (x2-x1)/n;
30
31 out = fopen(a, "w");
32
33 for(i=0; i<=n; i++){
34 x = x1+dx*i;
35 y1 = sin(x);
36 y2 = cos(x);
37 y3 = tan(x);
38
39 fprintf(out, "%e\t%e\t%e\t%e\n", x, y1, y2, y3);
40 }
41
42 fclose(out);
43 }
44
45 /*==========================================================*/
46 /* make a graph */
47 /*==========================================================*/
48 void mk_graph(char *f, char *xlb, double x1, double x2,
49 char *ylb, double y1, double y2)
50 {
51
52 FILE *gp;
53
54 gp = popen("gnuplot -persist","w");
55
56 fprintf(gp, "reset\n");
57
58 /* ------- set x grid ---------*/
59
60 fprintf(gp, "set grid\n");
61
62 /* ------- set x axis ---------*/
63
64 fprintf(gp, "set xtics 1\n");
65 fprintf(gp, "set mxtics 10\n");
66 fprintf(gp, "set xlabel \"%s\"\n", xlb);
67 fprintf(gp, "set nologscale x\n");
68 fprintf(gp, "set xrange[%e:%e]\n", x1, x2);
69
70 /* ------- set y axis ---------*/
71
72 fprintf(gp, "set ytics 1\n");
73 fprintf(gp, "set mytics 10\n");
74 fprintf(gp, "set ylabel \"%s\"\n", ylb);
75 fprintf(gp, "set nologscale y\n");
76 fprintf(gp, "set yrange[%e:%e]\n", y1, y2);
77
78 /* ------- plat graphs ---------*/
79
80 fprintf(gp, "set terminal x11\n");
81
82 fprintf(gp, "plot \"%s\" using 1:2 with line,\
83 \"%s\" using 1:3 with line,\
84 \"%s\" using 1:4 with line\n", f, f, f);
85
86 fprintf(gp, "set terminal emf\n");
87 fprintf(gp, "set output \"tri.emf\"\n");
88
89 fprintf(gp, "replot\n");
90
91 pclose(gp);
92 }
ホームページ:
Yamamoto's laboratory著者:
山本昌志
Yamamoto Masashi
平成18年7月4日