Integral dengan Metode Trapesium dan Simson

Selasa, 19 Januari 2010


//ingat ini hanya berbagi kode dan belum tentu benar. Saran & kritik sangat dibutuhkan.


unit integral;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;

type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Edit4: TEdit;
Edit5: TEdit;
StringGrid1: TStringGrid;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Edit6: TEdit;
Label6: TLabel;
Label8: TLabel;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
n,a,b,i,j:integer;
fx:array [0..100] of real;
h,c,en,jum,f1x,x,integral:real;
begin
label8.caption:='fx:=exp(-x*x)';
n:=strtoint(edit3.text);
a:=strtoint(edit1.text);
b:=strtoint(edit2.text);
h:=(b-a)/n;
fx[0]:=exp(-a*a);
jum:=fx[0];
x:=a;
stringgrid1.Cells[1,1]:=floattostr(x);
stringgrid1.Cells[2,1]:=floattostr(fx[0]);
for i:=1 to n-1 do
begin
x:=h+x;
fx[i]:=exp(-x*x);
jum:=jum+(2*fx[i]);
stringgrid1.Cells[1,i+1]:=floattostr(x);
stringgrid1.Cells[2,i+1]:=floattostr(fx[i]);
end;
x:=x+h;
fx[n]:=exp(-x*x);
stringgrid1.Cells[1,n+1]:=floattostr(x);
stringgrid1.Cells[2,n+1]:=floattostr(fx[n]);
c:=(b-a)/2;
f1x:=-2*c*exp(-c*c);
en:=(-(b-a)*(b-a)*(b-a)*f1x)/(12*n*n);
integral:=(h/2)*(jum+fx[n]);
edit4.Text:=floattostr(h);
edit5.Text:=floattostr(integral);
edit6.Text:=floattostr(en);
stringgrid1.RowCount:=n+2;
stringgrid1.colCount:=3;

for j:=0 to i+2 do
stringgrid1.Cells[0,j+1]:=inttostr(j);
end;



procedure TForm1.FormCreate(Sender: TObject);
begin
stringgrid1.Cells[0,0]:='No.';
stringgrid1.Cells[1,0]:='x';
stringgrid1.Cells[2,0]:='fx';
end;

procedure TForm1.Button2Click(Sender: TObject);
var
a,b,n,i,j:integer;
fx,x:array [0..100] of real;
jum,h,integral,c,f4x,en:real;
begin
label8.caption:='fx:=exp(-x*x)';
n:=strtoint(edit3.text);
a:=strtoint(edit1.text);
b:=strtoint(edit2.text);
h:=(b-a)/n;
fx[0]:=exp(-a*a);
jum:=fx[0];
x[0]:=a;
for i:=1 to n-1 do
begin
x[i]:=h+x[i-1];
if i mod 2 =1 then
begin
fx[i]:=exp(-x[i]*x[i]);
jum:=jum+4*fx[i];
end
else
begin
fx[i]:=exp(-x[i]*x[i]);
jum:=jum+2*fx[i];
end
end;
x[n]:=h+x[n-1];
fx[n]:=exp(-x[n]*x[n]);
jum:=jum+fx[n];
integral:=(h/3)*(jum);
c:=(b-a)/2;
f4x:=-16*c*exp(-c*c);
en:=-((b-a)*(b-a)*(b-a)*(b-a)*(b-a)* f4x)/(180*n*n*n*n);

//untuk tampilan
for j:=0 to n do
begin
stringgrid1.Cells[0,j+1]:=inttostr(j);
stringgrid1.Cells[1,j+1]:=floattostr(x[j]);
stringgrid1.Cells[2,j+1]:=floattostr(fx[j]);
end;
edit4.Text:=floattostr(h);
edit5.Text:=floattostr(integral);
edit6.Text:=floattostr(en);
stringgrid1.rowCount:=n+2;
stringgrid1.ColCount:=3;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
a,b,n,i,j,k:integer;
fx,x:array [0..100] of real;
jum1,jum2,h,integral,integral1,integral2,c,f4x,en,
en1,en2:real;
begin
label8.caption:='fx:=exp(-x*x)';
n:=strtoint(edit3.text);
a:=strtoint(edit1.text);
b:=strtoint(edit2.text);
h:=(b-a)/n;

