#include <stdio.h>
int
main() {
FILE
*outputFile =
fopen
(
"motion_data.txt"
,
"w"
);
if
(!outputFile) {
fprintf
(stderr,
"Failed to create data file.\n"
);
return
1;
}
double
x = 0.2;
double
v = 0.0;
double
dt = 0.01;
double
t = 0.0;
while
(t <= 10.0) {
double
acceleration = (-10.0 * x - 0.5 * v) / 1.0;
v += acceleration * dt;
x += v * dt;
fprintf
(outputFile,
"%lf %lf %lf\n"
, t, x, v);
t += dt;
}
fclose
(outputFile);
FILE
*gnuplotPipe = popen(
"gnuplot -persistent"
,
"w"
);
if
(!gnuplotPipe) {
fprintf
(stderr,
"Failed to start Gnuplot.\n"
);
return
1;
}
fprintf
(gnuplotPipe,
"set terminal pngcairo enhanced font 'default,10' size 800,400\n"
);
fprintf
(gnuplotPipe,
"set output 'motion_plot.png'\n"
);
fprintf
(gnuplotPipe,
"set title 'Displacement and Velocity vs. Time'\n"
);
fprintf
(gnuplotPipe,
"set xlabel 'Time (s)'\n"
);
fprintf
(gnuplotPipe,
"set ylabel 'Displacement (m)'\n"
);
fprintf
(gnuplotPipe, "plot
'motion_data.txt'
using
1:2 with lines lw 2 title
'Displacement'
, \
'motion_data.txt'
using
1:3 with lines lw 2 title
'Velocity'
\n");
fprintf
(gnuplotPipe,
"exit\n"
);
pclose(gnuplotPipe);
return
0;
}