用管道加载P中的绘图脚本

时间:2014-01-14 13:12:33

标签: c gnuplot piping

我已经制作了一些数据,我的目的是绘制它。为方便起见,我已经定义了一个管道,并通过这个管道将plot命令传递给GNUplot,命令为:

load "file.gnu"

但是我注意到图中缺少数据文件中的一些数据点。当我通过终端启动GNUplot并使用相同的命令绘制时,没有丢失点这样的问题。管道内存有限吗?我对这种情况感到困惑,并希望收到你对此事的看法。

编辑:源文件如下(取消注释管道部分):

//Compilation: gcc Q5.c -lm -o Q5
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define lowbnd 2.5
#define upbnd 10.0
#define precision 100.0
// Gamma function is defined in the GNU C library so we use it
double approx1(double x)
{
    return sqrt(2 * M_PI / x) * pow(x, x) * exp(-(x - 1 / (12.0 * x) + 1 / (pow(x, 3) * 360.0)));
}
double approx2(double x)
{
    return tgamma(x + 5) / ( (x + 4) * (x + 3) * (x + 2) * (x + 1) * x );
}
int main(void)
{
    FILE *fp = fopen("Q5data.dat", "w+");
    int i;
    double x, y1, y2, yexact, diff1, diff2;
    for(i = 0; x < upbnd; i++)
    {
        x = lowbnd + i / precision;
        y1 = approx1(x);
        y2 = approx2(x);
        yexact = tgamma(x);
        diff1 = fabs(y1 - yexact);
        diff2 = fabs(y2 - yexact);
        fprintf(fp, "%f \t %f \t %f \t %f \t %e \t %e \n", x, y1, y2, yexact, diff1, diff2);

    }
    // The below line works well but through piping some data points were lost so I commented this section out. You may try
    // it yourself and see.
    FILE *gp = popen("gnuplot -persist","w"); 
    if (gp==NULL)
    {
        printf("Error opening pipe to GNUplot. Check if you have it! \n");
        exit(0);
    }
    fprintf(gp, "load 'Q5.gnu' \n"); 
    puts("Please check the generated plot files named Q5.ps and Q5diff.ps");
    // Just to try how well these represent the gamma function
    //x = 35;
    //printf("%f \n %f \n %f \n", tgamma(x), approx1(x), approx2(x));
    pclose(gp);
    fclose(fp);
    return 0;
}

PLOT脚本:

set key below Left title 'Legend' box 3
set logscale 
set terminal postscript enhanced color font 'Euclid, 15'
set border linewidth 1.5
set title "Gamma fnc. and Approximations" tc rgb"red"
set pointsize 1.0
set xrange [2.5:10.0]
set autoscale y
set xtics autofreq
set xlabel "x" tc rgb"green"
set ylabel "" tc rgb"green"
set grid xtics ytics mxtics nomytics
set output "Q5log.ps"
plot "Q5data.dat" u 1:2 t 'Approximation 1' lc rgb"blue" with lines, \
     "Q5data.dat" u 1:3 t 'Approximation 2' lc rgb"red" with lines, \
     "Q5data.dat" u 1:4 t 'Exact' lc rgb"cyan" with lines
set output "Q5difference.ps"
set title "Difference btw. Approximations and Exact Representation"
plot "Q5data.dat" u 1:5 t 'Difference 1' lc rgb"blue" with lines, \
     "Q5data.dat" u 1:6 t 'Difference 2' lc rgb"red" with lines
set output "Q5nonlog.ps"
unset logscale
plot "Q5data.dat" u 1:2 t 'Approximation 1' lc rgb"blue" with lines, \
     "Q5data.dat" u 1:3 t 'Approximation 2' lc rgb"red" with lines, \
     "Q5data.dat" u 1:4 t 'Exact' lc rgb"cyan" with lines

由于

0 个答案:

没有答案