//untuk bagian simson 1/3
fx[0]:=exp(-a*a);
jum1:=fx[0];
x[0]:=a;
x[1]:=h+x[0];
fx[1]:=exp(-x[1]*x[1]);
jum1:=jum1+4*fx[1];
x[2]:=h+x[1];
fx[2]:=exp(-x[2]*x[2]);
jum1:=jum1+fx[2];
c:=(b-a)/2;
f4x:=-16*c*exp(-c*c);
en1:=-((b-a)*(b-a)*(b-a)*(b-a)*(b-a)* f4x)/(180*n*n*n*n);
integral1:=(h/3)*(jum1)+ en1;

//bagian simson 3/8
jum2:=fx[2];
for k:=3 to n-1 do
begin
x[k]:=h+x[k-1];
fx[k]:=exp(-x[k]*x[k]);
jum2:=jum2+3*fx[k];
end;
x[n]:=h+x[n-1];
fx[n]:=exp(-x[n]*x[n]);
jum2:=jum2+fx[n];
c:=(b-a)/2;
f4x:=-16*c*exp(-c*c);
en2:=-((b-a)*(b-a)*(b-a)*(b-a)*(b-a)* f4x)/(6480);
integral2:=(b-a)*(jum2)/8 +en2;



integral:=integral1+integral2;
en:=en2;

//untuk tampilan
for j:=0 to n do
begin
stringgrid1.Cells[0,j+1]:=inttostr(j);
stringgrid1.Cells[1,j+1]:=floattostr(x[j]);
stringgrid1.Cells[2,j+1]:=floattostr(fx[j]);
end;
edit4.Text:=floattostr(h);
edit5.Text:=floattostr(integral);
edit6.Text:=floattostr(en);
stringgrid1.rowCount:=n+2;
stringgrid1.ColCount:=3;
end;


procedure TForm1.Button4Click(Sender: TObject);
var
a,b,n,i,j:integer;
fx,x:array [0..100] of real;
jum,h,integral,c,f4x,en:real;
begin
label8.caption:='fx:=exp(-x*x)';
n:=strtoint(edit3.text);
a:=strtoint(edit1.text);
b:=strtoint(edit2.text);
h:=(b-a)/n;
fx[0]:=exp(-a*a);
jum:=fx[0];
x[0]:=a;
for i:=1 to n-1 do
begin
x[i]:=h+x[i-1];
if i mod 2 =1 then
begin
fx[i]:=exp(-x[i]*x[i]);
jum:=jum+3*fx[i];
end
else
begin
fx[i]:=exp(-x[i]*x[i]);
jum:=jum+3*fx[i];
end
end;
x[n]:=h+x[n-1];
fx[n]:=exp(-x[n]*x[n]);
jum:=jum+fx[n];
integral:=(b-a)*(jum)/8;
c:=(b-a)/2;
f4x:=-16*c*exp(-c*c);
en:=-((b-a)*(b-a)*(b-a)*(b-a)*(b-a)* f4x)/(180*n*n*n*n);

//untuk tampilan
for j:=0 to n do
begin
stringgrid1.Cells[0,j+1]:=inttostr(j);
stringgrid1.Cells[1,j+1]:=floattostr(x[j]);
stringgrid1.Cells[2,j+1]:=floattostr(fx[j]);
end;
edit4.Text:=floattostr(h);
edit5.Text:=floattostr(integral);
edit6.Text:=floattostr(en);
stringgrid1.rowCount:=n+2;
stringgrid1.ColCount:=3;
end;


end.

Share this Article on :

2 komentar:

Anonymous mengatakan...

Ijin nyimak n Sedot gan..

ada tugas ginian juga ni dari dosen...
mohon bantuanya

Hikam Adzkiyak mengatakan...

@Anonim:oke, thanks. mohon direview kembali. Didisksikan lagi.

Posting Komentar

LinkWithin

Related Posts Plugin for WordPress, Blogger...
 

© Copyright Ngidup Buat Ngakhirat 2010 -2011 | Design by Herdiansyah Hamzah | Published by Borneo Templates | Powered by Blogger.com.