home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c129 / 1.ddi / CURVEDEM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-13  |  4.6 KB  |  160 lines

  1.  
  2. # include <math.h>
  3. # include <stdlib.h>
  4. # include <stdio.h>
  5. # include <graph.h>
  6. # include <string.h>
  7. # include "worlddr.h"
  8. # include "segraph.h"
  9. # include "asyncxx.h"
  10. # include "curvefit.h"
  11.  
  12. float xdata[50], ydata[50];
  13. float indvar[50],depvar[50];
  14. float coef[50],coefsig[50];
  15. int numobs,i,j,order;
  16. char error;
  17. float  xval,randomnum;
  18. float  yest[50],resid[50];
  19. float  rsq,r,see;
  20. int maxX, maxY,code;
  21. char  xstr[20],tempstr[20],coefstr[20], coefsigstr[20];
  22. int count;
  23. char ch, chstr[2] = { 0,0 };
  24.  
  25.  
  26.  
  27.  
  28. void main() {
  29.   numobs = 40;
  30.   for (i = 0; i<= numobs-1; i++){    /* set up dependent / independent variables */
  31.     xval = i/10.0 - 2;
  32.     indvar[i] = xval;
  33.     depvar[i] = xval*((rand()/32767.0)*200) + xval*xval*
  34.                  ((rand()/32767.0)*50) +
  35.                  xval*xval*xval*( (rand()/32737.0) * 20);
  36.   }
  37.  
  38. /****************************************************
  39.     - IF YOU WANT TO CONFIGURE FOR CGA 4 COLORS    
  40.       CHOOSE InitSEGraphics(1);
  41.     - HERCULES SUPPORT IS NOW INCLUDED IN MICROSOFT
  42.       C VERSION 5.1.  IF YOU WANT TO USE THE         
  43.       HERCULES DRIVER YOU MUST FIRST INSTALL       
  44.       MSHERC.COM FROM DOS BY TYPING  
  45.                   msherc<enter>
  46.       THEN IN YOUR MAIN PROGRAM CALL
  47.                   InitSEGraphics(12);
  48.     - REFER TO THE README.DOC FILE - PART 3
  49.       INCLUDED IN YOUR MICROSOFT C 5.1 COMPILER 
  50.       FOR DETAILS ON ADDED HERCULES SUPPORT.
  51.  **************************************************/
  52.  
  53.   InitSEGraphics(6);
  54.   GetMaxCoords(&maxX,&maxY);
  55.   DefGraphWindow(maxX / 3,1,maxX,maxY / 2,3);
  56.   DefGraphWindow(maxX / 3,maxY / 2,maxX,maxY,4);
  57.   DefGraphWindow(0,0,Round(maxX / 3.0),maxY / 2.0,5);
  58.   SetWin2PlotRatio(3, 0.2,0.14,0.05,0.14 );
  59.   SetWin2PlotRatio(4, 0.2,0.14,0.05,0.14 );
  60.  
  61.  
  62.   do {
  63.     SetCurrentWindow(5);
  64.     ClearWindow();
  65.     setlinestyleXX(0,0,3);
  66.     BorderCurrentWindow(1);
  67.     LabelGraphWindow(30,900,"Input Order of Curve",0,0);
  68.     LabelGraphWindow(30,850,"  must be 6 or less: ",0,0);
  69.     ch = getch();
  70.     chstr[0] = ch;
  71.     LabelGraphWindow(900, 850, chstr,0,0);
  72.     order = ch - 48;
  73.     LabelGraphWindow(30,800,"Curvefit Equation = ",0,0);
  74.     LabelGraphWindow(30,750,"Coefficient   Error ",0,0);
  75.     if (  order < 1 || order >=  6)  exit(code);
  76.     PolyCurveFit(indvar,depvar,numobs,order,coef,yest,resid,&see,coefsig,&rsq,&r,&error);
  77.  
  78.     for (i = 0; i <= numobs-1; i++){
  79.       xdata[i] = indvar[i];
  80.       ydata[i] = depvar[i];
  81.     }
  82.  
  83.     LabelGraphWindow(30,700,"Y =    ",0,0);
  84.     count = 650;
  85.     for (i = 0; i <= order-1; i++ ){
  86.       itoa(i,xstr,10);
  87.       strcpy(tempstr," X^");
  88.       strcat(tempstr,xstr );
  89.       LabelGraphWindow(30,count,tempstr, 0,0);
  90.       sprintf( coefstr, "%*.*f",1,2,coef[i]);
  91.       LabelGraphWindow(550,count,coefstr, 2,0);
  92.       sprintf( coefsigstr,  "%*.*f",1,2,coefsig[i]);
  93.       LabelGraphWindow(800,count,coefsigstr, 2,0);
  94.       count = count - 50;
  95.     }
  96.     settextjustifyXX(0,0);
  97.     sprintf(xstr, "%*.*f",1,2,r);
  98.     strcpy(tempstr,"R Value = ");
  99.     strcat(tempstr, xstr );
  100.     LabelGraphWindow(30,count,tempstr,0,0);
  101.     count -= 50;
  102.     sprintf(xstr, "%*.*f",1,2,rsq);
  103.     strcpy(tempstr,"R Squared Value = ");
  104.     strcat(tempstr, xstr );
  105.     LabelGraphWindow(30,count,tempstr,0,0);
  106.     count -= 50;
  107.     sprintf(xstr, "%*.*f",1,2,see);
  108.     strcpy(tempstr,"SEE = ");
  109.     strcat(tempstr, xstr );
  110.     LabelGraphWindow(30,count,tempstr,0,0);
  111.     setlinestyleXX(0,0,1);
  112.  
  113.  
  114.  
  115.     SetCurrentWindow(3);
  116.     ClearWindow();
  117.     SetAxesType(0,0);
  118.     AutoAxes(xdata,ydata,numobs,1);
  119.     LinePlotData(xdata, ydata, numobs, 1,0);
  120.     SelectColor(3);
  121.     TitleWindow("ORIGINAL DATA - SOLID,  FITTED DATA - DOTTED");
  122.     TitleXAxis("INDEPENDENT VARIABLE");
  123.     TitleYAxis("DEPENDENT VARIABLE");
  124.     for (i = 0; i <= numobs-1; i++) {
  125.       ydata[i] = yest[i];
  126.     }
  127.     LinePlotData(xdata, ydata, numobs, 1,3);
  128.     setlinestyleXX(1,0,1);
  129.     DrawGrid(10);
  130.     setlinestyleXX(0,0,1);
  131.  
  132.     SetCurrentWindow(4);
  133.     ClearWindow();
  134.     SetAxesType(0,0);
  135.     for (i = 0; i <= numobs-1; i++){
  136.       ydata[i] = resid[i];
  137.     }
  138.     AutoAxes(xdata,ydata,numobs,1);
  139.     BargraphData(xdata, ydata, numobs, 0.05, 1,1);
  140.     SelectColor(3);
  141.     TitleWindow("CURVE FIT ERROR ANALYSIS");
  142.     TitleXAxis("INDEPENDENT VARIABLE");
  143.     TitleYAxis("RESIDUALS");
  144.     setlinestyleXX(1,0,1);
  145.     DrawGridY(10);
  146.  
  147.     SetCurrentWindow(5);
  148.     LabelGraphWindow(30,200,"MORE  <Y or N> ?",0,0);
  149.     ch = getch();
  150.     ch = toupper(ch);
  151.     chstr[0] = ch;
  152.     LabelGraphWindow( 850, 200, chstr,0,0);
  153.  
  154.    } while (  ch != 'N' );
  155.  
  156.   CloseSEGraphics();
  157.  
  158.  
  159. }
  160.