//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include<math.h>
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
float f(float x)
{
// return x*x*x + 3*x - 2;
return x*x*x + x - 5;
}
float fdx(float x)
{// return 3*x*x +3;
return 3*x*x +1;
}
float fd2x(float x)
{
return 4*x;
}
//---------------------------------------------------------------------------
void TForm1::modinewton(float x0, float x1, float epsilon, int step)
{
if (abs(x1-x0) > epsilon)
{
ListBox2->Items->Add("x: "+FloatToStr(x1));
ListBox2->Items->Add("f(x): "+FloatToStr(f(x1)));
ListBox2->Items->Add("steps: "+IntToStr(step));
return;
}
const float xc = fdx(x0);
float x1 = x0 - f(x0)/xc;
modinewton(x0, x1, epsilon, step + 1);
}
void __fastcall TForm1::bExe2Click(TObject *Sender)
{
float a = StrToFloat(Edit1->Text);
float b = StrToFloat(Edit2->Text);
float epsilon = StrToFloat(Edit3->Text);
if(f(a)*f(b) < 0)
{
ListBox2->Items->Add("данные неверны");
return;
}
else
{
float x0 = a;
if(f(a)*fd2x(a) < 0)
{
x0 = b;
}
ListBox2->Items->Add("===================================");
modinewton(x0, x1, epsilon, 1);
ListBox2->Items->Add("===================================");
ListBox2->Items->Add("");
}
}
//---------------------------------------------------------------------------
выдает ошибки:
[C++ Error] Unit1.cpp(101): E2238 Multiple declaration for 'x1'
[C++ Error] Unit1.cpp(128): E2451 Undefined symbol 'x1'