/*************************************************** * Parabolic Least Squares Demonstration Program * * ------------------------------------------------ * * Reference: BASIC Scientific Subroutines, Vol. II * * By F.R. Ruckdeschel, BYTE/McGRAWW-HILL, 1981 [1].* * * * C++ version by J-P Moreau, Paris * * with possibility of a graph. * * (to be used with lstsqr2.mak and graph2d.cpp) * * (www.jpmoreau.fr) * * ------------------------------------------------ * * This program calculates a parabolic least squares* * fit to a given data set. * * * * INSTRUCTIONS * * ------------ * * * * The number of data coordinates provided must be * * greater than three. Otherwise, a divide by zero * * error may result. * * * * SAMPLE DATA: * * * * Number of data points : 4 * * * * X[0]=1 Y[0]=1 * * X[1]=2 Y[1]=4 * * X[2]=3 Y[2]=9 * * X[3]=5 Y[3]=24.95 * * * * Fitted equation is: * * Y = -0.017727 + 0.022045 X + 0.994318 X^2 * * * * Standard deviation of fit: 0.004767 * * * * (A graph is displayed). * ***************************************************/ #include #include #include #include HDC hdc; RECT rect; HPEN hpen, hpenOld; //Functions used here of Graph2D.cpp void Conversion(double,double,int *,int *); void MinMax(int,float *,double *,double *); void Initwindow(int,double,double,double,double); void MoveXY(double,double); void LineXY(double,double); void Legendes (int,char *,char *,char *,int,int,char *,int,int,char *); //"home made" graphic commands for hdc environment used by above functions void DrawPixel(int ix,int iy) { //sorry, no other available command found Rectangle(hdc,rect.left+ix,rect.top+iy, rect.left+ix+2,rect.top+iy+1); } void Swap(int *i1,int *i2) { int it; it=*i1; *i1=*i2; *i2=it; } void DrawLine(int ix1,int iy1,int ix2,int iy2) { int i,il,ix,iy; double dx,dy; if (ix2 0 && a != 0) strcat(titre,"+ "); if (fabs(b) != 1) { sprintf(buf,"%f X ",b); strcat(titre,buf); } else if (b > 0) strcat(titre," X "); else strcat(titre," -X "); } if (c != 0) { if (c > 0 && (a != 0 || b != 0)) strcat(titre,"+ "); if (fabs(c) != 1) { sprintf(buf,"%f X^2 ",c); strcat(titre,buf); } else if (c > 0) strcat(buf," X^2 "); else strcat(titre," -X^2 "); } //Display graph caption, names of axes and //standard deviation, d sprintf(buf," Standard deviation = %f",d); Legendes(10,titre,"X","Y",50,50,buf,0,0,""); } void Lstsqr2() { Data(); Least_Square(n); Draw_graph(); } //Handle window Paint and Destroy messages long FAR PASCAL /*_export*/ WndProc(HWND hwnd, UINT message, UINT wParam, LONG lParam) { PAINTSTRUCT ps; switch (message) { case WM_PAINT: hdc = BeginPaint(hwnd, &ps); GetClientRect(hwnd, &rect); hpen = CreatePen(PS_SOLID, 1, RGB(0, 0, 255)); hpenOld = SelectObject(hdc,hpen); Lstsqr2(); SelectObject(hdc,hpenOld); DeleteObject(hpen); EndPaint(hwnd, &ps); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); } // main program int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow) { static char szAppName[] = "Lstsqr2"; HWND hwnd; MSG msg; WNDCLASS wndclass; if (!hPrevInstance) { wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szAppName; RegisterClass(&wndclass); } hwnd = CreateWindow(szAppName, // window class name " PARABOLIC LEAST SQUARES", // window caption WS_OVERLAPPEDWINDOW, // window style CW_USEDEFAULT, // initial x position CW_USEDEFAULT, // initial y position CW_USEDEFAULT, // initial x size CW_USEDEFAULT, // initial y size NULL, // parent window handle NULL, // window menu handle hInstance, // program instance handle NULL); // creation parameters ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } //end of file lstsqr2.cpp