I want to plot the sinus function with Gnuplot using C99 on CLion from JetBrains (Windows 10). I have been given a code that is supposed to work:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void plot(double a, double b, int nbpoints, double (*f)(double)){
double x, h = (b-a)/nbpoints;
/* create data file */
FILE *data = fopen("data.txt", "w");
for (x = a; x < b+h/2; x+=h)
fprintf(data, "%g %g\n", x, f(x));
fclose(data);
/* create command file */
FILE *cmd = fopen("cmd.txt", "w");
fprintf(cmd, "plot 'data.txt' using 1:2 with lines\n");
fclose(cmd);
/* execute commands in a file */
system("gnuplot -persistent cmd.txt");
}
int main() {
plot(0, M_PI, 100, sin);
}
The interpreter says that "gnuplot" is not recognised. It shows me the same message when I directly write it in the Windows terminal. Yet, I downloaded Gnuplot on my computer, and I also have Octave. Could someone help me to make this work ? Here is the the complete project file on Google Drive in case you would need the additional files of CLion.
EDIT: I have modified the path this way:
system("cd C:\Program Files\gnuplot\bin")
Now, when I enter system("gnuplot")
or system("gnuplot.exe")
, it opens me another window with gnuplot. Half of my problem is solved. However, I still can't directly write in gnuplot from C, and the command system("gnuplot>plot sin(x)")
doesn't work. How could I use the gnuplot terminal without interrupting the code, and make it only pop the plot graph ?
C:\Program Files\gnuplot\bin>gnuplot plot sin(x)
line 0: Cannot load input from 'sin(x)'
^
"plot" line 1: invalid command