📄 polyfitdemo.cpp
字号:
//---------------------------------------------------------------------------
#pragma hdrstop
#include "CPolyfit.cpp"
#include "polyfitdemo.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cspin"
#pragma resource "*.dfm"
TForm1 *Form1;
using namespace std; //std::vector;
//---------------------------------------------------------------------------
bool __fastcall CData::ReadData()
/*
=============================================================================
ReadData function reads the data from an existing text file. Save the x, y
values to the vectors X and Y.
Return:
true when success
false when fails
=============================================================================
*/
{
if(Owner->OpenDialog1->Execute())
{
X.clear();
Y.clear();
A.clear();
ifstream In(Owner->OpenDialog1->FileName.c_str());
double x, y;
while(In)
{
In >> x;
In >> y;
X.push_back(x);
Y.push_back(y);
}
In.close();
return true;
}
return false;
}
//-----------------------------------------------------------------------------
double __fastcall CData::F(double x)
{
int n = A.size();
double ret = 0;
for(int i = 0; i < n; ++i)
{
if(x == 0)
ret += A[i];
else
ret += A[i] * NSUtility::Pow(x, i);
}
return ret;
}
//---------------------------------------------------------------------------
double __fastcall CData::RootMeanSquare(vector<long double> &x, vector<long double> &y)
{
double ret = 0;
double t;
unsigned int n = x.size();
for(unsigned int i = 0; i < n; ++i)
{
t = y[i] - F(x[i]);
ret += t * t;
}
return sqrt(ret) / n;
}
//----------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall CData::RemoveSignificant(vector<long double> &x, vector<long double> &y, double rms)
{
// remove elements where y[i] - f(x[i]) > 1.0
for(unsigned int i = 0; i < N; ++i)
{
if(fabs(y[i] - F(x[i])) > 10 * rms)
{
x[i] = -100.0;
y[i] = -100.0;
}
}
// remove all -100, s
x.erase(remove(x.begin(), x.end(), -100.0), x.end());
y.erase(remove(y.begin(), y.end(), -100.0), y.end());
}
//----------------------------------------------------------------------------
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Data = new CData(this);
}
//---------------------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
delete Data;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnReadClick(TObject *Sender)
{
Data->ReadData();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnCalcClick(TObject *Sender)
{
TCursor oldcursor = Screen->Cursor;
Screen->Cursor = crHourGlass;
try
{
int nOrder = CSpinEdit1->Value;
double correl;
CPolyFit<long double> *PolyFitObj = new CPolyFit<long double>();
Data->A.clear();
for(int i = 0; i < nOrder; ++i)
Data->A.push_back(0.0);
correl = PolyFitObj->PolyFit (Data->X, Data->Y, Data->A);
ListBox1->Items->Add(AnsiString("Corr Coefficient = ") + correl);
ListBox1->Items->Add("");
for(size_t i = 0; i < Data->A.size(); ++i)
{
AnsiString spaces("");
if(i < 100)
spaces += ' ';
if(i < 10)
spaces += ' ';
ListBox1->Items->Add(AnsiString('A') + i + spaces + " = " + float(Data->A[i]));
}
ListBox1->Items->Add("");
ListBox1->Items->Add("=========================================================");
// Data->DrawLine(clRed);
delete PolyFitObj;
}
__finally
{
Screen->Cursor = oldcursor;
}
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -