Lagi - lagi... tugas kuliah di upload ke blog. Ya nggak apa - apa berbagi ilmu dengan sesama, barangkali temen - temen membutuhkan materi. Ini saya punya program golden section dengan menggunakan Delphi 7. Dengan coding yang simpel dan mudah dipelajari. Silahkan yang mau copy, bisa diambil dibawah ini.
SOURCE
CODE PROGRAM GOLDEN SECTION
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants,
Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Button1: TButton;
Label4: TLabel;
Label5: TLabel;
Edit4: TEdit;
Label6: TLabel;
Button2: TButton;
iterasi: TListBox;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
a,b,l,e,p,x1,x2,fx1,fx2,hasil : real;
i : Integer;
ulang : boolean;
implementation
{$R *.dfm}
procedure
TForm1.Button2Click(Sender: TObject);
begin
a := -1;
b := 2;
p := 0.382;
l := b - a;
Edit4.Text :=
FloatToStr(l);
x1 := a + (p *
l);
Edit1.Text :=
FloatToStr(x1);
x2 := a +
(1-p) * l;
Edit2.Text :=
FloatToStr(x2);
Edit3.Text :=
FloatToStr(p);
end;
procedure
TForm1.Button1Click(Sender: TObject);
begin
iterasi.Clear;
e := 0.01;
ulang := true;
i := 0;
l := b-a;
x1 := a + p *
l;
x2 := a +
(1-p) * l;
iterasi.Items.Add('E
= '+FloatToStr(e));
iterasi.Items.Add('l
= '+FloatToStr(l));
iterasi.Items.Add('x1
= '+FloatToStr(x1));
iterasi.Items.Add('x2
= '+FloatToStr(x2));
iterasi.Items.Add('');
iterasi.Items.Add('');
while (ulang =
true) do
begin
i := i + 1;
fx1 := x1*x1*x1 + 2*x1*x1 + 6;
fx2 := x2*x2*x2 + 2*x2*x2 + 6;
iterasi.Items.Add('');
iterasi.Items.Add('Iterasi =
'+IntToStr(i));
iterasi.Items.Add('f(x1) =
'+FloatToStr(fx1));
iterasi.Items.Add('f(x2) =
'+FloatToStr(fx2));
if (fx1 > fx2) then
begin
a :=x1;
x1 := x2;
x2 := a + (1 -p) * (b - a);
iterasi.Items.Add('f(x1) < f(x2)');
iterasi.Items.Add('a =
'+FloatToStr(a));
iterasi.Items.Add('f(x1) =
'+FloatToStr(x1));
iterasi.Items.Add('f(x2) =
'+FloatToStr(x2));
iterasi.Items.Add('');
end
else
if (fx1 < fx2) then
begin
b := x2;
x2 := x1;
x1 := a + p * (b - a);
iterasi.Items.Add('f(x1) > f(x2)');
iterasi.Items.Add('b =
'+FloatToStr(b));
iterasi.Items.Add('x2 =
'+FloatToStr(x2));
iterasi.Items.Add('x1 = '+FloatToStr(x1));
iterasi.Items.Add('');
end
else
begin
b := x2;
a := x1;
x1 := a + p * l;
x2 := a + (1 - p) * l;
iterasi.Items.Add('f(x1) = f(x2)');
iterasi.Items.Add('b =
'+FloatToStr(b));
iterasi.Items.Add('a =
'+FloatToStr(a));
iterasi.Items.Add('x1 =
'+FloatToStr(x1));
iterasi.Items.Add('x2 =
'+FloatToStr(x2));
end;
if ((b-a) < e) then
begin
iterasi.Items.Add('');
iterasi.Items.Add('berhenti (
'+FloatToStr(b-a)+' < '+FloatToStr(e)+' )');
ulang := false;
end
else
iterasi.Items.Add('repeat (
'+FloatToStr(b-a)+' > '+FloatToStr(e)+ ' ) ');
end;
iterasi.Items.Add('');
iterasi.Items.Add('');
hasil := (a + b) / 2;
iterasi.Items.Add('x* = '+FloatToStr(hasil));
end;
end.
0 komentar:
Post a Comment