0% found this document useful (0 votes)
52 views11 pages

Tugas Program Lagrange

This document contains code for a program that uses Lagrange polynomial interpolation to estimate values of functions. It includes code to: 1) Calculate f(3.5) using data points (1,1.5709), (4,1.5727), (6,1.5751) and Lagrange polynomials of degree 2. 2) Estimate the value of cos(0.5) using data points (0,1), (0.4,0.921061), (0.8,0.696707), (1.2,0.362358) and a Lagrange polynomial of degree 3. 3) Display the data points and calculate the interpolating polynomials using

Uploaded by

Farhan S. Adul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views11 pages

Tugas Program Lagrange

This document contains code for a program that uses Lagrange polynomial interpolation to estimate values of functions. It includes code to: 1) Calculate f(3.5) using data points (1,1.5709), (4,1.5727), (6,1.5751) and Lagrange polynomials of degree 2. 2) Estimate the value of cos(0.5) using data points (0,1), (0.4,0.921061), (0.8,0.696707), (1.2,0.362358) and a Lagrange polynomial of degree 3. 3) Display the data points and calculate the interpolating polynomials using

Uploaded by

Farhan S. Adul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

TUGAS

METODE NUMERIK

NAMA

: FARHANI

NIM

: DBC 113 124

JURUSAN TEKNIK INFORMATIKA


FAKULTAS TEKNIK
UNIVERSITAS PALANGKA RAYA
2014

TUGAS POLINOM LAGRANGE

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) +

= 0,1309 + 1,6382 + 0,19688 = 1,5722

(1,5751)

Kode Program Delphi :


unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, ExtCtrls;
type
t = array [1..100] of real;
TPROGRAM_LAGRANGE = class(TForm)
Edit1: TEdit;Edit2: TEdit;
Button1: TButton;
StringGrid1: TStringGrid;
Button2: TButton;
Label1: TLabel;
Label2: TLabel;
Shape1: TShape;
Label3: TLabel;
Button3: TButton;
Edit3: TEdit;
Label4: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
PROGRAM_LAGRANGE: TPROGRAM_LAGRANGE;
Edit1: TEdit;
Edit2: TEdit;
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.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)

dan bandingkan dengan nilai sejatinya.


X

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)

= (-0.0546875) + (0.8203125) (0.921061) + (0.2734375)(0.696707) +


(-0.0390625) (0.362358)
= (-0.0546875)+(0.7555578515625)+(0.1905058203125)+(-0.014154609375)
= 0.877221563

Perbandingan

= Nilai Sejatinya - Nilai P3


= cos (0.5) - 0.877221563
= 0.877582561 - 0.877221563
= 0.000360998

Kode Program Delphi No. 1 :


unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, ExtCtrls;
type
t = array [1..100] of real;
TPROGRAM_LAGRANGE = class(TForm)
Edit1: TEdit;Edit2: TEdit;
Button1: TButton;
StringGrid1: TStringGrid;
Button2: TButton;
Label1: TLabel;
Label2: TLabel;
Shape1: TShape;
Label3: TLabel;
Button3: TButton;
Edit3: TEdit;
Label4: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
PROGRAM_LAGRANGE: TPROGRAM_LAGRANGE;
Edit1: TEdit;
Edit2: TEdit;
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.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 :

Kode Program Delphi No. 2:


unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, ExtCtrls;
type
t = array [1..100] of real;
TPROGRAM_LAGRANGE = class(TForm)
Edit1: TEdit;Edit2: TEdit;
Button1: TButton;
StringGrid1: TStringGrid;
Button2: TButton;
Label1: TLabel;
Label2: TLabel;
Shape1: TShape;
Label3: TLabel;
Button3: TButton;
Edit3: TEdit;
Label4: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
PROGRAM_LAGRANGE: TPROGRAM_LAGRANGE;
Edit1: TEdit;
Edit2: TEdit;

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;

//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 :

You might also like