Nadesłany przez Tomasz Lubiński, 08 sierpnia 2005 01:00
Kod przedstawiony poniżej przedstawia główną część rozwiązania problemu.Pobierz pełne rozwiązanie.
Jeżeli nie odpowiada Ci sposób formatowania kodu przez autora skorzystaj z pretty printer'a i dostosuj go automatycznie do siebie.
polowienie_d/Metoda.pas:
//www.algorytm.org
//Tomasz Lubiński (c)2001
//Algorytmy numeryczne - Metoda połowienia
//Obliczanie miejsce zerowych funkcji nieliniowych na przykładzie wielomianu w danym przedziale
unit Metoda;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, Grids, Buttons, Math;
type
TForm1 = class(TForm)
Edit1: TEdit;
StringGrid1: TStringGrid;
Label1: TLabel;
BitBtn1: TBitBtn;
BitBtn3: TBitBtn;
Edit2: TEdit;
Label2: TLabel;
Button1: TButton;
Button2: TButton;
Label3: TLabel;
Edit3: TEdit;
Label4: TLabel;
Label5: TLabel;
Edit4: TEdit;
Label8: TLabel;
Edit5: TEdit;
Label6: TLabel;
procedure Edit1Change(Sender: TObject);
procedure w_lewo(Sender: TObject);
procedure w_prawo(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
n: Integer;
prawo: Integer=1;
A: Array of real;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
StringGrid1.ColCount:=1;
StringGrid1.RowCount:=2;
Edit1Change(Form1);
end;
//Zrobienie siatki i ustawienie tablic dynamicznych
procedure TForm1.Edit1Change(Sender: TObject);
var i:Integer;
begin
n:=StrToInt(Edit1.Text);
if n<1 then begin ShowMessage('Wielomian musi być conajmniej pierwszego stopnia'); exit; end;
StringGrid1.ColCount:=n+1;
SetLength(A, n+1);
for i:=0 to n+1 do StringGrid1.Cells[i,1]:='';
for i:=0 to n do StringGrid1.Cells[i,0]:='x^'+IntToStr(n-i);
end;
//Nawigacja
procedure TForm1.w_lewo(Sender: TObject);
begin
if prawo>0 then
begin
prawo:=prawo-1;
StringGrid1.Col:=prawo;
end;
end;
procedure TForm1.w_prawo(Sender: TObject);
begin
if prawo<n then
begin
prawo:=prawo+1;
StringGrid1.Col:=prawo;
end;
end;
procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
begin
prawo:=ACol;
end;
//wpis
procedure TForm1.Button1Click(Sender: TObject);
var i:Integer;
begin
Val(Edit2.Text,A[n-prawo],i);
if i<>0 then ShowMessage('Blad podczas wpisu') Else StringGrid1.Cells[prawo,1]:=Edit2.Text;
end;
function w(k:Integer; x:Real):Real; //algorytm Hornera - obliczanie wartości wielomianu
begin
if k=n then w:=A[k] else w:=w(k+1,x)*x+A[k]
end;
procedure TForm1.Button2Click(Sender: TObject);
var i,j: Integer;
p,k,s: Real;
begin
s:=0;
Val(Edit3.Text,p,i);
if i<>0 then begin ShowMessage('Źle wpisany początek przedziału'); exit; end;
Val(Edit4.Text,k,i);
if i<>0 then begin ShowMessage('Źle wpisany koniec przedziału'); exit; end;
if k<p then begin ShowMessage('Koniec przedziału jest mniejszy od początku!'); exit; end;
j:=StrToInt(Edit5.Text);
if j<1 then begin ShowMessage('Nieprawidłowa liczba iteracji'); exit; end;
if w(0,p)*w(0,k)>=0 then begin Label8.Caption:='Przedział nie spełnia założeń'; exit; end;
for i:=1 to j do
begin
s:=(p+k)/2;
if (w(0,p)*w(0,s)<0) and (w(0,s)*w(0,k)>0) then begin k:=s; continue; end else
if (w(0,p)*w(0,s)>0) and (w(0,s)*w(0,k)<0) then begin p:=s; continue; end else
if w(0,s)=0 then break else
begin Label8.Caption:='Przedział nie spełnia założeń'; exit; end;
end;
if w(0,s)=0 then Label8.Caption:='Dokładny pierwiastek wynosi '+FloatToStr(s) else
Label8.Caption:='Przybliżony pierwiastek wynosi '+FloatToStr(s);
end;
end.

