Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
18
Добавлен:
02.05.2014
Размер:
5.56 Кб
Скачать
#include <vcl.h>
#include <math.h>
#pragma hdrstop
#include "frmmain.h"
#pragma package(smart_init)
#pragma link "RzButton"
#pragma link "RzCmboBx"
#pragma link "RzEdit"
#pragma link "RzPanel"
#pragma link "RzSpnEdt"
#pragma resource "*.dfm"

bool waserrors = false;
int _matherr(struct _exception *e) {
    waserrors = true;
    return 1;
}

__fastcall DownableFunction::DownableFunction(FunctionValue value, FunctionDerivative dx, FunctionDerivative dy, ItitializeParams init)
    : m_Function(value), m_Initialize(init), m_DerivativeX(dx), m_DerivativeY(dy) {}

TMainForm *MainForm;
__fastcall TMainForm::TMainForm(TComponent* Owner) : TForm(Owner) {
    FunctionList->Items->AddObject("f(x)=A*x+B*y+e^(C*x^2+D*y^2)", (TObject*)(new DownableFunction(&ComplexFunction, &ComplexFunctionDX, &ComplexFunctionDY, &ComplexInitialize)));
    FunctionList->ItemIndex = 0;
    if (FunctionList->OnChange) FunctionList->OnChange(this);
}
double __fastcall TMainForm::ComplexFunction(double &x, double &y) { return m_A*x+m_B*y+exp(m_C*x*x+m_D*y*y); }
double __fastcall TMainForm::ComplexFunctionDX(int n, double x, double y) { return m_A+2.0*m_C*x*exp(m_C*x*x+m_D*y*y); }
double __fastcall TMainForm::ComplexFunctionDY(int n, double x, double y) { return m_B+2.0*m_D*y*exp(m_C*x*x+m_D*y*y); }
void __fastcall TMainForm::ComplexInitialize() {
    AParamEdit->Min = -1.0e299; AParamEdit->Max = 1.0e299;
    AParamEdit->Value = 1.0;
    BParamEdit->Min = -1.0e299; BParamEdit->Max = 1.0e299;
    BParamEdit->Value = -1.4;
    CParamEdit->Min = -1.0e299; BParamEdit->Max = 1.0e299;
    CParamEdit->Value = 0.01;
    DParamEdit->Min = -1.0e299; BParamEdit->Max = 1.0e299;
    DParamEdit->Value = 0.11;
    X0Edit->Min = -1.0e299; X0Edit->Max = 1.0e299;
    Y0Edit->Min = -1.0e299; Y0Edit->Max = 1.0e299;
    X0Edit->Value = 1.0; Y0Edit->Value = 1.0;
}
void __fastcall TMainForm::PrepareData() {
    m_A = AParamEdit->Value;
    m_B = BParamEdit->Value;
    m_C = CParamEdit->Value;
    m_D = DParamEdit->Value;
    m_X0 = X0Edit->Value;
    m_Y0 = Y0Edit->Value;
    m_Eps = pow(10.0, EpsEdit->Value);
    m_Function = ((DownableFunction*)(FunctionList->Items->Objects[FunctionList->ItemIndex]))->m_Function;
    m_DerivativeX = ((DownableFunction*)(FunctionList->Items->Objects[FunctionList->ItemIndex]))->m_DerivativeX;
    m_DerivativeY = ((DownableFunction*)(FunctionList->Items->Objects[FunctionList->ItemIndex]))->m_DerivativeY;
    waserrors = false;
    ErrorLabel->Visible = false;
}
void __fastcall TMainForm::ValidateData() {
    ErrorLabel->Visible = waserrors;
}
void __fastcall TMainForm::FunctionListChange(TObject *Sender) {
    ((DownableFunction*)(FunctionList->Items->Objects[FunctionList->ItemIndex]))->Initialize();
}
long double __fastcall sqr(long double a) { return a*a; }
void __fastcall TMainForm::EvaluateButtonClick(TObject *Sender) {
    this->PrepareData();
    OutputGrid->ColCount = 8;
    OutputGrid->RowCount = 3;
    OutputGrid->FixedRows = 1;
    OutputGrid->ColWidths[0] = 80; OutputGrid->Cells[0][0] = " x*";
    OutputGrid->ColWidths[1] = 80; OutputGrid->Cells[1][0] = " y*";
    OutputGrid->ColWidths[2] = 80; OutputGrid->Cells[2][0] = " f(x*, y*)";
    OutputGrid->ColWidths[3] = 80; OutputGrid->Cells[3][0] = " df/dx|(x*, y*)";
    OutputGrid->ColWidths[4] = 80; OutputGrid->Cells[4][0] = " df/dy|(x*, y*)";
    OutputGrid->ColWidths[5] = 80; OutputGrid->Cells[5][0] = " |grad f(x*, y*)|";
    OutputGrid->ColWidths[6] = 65; OutputGrid->Cells[6][0] = " ak";
    OutputGrid->ColWidths[7] = 50; OutputGrid->Cells[7][0] = " count";

    double xx = m_X0, yy = m_Y0;
    double dx = m_DerivativeX(1, xx, yy), dy = m_DerivativeY(1, xx, yy);
    double ak = 0.5;
    long int count = 0;
    while ((sqrt(sqr(dx)+sqr(dy))>m_Eps)) {
        double x0 = xx, y0 = yy;
        xx -= ak*dx;
        yy -= ak*dy;
        if (m_Function(x0, y0)<m_Function(xx, yy)) {
            xx = x0;
            yy = y0;
            ak *= 0.5;
            if (ak<m_Eps) break;
        }
        dx = m_DerivativeX(1, xx, yy);
        dy = m_DerivativeY(1, xx, yy);
        count++;
    }

    OutputGrid->Cells[0][1] = FormatFloat("0.000000E+00", xx);
    OutputGrid->Cells[1][1] = FormatFloat("0.000000E+00", yy);
    OutputGrid->Cells[2][1] = FormatFloat("0.000000E+00", m_Function(xx, yy));
    OutputGrid->Cells[3][1] = FormatFloat("0.000000E+00", dx);
    OutputGrid->Cells[4][1] = FormatFloat("0.000000E+00", dy);
    OutputGrid->Cells[5][1] = FormatFloat("0.000000E+00", sqrt(sqr(dx)+sqr(dy)));
    OutputGrid->Cells[6][1] = FormatFloat("0.0000E+00", ak);
    OutputGrid->Cells[7][1] = IntToStr(count);

    AnsiString str = "";
    str.printf("%lg", xx);
    OutputGrid->Cells[0][2] = str;
    str.printf("%lg", yy);
    OutputGrid->Cells[1][2] = str;
    str.printf("%lg", m_Function(xx, yy));
    OutputGrid->Cells[2][2] = str;
    str.printf("%lg", dx);
    OutputGrid->Cells[3][2] = str;
    str.printf("%lg", dy);
    OutputGrid->Cells[4][2] = str;
    str.printf("%lg", sqrt(sqr(dx)+sqr(dy)));
    OutputGrid->Cells[5][2] = str;
    str.printf("%lg", ak);
    OutputGrid->Cells[6][2] = str;
    OutputGrid->Cells[7][2] = "";

    this->ValidateData();
}
void __fastcall TMainForm::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift) {
    if (Key==16) AuthorLabel->Visible = true;
}
void __fastcall TMainForm::FormKeyUp(TObject *Sender, WORD &Key, TShiftState Shift) {
    AuthorLabel->Visible = false;
}

Соседние файлы в папке Лабораторная работа №5