#include #include void mk_triangle_data(char *a, double x1, double x2, int n); void mk_graph(char *f, char *xlb, double x1, double x2, char *ylb, double y1, double y2); /*==========================================================*/ /* main function */ /*==========================================================*/ int main(void){ double pi = 4*atan(1); mk_triangle_data("out.txt", -2*pi, 2*pi, 1000); mk_graph("out.txt", "x", -2*pi, 2*pi, "y", -3, 3); return 0; } /*==========================================================*/ /* make a data file */ /*==========================================================*/ void mk_triangle_data(char *a, double x1, double x2, int n){ double x, dx; double y1, y2, y3; int i; FILE *out; dx = (x2-x1)/n; out = fopen(a, "w"); for(i=0; i<=n; i++){ x = x1+dx*i; y1 = sin(x); y2 = cos(x); y3 = tan(x); fprintf(out, "%e\t%e\t%e\t%e\n", x, y1, y2, y3); } fclose(out); } /*==========================================================*/ /* make a graph */ /*==========================================================*/ void mk_graph(char *f, char *xlb, double x1, double x2, char *ylb, double y1, double y2) { FILE *gp; gp = popen("gnuplot -persist","w"); fprintf(gp, "reset\n"); /* ------- set x grid ---------*/ fprintf(gp, "set grid\n"); /* ------- set x axis ---------*/ fprintf(gp, "set xtics 1\n"); fprintf(gp, "set mxtics 10\n"); fprintf(gp, "set xlabel \"%s\"\n", xlb); fprintf(gp, "set nologscale x\n"); fprintf(gp, "set xrange[%e:%e]\n", x1, x2); /* ------- set y axis ---------*/ fprintf(gp, "set ytics 1\n"); fprintf(gp, "set mytics 10\n"); fprintf(gp, "set ylabel \"%s\"\n", ylb); fprintf(gp, "set nologscale y\n"); fprintf(gp, "set yrange[%e:%e]\n", y1, y2); /* ------- plat graphs ---------*/ fprintf(gp, "set terminal x11\n"); fprintf(gp, "plot \"%s\" using 1:2 with line,\ \"%s\" using 1:3 with line,\ \"%s\" using 1:4 with line\n", f, f, f); fprintf(gp, "set terminal emf\n"); fprintf(gp, "set output \"tri.emf\"\n"); fprintf(gp, "replot\n"); pclose(gp); }