Tugas Program Lagrange
Tugas Program Lagrange
METODE NUMERIK
NAMA
: FARHANI
NIM
1.
Dari fungsi y=f(x), diberikan tiga buah titik data dalam bentuk tabel :
X
1,5709
1,5727
1,5751
Tentukkan f(3,5) dengan polinom lagrange derajat 2. Gunakan lima angka bena.
Jawab :
Polinom derajat 2. n=2
Lo(x)
)(
)(
(1,5709)
=
L1(x)
(1,5709)
(
)(
)(
=
=
P2(3.5) =
(1,5727)
(1,5727)
=
L2(x)
(1,5709)
(1,5727)
(
)(
)(
(1,5751)
(1,5751)
(1,5751)
(1,5709) +
(1,5709) +
(1,5751)
begin
//memproses nilai ke program
n:=StrToInt(Edit3.Text);
for i:=1 to n+1 do
begin
x[i]:=StrToFloat(StringGrid1.Cells[i,0]);
y[i]:=StrToFloat(StringGrid1.Cells[i,1]);
end;
xb:=StrToFloat(Edit1.Text);
//algoritma dari lagrange mulai dijalankan
L:=0;
for i:=1 to n+1 do
begin
Pi:=1;
for j:=1 to n+1 do
if(i<>j) then
Pi:=Pi*((xb-x[j])/(x[i]-x[j]));
L:=L+y[i]*pi;
end;
Edit2.Text:=FloatToStr(L);
end;
procedure TPROGRAM_LAGRANGE.Button3Click(Sender: TObject);
var
x,y
: t;
n,i
: integer;
begin
//nilai-nilai ketentuan
y[1]:=1.5709;
y[2]:=1.5727;
y[3]:=1.5751;
x[1]:=1;
x[2]:=4;
x[3]:=6;
//menampilkan nilai ke output
n:=StrToInt(Edit3.Text);
for i:=1 to n+1 do
begin
StringGrid1.Cells[i,0]:=FloatToStr(x[i]);
StringGrid1.Cells[i,1]:=FloatToStr(y[i]);
end;
end;
procedure TPROGRAM_LAGRANGE.Button2Click(Sender: TObject);
begin
close; //akhiri program
end;
end.
Output :
2.
1,2].
Hampiri fungsi f(x) = cos x dengan polinom interpolasi derajat tiga di dalam selang [0,0,
Gunakan empat titik, x0 = 0,0 , x1 = 0,4 , x2 = 0,8 dan x3 = 1,2. Perkirakan nilai P3 = (0,5)
0.0
0.4
0.8
1.2
0.921061
0.696707
0.362358
Jawab :
Polinom derajat 3. n=3
Lo(x)
)(
)(
)(
)(
)(
)(
)(
(1)
=
L1(x)
(1)
(1)
(
)(
)(
(
)(
)(
)(
)(
)(
)
)
(0.921061)
(0.921061)
(0.921061)
L2(x)
)(
)(
)(
(
)(
)(
)(
)(
=
L3(x)
)
)
(0.696707)
(0.696707)
(0.696707)
(
)(
)(
)(
)(
)(
)(
)
)(
(0.362358)
(0.362358)
(0.362358)
P3(0.5) =
(1) +
(0.921061) +
(0.696707) +
(0.362358)
Perbandingan
begin
//memproses nilai ke program
n:=StrToInt(Edit3.Text);
for i:=1 to n+1 do
begin
x[i]:=StrToFloat(StringGrid1.Cells[i,0]);
y[i]:=StrToFloat(StringGrid1.Cells[i,1]);
end;
xb:=StrToFloat(Edit1.Text);
//algoritma dari lagrange mulai dijalankan
L:=0;
for i:=1 to n+1 do
begin
Pi:=1;
for j:=1 to n+1 do
if(i<>j) then
Pi:=Pi*((xb-x[j])/(x[i]-x[j]));
L:=L+y[i]*pi;
end;
Edit2.Text:=FloatToStr(L);
end;
procedure TPROGRAM_LAGRANGE.Button3Click(Sender: TObject);
var
x,y
: t;
n,i
: integer;
begin
//nilai-nilai ketentuan
y[1]:=1.5709;
y[2]:=1.5727;
y[3]:=1.5751;
x[1]:=1;
x[2]:=4;
x[3]:=6;
//menampilkan nilai ke output
n:=StrToInt(Edit3.Text);
for i:=1 to n+1 do
begin
StringGrid1.Cells[i,0]:=FloatToStr(x[i]);
StringGrid1.Cells[i,1]:=FloatToStr(y[i]);
end;
end;
procedure TPROGRAM_LAGRANGE.Button2Click(Sender: TObject);
begin
close; //akhiri program
end;
end.
Output :
StringGrid1: TStringGrid;
implementation
{$R *.dfm}
procedure TPROGRAM_LAGRANGE.FormCreate(Sender: TObject);
begin
StringGrid1.Cells[0,0]:=' X ';
StringGrid1.Cells[0,1]:=' Y ';
end;
procedure TPROGRAM_LAGRANGE.Button1Click(Sender: TObject);
var
Pi,L
: real;
n,i,j
: Integer;
x, y : t;
xb
: real;
begin
//memproses nilai ke program
n:=StrToInt(Edit3.Text);
for i:=1 to n+1 do
begin
x[i]:=StrToFloat(StringGrid1.Cells[i,0]);
y[i]:=StrToFloat(StringGrid1.Cells[i,1]);
end;
xb:=StrToFloat(Edit1.Text);
//algoritma dari lagrange mulai dijalankan
L:=0;
for i:=1 to n+1 do
begin
Pi:=1;
for j:=1 to n+1 do
if(i<>j) then
Pi:=Pi*((xb-x[j])/(x[i]-x[j]));
L:=L+y[i]*pi;
end;
Edit2.Text:=FloatToStr(L);
end;
procedure TPROGRAM_LAGRANGE.Button3Click(Sender: TObject);
var
x,y
: t;
n,i
: integer;
begin
//nilai-nilai ketentuan
y[1]:=1;
y[2]:=0.921061;
y[3]:=0.696707;
y[4]:=0.362358;
x[1]:=0.0;
x[2]:=0.4;
x[3]:=0.8;
x[4]:=1.2;
Output :