home *** CD-ROM | disk | FTP | other *** search
/ Dr. CD ROM (Annual Premium Edition) / premium.zip / premium / IBMOS2_1 / LSQRFT13.ZIP / plot.c < prev    next >
Text File  |  1993-07-10  |  7KB  |  275 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "fit.h"
  4.  
  5. extern FILE *pstream;
  6. extern double xmin, xmax;
  7. extern int wiflag;
  8. extern int debug;
  9.  
  10. int plot(func,data, order, num_indep, ndata, filename, comment, a, ma)
  11. int (*func)();
  12. double **data;
  13. struct data_order order;
  14. int num_indep;
  15. int ndata;
  16. char *filename;
  17. char *comment;
  18. double *a;
  19. int ma;
  20. {
  21. int i, failed;
  22. FILE *stream;
  23. char gnubuf[100];
  24. double umin = 1e200, umax= -1e200, vmin = 1e200, vmax=-1e200;
  25. double chisqr;
  26. int num_samples;
  27. char fitfile[40];
  28.  
  29. #ifdef DOS
  30. gnucmd(gnubuf);
  31. return 1;
  32. #endif
  33. #ifdef UNIX
  34. strcpy(fitfile,"/tmp/fit.tmp");
  35. #endif
  36. #ifndef UNIX
  37. strcpy(fitfile,"fit.tmp");
  38. #endif
  39.  
  40. if(ndata < 1 ){
  41.     printf("pl failed, no data\n");
  42.     return 0;
  43. }
  44. if( num_indep > 2){
  45.     printf("pl failed, can only plot 1 or 2 independent variables\n");
  46.     return 0;
  47. }
  48.  
  49. sprintf(gnubuf,"clear\n");
  50. gnucmd( gnubuf);
  51.  
  52. if(num_indep == 1){
  53. sprintf(gnubuf,"set xlabel 'x'\n");
  54. gnucmd( gnubuf);
  55. sprintf(gnubuf,"set ylabel 'f(x)'\n");
  56. gnucmd( gnubuf);
  57. sprintf(gnubuf,"set title 'data and fit'\n");
  58. gnucmd( gnubuf);
  59. }
  60.  
  61. if(num_indep == 2){
  62. sprintf(gnubuf,"set xlabel 'x'\n");
  63. gnucmd( gnubuf);
  64. sprintf(gnubuf,"set ylabel 'y'\n");
  65. gnucmd( gnubuf);
  66. sprintf(gnubuf,"set zlabel 'f(x,y)'\n");
  67. gnucmd( gnubuf);
  68. sprintf(gnubuf,"set title 'data and fit'\n");
  69. gnucmd( gnubuf);
  70. }
  71.  
  72.  
  73. /* function is plotted through a defined gnuplot function */
  74. if(strncmp(comment,"FILE",4) != 0){
  75.  
  76.         /* define the parameters in gnuplot */
  77.     for(i = 0; i < ma; i++){
  78.         sprintf(gnubuf,"a%d = %g\n",i,a[i]);
  79.         gnucmd(gnubuf);
  80.     }
  81.  
  82.         /* define the function in gnuplot */
  83.     sprintf(gnubuf,"%s\n",comment);
  84.     gnucmd( gnubuf);
  85.  
  86.         /* tell gnuplot to do the plot */
  87.     if(num_indep == 1){
  88.         sprintf(gnubuf,"plot '%s' using %d:%d, f(x) \n",
  89.             filename,order.x[0] + 1, order.y + 1);
  90.         gnucmd( gnubuf);
  91.         if(ndata > 40) num_samples = ndata/2;
  92.         else if(ndata > 20) num_samples = ndata;
  93.         else num_samples = 20;
  94.         if(num_samples > 500) num_samples = 500;
  95.         sprintf(gnubuf,"set samples %d\n",num_samples);
  96.         gnucmd(gnubuf);
  97.     }
  98.  
  99.  
  100. /* for 2 independent variables, we need to use the gnuplot */
  101. /* command splot and plot parametrically.  See gnuplot documentation */
  102. /* for the reasons why.  We also need to know tell gnuplot */
  103. /* the ranges of the independent variables, so we calculate these also */
  104.  
  105.     if(num_indep == 2){
  106.         for(i = 0; i < ndata; i++){
  107.             if(data[order.x[0]][i] < umin) umin = data[order.x[0]][i];
  108.             if(data[order.x[0]][i] > umax) umax = data[order.x[0]][i];
  109.             if(data[order.x[1]][i] < vmin) vmin = data[order.x[1]][i];
  110.             if(data[order.x[1]][i] > vmax) vmax = data[order.x[1]][i];
  111.         }
  112.         sprintf(gnubuf,"set parametric\n");
  113.         gnucmd( gnubuf);
  114.         sprintf(gnubuf,"set urange [%g:%g]\n", umin, umax);
  115.         gnucmd( gnubuf);
  116.         sprintf(gnubuf,"set vrange [%g:%g]\n", vmin, vmax);
  117.         gnucmd(gnubuf);
  118.         sprintf(gnubuf,"set samples 40\n");
  119.         gnucmd(gnubuf);
  120.         sprintf(gnubuf,"splot '%s' using %d:%d:%d, u,v,f(u,v) \n",
  121.             filename,order.x[0] + 1, order.x[1] + 1, order.y + 1);
  122.         gnucmd(gnubuf);
  123.         sprintf(gnubuf,"set noparametric\n");
  124.         gnucmd(gnubuf);
  125.     }
  126.  
  127. }
  128.  
  129. /* function is plotted through a temporary file */
  130. else if(strncmp(comment,"FILE",4) == 0){
  131.     if(debug) printf("in plot(), about to call calc_yfit()\n");
  132.  
  133. /* calculate the values of the fitting function for current parameters */
  134.     failed = calc_yfit(func, data, order, num_indep, a, ndata, ma, &chisqr);
  135.     if(failed) return 1;
  136.     if(debug) printf("in plot(), returned from calc_yfit()\n");
  137.     stream = fopen(fitfile,"wt");
  138.     if(num_indep == 1)
  139.         for(i = 0; i < ndata; i++) 
  140.             fprintf(stream,"%g %g\n", data[order.x[0]][i], data[order.yfit][i]);
  141.     if(num_indep == 2)
  142.         for(i = 0; i < ndata; i++)
  143.             fprintf(stream,"%g %g %g\n", data[order.x[0]][i], data[order.x[1]][i], data[order.yfit][i]);
  144.     fclose(stream);
  145.     if(num_indep == 1){
  146.         sprintf(gnubuf,"plot '%s' using %d:%d,'%s'\n",
  147.                                 filename,order.x[0] + 1, order.y + 1, fitfile);
  148.         gnucmd(gnubuf);
  149.     }
  150.     if(num_indep == 2){
  151.         sprintf(gnubuf,"set parametric\n");
  152.         gnucmd(gnubuf);
  153.         sprintf(gnubuf,"splot '%s' using %d:%d:%d,'%s' \n",
  154.                                 filename,order.x[0] + 1, order.x[1] + 1,order.y + 1, fitfile);
  155.         gnucmd(gnubuf);
  156.         sprintf(gnubuf,"set noparametric\n");
  157.         gnucmd(gnubuf);
  158.     }
  159.  
  160. }
  161. return 0;
  162. }
  163.  
  164. /* the pr function plot residual errors */
  165. int pr(command,func,data, order, num_indep, ndata, filename, comment, a, ma)
  166. char *command;
  167. int (*func)();
  168. double **data;
  169. struct data_order order;
  170. int num_indep;
  171. int ndata;
  172. char *filename;
  173. char *comment;
  174. double *a;
  175. int ma;
  176. {
  177. int i, flag, failed;
  178. FILE *stream;
  179. char gnubuf[100];
  180. double chisqr;
  181. char fitfile[40];
  182.  
  183. #ifdef DOS
  184. gnucmd(gnubuf);
  185. return 1;
  186. #endif
  187. #ifdef UNIX
  188. strcpy(fitfile,"/tmp/fit.tmp");
  189. #endif
  190. #ifndef UNIX
  191. strcpy(fitfile,"fit.tmp");
  192. #endif
  193.  
  194. if(debug)printf("in pr() command: %s\n", command);
  195. if(ndata < 1 ){
  196.     printf("pr failed, no data\n");
  197.     return 0;
  198. }
  199. if( num_indep > 2){
  200.     printf("pr failed, can only plot 1 or 2 independent variables\n");
  201.     return 0;
  202. }
  203.  
  204. sprintf(gnubuf,"clear\n");
  205. if( sscanf(command,"%*s %d", &flag ) == 0) flag = 1;
  206. if(debug)printf("flag: %d\n", flag);
  207.  
  208. failed = calc_yfit(func, data, order, num_indep, a, ndata, ma, &chisqr);
  209. if(failed) return 1;
  210.  
  211. /* option 1, plot yfit vs. y */
  212. if(flag ==1){
  213.     stream = fopen(fitfile,"wt");
  214.     for(i = 0; i < ndata; i++) 
  215.         fprintf(stream,"%g %g\n", data[order.yfit][i], data[order.y][i]);
  216.     fclose(stream);
  217.     sprintf(gnubuf,"set title 'data vs. fit'\n");
  218.     gnucmd( gnubuf);
  219.     sprintf(gnubuf,"set xlabel 'f(x)'\n");
  220.     gnucmd( gnubuf);
  221.     sprintf(gnubuf,"set ylabel 'y'\n");
  222.     gnucmd( gnubuf);
  223.     sprintf(gnubuf,"plot '%s' with points \n", fitfile);
  224.     gnucmd(gnubuf);
  225. }
  226.  
  227. /* option 1, plot y-yfit as a function of independent variables */
  228. else{
  229.     if(debug)printf("stream = fopen();\n");
  230.     stream = fopen(fitfile,"wt");
  231.     if(debug)printf("if(num_indep ==1);\n");
  232.     sprintf(gnubuf,"set title 'data-fit'\n");
  233.     gnucmd( gnubuf);
  234.     if(num_indep ==1)
  235.         for(i = 0; i < ndata; i++) 
  236.             fprintf(stream,"%g %g\n", data[order.x[0]][i],
  237.                         data[order.y][i]-data[order.yfit][i]);
  238.     if(debug)printf("if(num_indep ==2);\n");
  239.     else if(num_indep ==2)
  240.         for(i = 0; i < ndata; i++)
  241.             fprintf(stream,"%g %g %g\n", data[order.x[0]][i],data[order.x[1]][i],
  242.                         data[order.y][i]-data[order.yfit][i]);
  243.     fclose(stream);
  244.     if(num_indep ==1){
  245.         sprintf(gnubuf,"set title 'y - f(x)'\n");
  246.         gnucmd( gnubuf);
  247.         sprintf(gnubuf,"set xlabel 'x'\n");
  248.         gnucmd( gnubuf);
  249.         sprintf(gnubuf,"set ylabel 'y - f(x)'\n");
  250.         gnucmd( gnubuf);
  251.         sprintf(gnubuf,"plot '%s' with points \n", fitfile);
  252.         gnucmd(gnubuf);
  253.         sprintf(gnubuf,"plot '%s'\n", fitfile);
  254.         gnucmd(gnubuf);
  255.     }        
  256.     if(num_indep ==2){
  257.         sprintf(gnubuf,"set title 'y - f(x)'\n");
  258.         gnucmd( gnubuf);
  259.         sprintf(gnubuf,"set xlabel 'x'\n");
  260.         gnucmd( gnubuf);
  261.         sprintf(gnubuf,"set zlabel 'y - f(x)'\n");
  262.         gnucmd( gnubuf);
  263.         sprintf(gnubuf,"set ylabel 'y'\n");
  264.         gnucmd( gnubuf);
  265.         sprintf(gnubuf,"set parametric\n");
  266.         gnucmd(gnubuf);
  267.         sprintf(gnubuf,"splot '%s'\n", fitfile);
  268.         gnucmd(gnubuf);
  269.         sprintf(gnubuf,"set noparametric\n");
  270.         gnucmd(gnubuf);
  271.     }
  272. }
  273. return 0;
  274. }
  275.