c - Tracing out the motion of a double pendulum in gnuplot (and gif conversion)? -


i have written c program trace out motion of double pendulum, having difficulties in getting gnuplot (controlled c program) trace out paths of masses (example). far have created program such produces number of png images @ each interval (using runge kutta method), want output gif instead line traces out path of masses in real time.

in code below assuming problem occurs piping out gnuplot within loop (to save wasting time sifting through it)

/* header files */ #include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> #include <assert.h>  // definitions #define gravity 9.8 #define increment 0.0175  // declerations of functions double th1_xder(double t1d); double th1_yder(double t2d); double th2_xder(double t1d, double th1, double t2d, double th2, double l1, double l2, double m1, double m2); double th2_yder(double t1d, double th1, double t2d, double th2, double l1, double l2, double m1, double m2);  int main (int argc, char * argv[]) {      double x1=0, y1=0, x2=0, y2=0; //coordinates     double l1=0, l2=0;  //lengths     double m1=0, m2=0;  //masses     double th1=0, th2=0; //angles     double t1d=0, t2d=0; //zero velocity      //second order runge-kutta equations     double k1=0, k2=0, k3=0, k4=0; //for x1     double q1=0, q2=0, q3=0, q4=0; //for y1     double r1=0, r2=0, r3=0, r4=0; //for x2     double s1=0, s2=0, s3=0, s4=0; //for y2      int = 0;     double x0=0, y0=0;      printf("enter '1' input own data or '0' use preset data\n");     char dummy = 'a';     scanf("%c", &dummy);     assert((dummy == '1') || (dummy == '0'));      if (dummy == '1') {          printf("please enter length l1:\n");         scanf("%lf", &l1);         printf("please enter length l2:\n");         scanf("%lf", &l2);         printf("please enter mass m1:\n");         scanf("%lf", &m1);         printf("please enter mass m2:\n");         scanf("%lf", &m2);         printf("please enter angle theta1:\n");         scanf("%lf", &th1);         printf("please enter angle theta2:\n");         scanf("%lf", &th2);     } else {          l1 = 1;         l2 = 1;         m1 = 1;         m2 = 1;         th1= 90;         th2= 0;     }      th1 = th1*(m_pi/180);     th2 = th2*(m_pi/180);        file *fsp;      if((fsp=fopen("origin.dat", "w"))==null) {         fprintf(stdout, "cannot open origin.dat\n");         exit (exit_failure);     }       fprintf(fsp, "0\t0");         fclose(fsp);        file *fout;      if((fout=fopen("testout.dat", "w"))==null) {         fprintf(stdout, "cannot open testout.dat\n");         exit (exit_failure);     }       printf("%f\t%f\t%f\t%f\n", x1, y1, x2, y2);       fprintf(fout, "%f\t%f\t%f\t%f\n", x1, y1, x2, y2);       for(i = 0; < 250; i++) {          if ((fout=fopen("testout.dat", "w"))==null) {             fprintf(stdout, "cannot open testout.dat\n");             exit (exit_failure);         }          k1 = th1_xder(t1d);         q1 = th1_yder(t2d);         r1 = th2_xder(t1d, th1, t2d, th2, l1, l2, m1, m2);         s1 = th2_yder(t1d, th1, t2d, th2, l1, l2, m1, m2);          k2 = th1_xder(t1d + (r1/2));         q2 = th1_yder(t2d + (s1/2));         r2 = th2_xder((t1d + (r1/2)), (th1 + (k1/2)), (t2d +(s1/2)), (th2 + (q1/2)), l1, l2, m1, m2);          s2 = th2_yder((t1d + (r1/2)), (th1 + (k1/2)), (t2d +(s1/2)), (th2 + (q1/2)), l1, l2, m1, m2);           k3 = th1_xder(t1d + (r2/2));         q3 = th1_yder(t2d + (s2/2));         r3 = th2_xder((t1d + (r2/2)), (th1 + (k2/2)), (t2d +(s2/2)), (th2 + (q2/2)), l1, l2, m1, m2);         s3 = th2_yder((t1d + (r2/2)), (th1 + (k2/2)), (t2d +(s2/2)), (th2 + (q2/2)), l1, l2, m1, m2);            k4 = th1_xder(t1d + r3);         q4 = th1_yder(t2d + s3);         r4 = th2_xder((t1d + r3), (th1 + k3), (t2d + s3), (th2 + q3), l1, l2, m1, m2);         s4 = th2_yder((t1d + r3), (th1 + k3), (t2d + s3), (th2 + q3), l1, l2, m1, m2);           t1d = t1d + (r1 + 2*r2 + 2*r3 + r4)/6;         t2d = t2d + (s1 + 2*s2 + 2*s3 + s4)/6;         th1 = th1 + (k1 + 2*k2 + 2*k3 + k4)/6;          th2 = th2 + (q1 + 2*q2 + 2*q3 + q4)/6;          x1 = l1*sin(th1);         y1 = -l1*cos(th1);         x2 = x1 + l2*sin(th2);         y2 = y1 - l2*cos(th2);          printf("%f\t%f\t%f\t%f\n", x1, y1, x2, y2);           fprintf(fout, "%f\t%f\t%f\t%f\n", x1, y1, x2, y2);           fclose(fout);           file *gnuplotpipe = popen("gnuplot -persist","w");         if (gnuplotpipe) {             fprintf(gnuplotpipe, "set style data lines\n");             fprintf(gnuplotpipe, "set terminal png nocrop enhanced size 1280,720; set output 'yyy%d.png'\n", i);             fprintf(gnuplotpipe, "set title 'frame%d'\n", i);             fprintf(gnuplotpipe, "set multiplot\n");             fprintf(gnuplotpipe, "set xrange [-2.5:2.5]; set yrange [-2.5:2]\n");             fprintf(gnuplotpipe, "unset key; unset ytics; unset xtics\n");                                fprintf(gnuplotpipe, "plot 'testout.dat' using 3:4\n");              fprintf(gnuplotpipe, "plot '-' lines lw 2 lc rgb 'black', 'testout.dat' u 1:2 w points pt 7 ps 2, 'testout.dat' u 3:4 w points pt 7 ps 2, 'origin.dat' u 1:2 w points pt 7 ps 2 lc 0\n");             fprintf(gnuplotpipe, "%f %f\n", x0, y0);             fprintf(gnuplotpipe, "%f %f\n", x1, y1);             fprintf(gnuplotpipe, "%f %f\n", x2, y2);             fprintf(gnuplotpipe, "e\n");                fprintf(gnuplotpipe, "\n");             fprintf(gnuplotpipe, "set nomultiplot\n");                            fflush(gnuplotpipe);                        fprintf(gnuplotpipe,"exit \n");             pclose(gnuplotpipe);         }                                              }                    return exit_success; }  double th1_xder(double t1d) {      double k = t1d*increment;     return k; }  double th1_yder(double t2d) {      double m = t2d*increment;     return m; }  double th2_xder(double t1d, double th1, double t2d, double th2, double l1, double l2, double m1, double m2) {      double l = increment*((gravity/l1)*((m2/(m1 + m2))*sin(th2)*cos(th1-th2)-sin(th1))-(m2/(m1 + m2))*sin(th1-th2)*((l2/l1)*t2d*t2d + t1d*t1d*cos(th1-th2)))/(1-((m2/(m1 + m2))*cos(th1-th2)*cos(th1-th2)));     return l; }  double th2_yder(double t1d, double th1, double t2d, double th2, double l1, double l2, double m1, double m2) {      double p = increment*((gravity/l2)*(sin(th1)*cos(th1-th2)-sin(th1)) + sin(th1-th2)*((l1/l2)*t1d*t1d + (m2/(m1 + m2))*t2d*t2d*cos(th1-th2)))/(1-((m2/(m1 + m2))*cos(th1-th2)*cos(th1-th2)));     return p; } 


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -