home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 March / Chip_1998-03_cd.bin / zkuste / matemat / Vyssi / MATH2.ZIP / exmpl-3 / cpp / frmcfit.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-22  |  8.0 KB  |  274 lines

  1. //---------------------------------------------------------------------------
  2. #include <vcl\vcl.h>
  3. #pragma hdrstop
  4.  
  5. #include "frmcfit.h"
  6. #include "math.h"
  7. #include "math2.hpp"
  8. //---------------------------------------------------------------------------
  9. #pragma link "NumLab"
  10. #pragma link "RChart"
  11. #pragma link "Math2"
  12. #pragma resource "*.dfm"
  13.  
  14. TForm1 *Form1;
  15. TCurveFit *Stats1 = new TCurveFit;
  16. const int ChartXRes = 200;
  17.  
  18.  
  19. //---------------------------------------------------------------------------
  20. __fastcall TForm1::TForm1(TComponent* Owner)
  21.     : TForm(Owner)
  22. {
  23. }
  24. //---------------------------------------------------------------------------
  25. void __fastcall TForm1::BButExitClick(TObject *Sender)
  26. {
  27. Close();    
  28. }
  29. //---------------------------------------------------------------------------
  30. void __fastcall TForm1::SBClearClick(TObject *Sender)
  31. {
  32. Stats1->Init();
  33. RChart1->ClearGraf();
  34. RChart1->ShowGraf();
  35. SBNewPnts->Down = false;
  36. LblFormula->Visible = false;
  37. LblParams->Visible = true;
  38. LblParams->Caption = "<---- click here to enter data";
  39. }
  40. //---------------------------------------------------------------------------
  41. void __fastcall TForm1::RChart1MouseDown(TObject *Sender, TMouseButton Button,
  42.     TShiftState Shift, int X, int Y)
  43. {
  44. double rx;
  45. double ry;
  46.  
  47. if (SBNewPnts->Down == true)
  48.   {
  49.   RChart1->M2R (X,Y, rx, ry);
  50.   RChart1->MarkAt (rx,ry,4);
  51.   Stats1->EnterStatValue (rx, ry);
  52.   NLabRXY->Value = Stats1->CorrCoeff;
  53.   RChart1->ShowGraf();
  54.   }
  55. }
  56. //---------------------------------------------------------------------------
  57. void __fastcall TForm1::SBNewPntsClick(TObject *Sender)
  58. {
  59. while ((RChart1->TypeOfLastItem != tkMarkAt) &&
  60.       (RChart1->TypeOfLastItem != tkNone))   // remove any curve from graph
  61.   RChart1->RemoveLastItem();
  62. RChart1->ShowGraf();
  63. LblFormula->Visible = false;
  64. if (SBNewPnts->Down == false)
  65.   {
  66.   LblParams->Caption = "<---- click here to enter data";
  67.   }
  68. else
  69.   {
  70.   LblParams->Caption = "click into chart to enter new data points";
  71.   }
  72. LblParams->Visible = true;
  73. }
  74. //---------------------------------------------------------------------------
  75. void __fastcall TForm1::BButParabolFitClick(TObject *Sender)
  76. {
  77. double  k0, k1, k2;
  78. double  FitQual;
  79. double  xstep;
  80. double  x;
  81. int     i;
  82.  
  83. while ((RChart1->TypeOfLastItem != tkMarkAt) &&
  84.       (RChart1->TypeOfLastItem != tkNone))   // remove any curve from graph
  85.   RChart1->RemoveLastItem();
  86. SBNewPnts->Down = false;
  87. Stats1->CalcParabolFit (k0, k1, k2, FitQual);
  88. NLabFitQual->Value = FitQual;
  89. RChart1->MoveTo (RChart1->RangeLoX,k0+k1*RChart1->RangeLoX+k2*RChart1->RangeLoX*RChart1->RangeLoX);
  90. xstep = (RChart1->RangeHiX-RChart1->RangeLoX) / ChartXRes;
  91. for (i=1; i<=ChartXRes; i++)
  92.   {
  93.   x = RChart1->RangeLoX+i*xstep;
  94.   RChart1->DrawTo (x,k0+k1*x+k2*x*x);
  95.   }
  96. RChart1->ShowGraf();
  97. LblFormula->Caption = "y = k0 + k1*x + k2*x*x";
  98. LblParams->Caption = "k0 = "+strf(k0,1,3)+"    k1 = "+strf(k1,1,3)+"    k2 = "+strf(k2,1,3);
  99. LblFormula->Visible = True;
  100. LblParams->Visible = true;
  101. }
  102. //---------------------------------------------------------------------------
  103. void __fastcall TForm1::BButCalcReciprLineClick(TObject *Sender)
  104. {
  105. double k0, k1;
  106. double FitQual;
  107. double xstep;
  108. int    i;
  109. double x;
  110. double denom;
  111.  
  112. while ((RChart1->TypeOfLastItem != tkMarkAt) &&
  113.        (RChart1->TypeOfLastItem != tkNone)) // remove any curve from graph
  114.   RChart1->RemoveLastItem();
  115. SBNewPnts->Down = false;
  116. Stats1->CalcReciLinFit (k0, k1, FitQual);
  117. NLabFitQual->Value = FitQual;
  118. denom = k0+k1*RChart1->RangeLoX;
  119. if (denom != 0)
  120.   {
  121.   RChart1->MoveTo (RChart1->RangeLoX,1/denom);
  122.   }
  123. xstep = (RChart1->RangeHiX-RChart1->RangeLoX) / ChartXRes;
  124. for (i=1; i<=ChartXRes; i++)
  125.   {
  126.   x = RChart1->RangeLoX+i*xstep;
  127.   denom = k0+k1*x;
  128.   if (denom != 0)
  129.     {
  130.     RChart1->DrawTo (x,1/denom);
  131.     }
  132.   }
  133. RChart1->ShowGraf();
  134. LblFormula->Caption = "y = 1/(k0 + k1*x)";
  135. LblParams->Caption = "k0 = "+strf(k0,1,3)+"     k1 = "+strf(k1,1,3);
  136. LblFormula->Visible = true;
  137. LblParams->Visible = true;
  138. }
  139. //---------------------------------------------------------------------------
  140. void __fastcall TForm1::BButHyperClick(TObject *Sender)
  141. {
  142. double k0, k1;
  143. double FitQual;
  144. double xstep;
  145. int    i;
  146. double x;
  147.  
  148. while ((RChart1->TypeOfLastItem != tkMarkAt) &&
  149.        (RChart1->TypeOfLastItem != tkNone))      // remove any curve from graph
  150.   RChart1->RemoveLastItem();
  151. SBNewPnts->Down = false;
  152. Stats1->CalcHyperbolFit (k0, k1, FitQual);
  153. NLabFitQual->Value = FitQual;
  154. if (RChart1->RangeLoX != 0)
  155.   {
  156.   RChart1->MoveTo (RChart1->RangeLoX,k0+k1/RChart1->RangeLoX);
  157.   }
  158. else
  159.   {
  160.   RChart1->MoveTo (RChart1->RangeLoX,MaxReal);
  161.   }
  162. xstep = (RChart1->RangeHiX-RChart1->RangeLoX) / ChartXRes;
  163. for (i=1; i<=ChartXRes; i++)
  164.   {
  165.   x = RChart1->RangeLoX+i*xstep;
  166.   if (x != 0)
  167.     {
  168.     RChart1->DrawTo (x,k0+k1/x);
  169.     }
  170.   else
  171.     {
  172.     RChart1->DrawTo (x,MaxReal);
  173.     }
  174.   }
  175. RChart1->ShowGraf();
  176. LblFormula->Caption = "y = k0 + k1/x";
  177. LblParams->Caption = "k0 = "+strf(k0,1,3)+"    k1 = "+strf(k1,1,3);
  178. LblFormula->Visible = True;
  179. LblParams->Visible = true;
  180. }
  181. //---------------------------------------------------------------------------
  182. void __fastcall TForm1::BButLogFitClick(TObject *Sender)
  183. {
  184. double    k0, k1;
  185. double    FitQual;
  186. double    xstep;
  187. int       i;
  188. double    x;
  189.  
  190. while ((RChart1->TypeOfLastItem != tkMarkAt) &&
  191.        (RChart1->TypeOfLastItem != tkNone))  // remove any curve from graph 
  192.   RChart1->RemoveLastItem();
  193. SBNewPnts->Down = false;
  194. Stats1->CalcLogFit (k0, k1, FitQual);
  195. NLabFitQual->Value = FitQual;
  196. if (RChart1->RangeLoX > 0)
  197.   {
  198.   RChart1->MoveTo (RChart1->RangeLoX,k0+k1*log(RChart1->RangeLoX));
  199.   }
  200. else
  201.   {
  202.   RChart1->MoveTo (0, -MaxReal);
  203.   }
  204. xstep = (RChart1->RangeHiX-RChart1->RangeLoX) / ChartXRes;
  205. for (i=1; i<=ChartXRes; i++)
  206.   {
  207.   x = RChart1->RangeLoX+i*xstep;
  208.   if (x > 0)
  209.     {
  210.     RChart1->DrawTo (x,k0+k1*log(x));
  211.     }
  212.   }
  213. RChart1->ShowGraf();
  214. LblFormula->Caption = "y = k0 + k1*ln(x)";
  215. LblParams->Caption = "k0 = "+strf(k0,1,3)+"    k1 = "+strf(k1,1,3);
  216. LblFormula->Visible = True;
  217. LblParams->Visible = true;
  218. }
  219. //---------------------------------------------------------------------------
  220. void __fastcall TForm1::BButGaussFitClick(TObject *Sender)
  221. {
  222. double    k0, k1, k2;
  223. double    FitQual;
  224. double    xstep;
  225. int       i;
  226. double    x;
  227.  
  228. while ((RChart1->TypeOfLastItem != tkMarkAt) &&
  229.        (RChart1->TypeOfLastItem != tkNone))  // remove any curve from graph
  230.   RChart1->RemoveLastItem();
  231. SBNewPnts->Down = false;
  232. Stats1->CalcGaussFit (k0, k1, k2, FitQual);
  233. NLabFitQual->Value = FitQual;
  234. x = RChart1->RangeLoX;
  235. RChart1->MoveTo (x,k0*exp(-(x-k1)*(x-k1)/k2));
  236. xstep = (RChart1->RangeHiX-RChart1->RangeLoX) / ChartXRes;
  237. for (i=1; i<=ChartXRes; i++)
  238.   {
  239.   x = RChart1->RangeLoX+i*xstep;
  240.   RChart1->DrawTo (x,k0*exp(-(x-k1)*(x-k1)/k2));
  241.   }
  242. RChart1->ShowGraf();
  243. LblFormula->Caption = "y = k0*exp(-sqr(x-k1)/k2)";
  244. LblParams->Caption = "k0 = "+strf(k0,1,3)+"    k1 = "+strf(k1,1,3)+"    k2 = "+strf(k2,1,3);
  245. LblFormula->Visible = True;
  246. LblParams->Visible = true;
  247. }
  248. //---------------------------------------------------------------------------
  249. void __fastcall TForm1::BButLinFitClick(TObject *Sender)
  250. {
  251. double    k,d;
  252. double    FitQual;
  253.  
  254. while ((RChart1->TypeOfLastItem != tkMarkAt) &&
  255.        (RChart1->TypeOfLastItem != tkNone))  // remove any curve from graph
  256.   RChart1->RemoveLastItem();
  257. SBNewPnts->Down = false;
  258. Stats1->CalcLinFit (k, d, FitQual);
  259. NLabFitQual->Value = FitQual;
  260. RChart1->MoveTo (RChart1->RangeLoX,k*RChart1->RangeLoX+d);
  261. RChart1->DrawTo (RChart1->RangeHiX,k*RChart1->RangeHiX+d);
  262. RChart1->ShowGraf();
  263. LblFormula->Caption = "y = k*x + d";
  264. LblParams->Caption = "k = "+strf(k,1,3)+"    d = "+strf(d,1,3);
  265. LblFormula->Visible = True;
  266. LblParams->Visible = true;
  267. }
  268. //---------------------------------------------------------------------------
  269. void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift, int X,
  270.     int Y)
  271. {
  272. Screen->Cursor = crDefault;
  273. }
  274. //---------------------------------------------------------------------------