home *** CD-ROM | disk | FTP | other *** search
-
- # include <math.h>
- # include <stdlib.h>
- # include <stdio.h>
- # include <graph.h>
- # include <string.h>
- # include "worlddr.h"
- # include "segraph.h"
- # include "asyncxx.h"
- # include "curvefit.h"
-
- float xdata[50], ydata[50];
- float indvar[50],depvar[50];
- float coef[50],coefsig[50];
- int numobs,i,j,order;
- char error;
- float xval,randomnum;
- float yest[50],resid[50];
- float rsq,r,see;
- int maxX, maxY,code;
- char xstr[20],tempstr[20],coefstr[20], coefsigstr[20];
- int count;
- char ch, chstr[2] = { 0,0 };
-
-
-
-
- void main() {
- numobs = 40;
- for (i = 0; i<= numobs-1; i++){ /* set up dependent / independent variables */
- xval = i/10.0 - 2;
- indvar[i] = xval;
- depvar[i] = xval*((rand()/32767.0)*200) + xval*xval*
- ((rand()/32767.0)*50) +
- xval*xval*xval*( (rand()/32737.0) * 20);
- }
-
- /****************************************************
- - IF YOU WANT TO CONFIGURE FOR CGA 4 COLORS
- CHOOSE InitSEGraphics(1);
- - HERCULES SUPPORT IS NOW INCLUDED IN MICROSOFT
- C VERSION 5.1. IF YOU WANT TO USE THE
- HERCULES DRIVER YOU MUST FIRST INSTALL
- MSHERC.COM FROM DOS BY TYPING
- msherc<enter>
- THEN IN YOUR MAIN PROGRAM CALL
- InitSEGraphics(12);
- - REFER TO THE README.DOC FILE - PART 3
- INCLUDED IN YOUR MICROSOFT C 5.1 COMPILER
- FOR DETAILS ON ADDED HERCULES SUPPORT.
- **************************************************/
-
- InitSEGraphics(6);
- GetMaxCoords(&maxX,&maxY);
- DefGraphWindow(maxX / 3,1,maxX,maxY / 2,3);
- DefGraphWindow(maxX / 3,maxY / 2,maxX,maxY,4);
- DefGraphWindow(0,0,Round(maxX / 3.0),maxY / 2.0,5);
- SetWin2PlotRatio(3, 0.2,0.14,0.05,0.14 );
- SetWin2PlotRatio(4, 0.2,0.14,0.05,0.14 );
-
-
- do {
- SetCurrentWindow(5);
- ClearWindow();
- setlinestyleXX(0,0,3);
- BorderCurrentWindow(1);
- LabelGraphWindow(30,900,"Input Order of Curve",0,0);
- LabelGraphWindow(30,850," must be 6 or less: ",0,0);
- ch = getch();
- chstr[0] = ch;
- LabelGraphWindow(900, 850, chstr,0,0);
- order = ch - 48;
- LabelGraphWindow(30,800,"Curvefit Equation = ",0,0);
- LabelGraphWindow(30,750,"Coefficient Error ",0,0);
- if ( order < 1 || order >= 6) exit(code);
- PolyCurveFit(indvar,depvar,numobs,order,coef,yest,resid,&see,coefsig,&rsq,&r,&error);
-
- for (i = 0; i <= numobs-1; i++){
- xdata[i] = indvar[i];
- ydata[i] = depvar[i];
- }
-
- LabelGraphWindow(30,700,"Y = ",0,0);
- count = 650;
- for (i = 0; i <= order-1; i++ ){
- itoa(i,xstr,10);
- strcpy(tempstr," X^");
- strcat(tempstr,xstr );
- LabelGraphWindow(30,count,tempstr, 0,0);
- sprintf( coefstr, "%*.*f",1,2,coef[i]);
- LabelGraphWindow(550,count,coefstr, 2,0);
- sprintf( coefsigstr, "%*.*f",1,2,coefsig[i]);
- LabelGraphWindow(800,count,coefsigstr, 2,0);
- count = count - 50;
- }
- settextjustifyXX(0,0);
- sprintf(xstr, "%*.*f",1,2,r);
- strcpy(tempstr,"R Value = ");
- strcat(tempstr, xstr );
- LabelGraphWindow(30,count,tempstr,0,0);
- count -= 50;
- sprintf(xstr, "%*.*f",1,2,rsq);
- strcpy(tempstr,"R Squared Value = ");
- strcat(tempstr, xstr );
- LabelGraphWindow(30,count,tempstr,0,0);
- count -= 50;
- sprintf(xstr, "%*.*f",1,2,see);
- strcpy(tempstr,"SEE = ");
- strcat(tempstr, xstr );
- LabelGraphWindow(30,count,tempstr,0,0);
- setlinestyleXX(0,0,1);
-
-
-
- SetCurrentWindow(3);
- ClearWindow();
- SetAxesType(0,0);
- AutoAxes(xdata,ydata,numobs,1);
- LinePlotData(xdata, ydata, numobs, 1,0);
- SelectColor(3);
- TitleWindow("ORIGINAL DATA - SOLID, FITTED DATA - DOTTED");
- TitleXAxis("INDEPENDENT VARIABLE");
- TitleYAxis("DEPENDENT VARIABLE");
- for (i = 0; i <= numobs-1; i++) {
- ydata[i] = yest[i];
- }
- LinePlotData(xdata, ydata, numobs, 1,3);
- setlinestyleXX(1,0,1);
- DrawGrid(10);
- setlinestyleXX(0,0,1);
-
- SetCurrentWindow(4);
- ClearWindow();
- SetAxesType(0,0);
- for (i = 0; i <= numobs-1; i++){
- ydata[i] = resid[i];
- }
- AutoAxes(xdata,ydata,numobs,1);
- BargraphData(xdata, ydata, numobs, 0.05, 1,1);
- SelectColor(3);
- TitleWindow("CURVE FIT ERROR ANALYSIS");
- TitleXAxis("INDEPENDENT VARIABLE");
- TitleYAxis("RESIDUALS");
- setlinestyleXX(1,0,1);
- DrawGridY(10);
-
- SetCurrentWindow(5);
- LabelGraphWindow(30,200,"MORE <Y or N> ?",0,0);
- ch = getch();
- ch = toupper(ch);
- chstr[0] = ch;
- LabelGraphWindow( 850, 200, chstr,0,0);
-
- } while ( ch != 'N' );
-
- CloseSEGraphics();
-
-
- }