Anda di halaman 1dari 55

Regression

and Interpolation

Course : MATH6183 – Scientific Computing


Year : 2021
Outlines

Linear Interpolation
Polynomial Interpolation
Cubic Spline Interpolation
Least-Square Fit
Interpolation VS Curve Fitting
Dalam interpolasi kita membuat
kurva melalui titik-titik data. Disini
kita asumsikan bahwa bahwa titik
data akurat

Sebaliknya, pecocokan kurva


diterapkan pada data yang
mengandung hamburan (noise),
yang disebabkan oleh kesalahan Three attempts to fit a “best”
pengukuran. Di sini kita ingin curve through five data
menemukan kurva yang points:
mendekati data tersebut . Dengan (a) Linear Interpolation
demikian, kurva tidak selalu (b) Polinomial Interpolation
mengenai titik data. (c) Curvilinear interpolation.
Macam Interpolasi

• Interpolasi Beda Terbagi Newton


• Interpolasi Lagrange
• Kuadrat Terkecil
Macam Interpolasi Beda Terbagi Newton

• Interpolasi Linier
Derajat/orde 1  memerlukan 2 titik
Berapa f(x = 1,325) = ?
x f(x)
Memerlukan 2 titik awal :
1 4,5 x=1
2 7.6 x=2
3 9.8
4 11.2
Macam Interpolasi Beda Terbagi Newton

• Interpolasi Kuadratik
Derajat/orde 2  memerlukan 3 titik
x = 1  f(x = 1) = . . . .
x = 2  f(x = 2) = . . . .f (x = 1,325) = ?
x = 3  f(x = 3) = . . . .
Macam Interpolasi Beda Terbagi Newton

• Interpolasi Kubik
Derajat/orde 3  memerlukan 4 titik

• Interpolasi derajat/orde ke-n
 memerlukan n+1 titik

• Semakin tinggi orde yang digunakan untuk


interpolasi hasilnya akan semakin baik
(teliti).
Interpolasi Linier
 Pendekatan interpolasi dengan derajat 1, pada
kenyataannya sama dengan mendekati suatu
harga tertentu melalui garis lurus.
 Untuk memperbaiki kondisi tersebut dilakukan
sebuah interpolasi dengan membuat garis yang
menghubungkan titik yaitu melalui orde 2, orde
3, orde 4, dst, yang sering juga disebut
interpolasi kuadratik, kubik, dst.
Interpolasi Linear
Dalam interpolasi linier, titik taksiran diasumsikan terletak pada garis yang
menghubungkan titik-titik terdekat ke kiri dan kanan. Asumsikan bahwa
titik-titik data x berada dalam urutan menaik, yaitu, xi <xi+1, dan misalkan x
adalah titik sedemikian rupa sehingga xi <x <xi+1. Maka interpolasi linier di
x adalah

Find the linear interpolation at x = 1.5


based on the data x = [0, 1, 2], y = [1, 3,2].
Interpolasi Kuadratik
 Interpolasi orde 2 sering disebut sebagai
interpolasi kuadratik, memerlukan 3 titik data.
 Bentuk polinomial orde ini adalah :
f2(x) = a0 + a1x + a2x2
dengan mengambil:
a0 = b0 – b1x0 + b2x0x1
a1 = b1 – b2x0 + b2x1
a2 = b2
 Sehingga
f2(x) = b0 + b1(x-x0) + b2(x-x0)(x-x1)

Pendekatan dengan Pendekatan dengan


garis linier kelengkungan
dengan b0  f x0 
f x1   f x0 
b1   f x 1 , x0 
x1  x0 
f x2   f x1  f x1   f x0 

b2 
x2  x1  x1  x0 
 f x2 , x1 , x0 
x2  x0 
Interpolasi Kubik
 f3(x) = b0 + b1(x-x0) + b2(x-x0)(x-x1) + b3(x-x0)(x-x1)(x-x2)
dengan:
b0  f x0 
f x1   f x0 
b1   f x 1 , x0 
x1  x0 
f x2   f x1  f x1   f x0 

b2 
f [x2 , x1 ]  f [x1 , x0 ]

x2  x1  x1  x0 
 f x2 , x1 , x0 
x2  x0  x2  x0 
f [x3 , x2 , x1 ]  f [x2 , x1 , x0 ]
b3   f x3 , x2 , x1 , x0 
x3  x0 
Interpolasi Beda Terbagi Newton
 Secara umum:
f1(x) = b0 + b1(x-x0)
f2(x) = b0 + b1(x-x0) + b2(x-x0)(x-x1)
f3(x) = b0 + b1(x-x0) + b2(x-x0)(x-x1) +
b3(x-x0)(x-x1)(x-x2)

fn(x) = b0 + b1(x-x0) + b2(x-x0)(x-x1) +
b3(x-x0)(x-x1)(x-x2) + … +
bn(x-x1)(x-x2)…(x-xn-1)
Dengan:
 b0 = f(x0)

 b1 = f[x1, x0]

 b2 = f[x2, x1, x0]


 bn = f[xn, xn-1, xn-2, . . . ., x0]
Example 1

The data points


lie on a polynomial. Determine the degree of this polynomial by
constructing the divided difference table.
Solution
Here are a few sample calculations used in arriving at the figures in the
table:

From the table we see that the last nonzero coefficient (last nonzero
diagonal term) of Newton’s polynomial is ∇3y3, which is the coefficient of
the cubic term. Hence, the polynomial is a cubic
Example 2
Misalnya, kita memiliki tabel seperti ini, yang memberikan beberapa nilai
fungsi f yang tidak diketahui . Interpolasi menyediakan cara untuk
memperkirakan fungsi pada titik-titik perantara, seperti x = 2,5
Jika fungsi nya adalah
f(x) = -0.001521x6 – 0.003130x5 + 0.07321x4 – 0.3577x3 + 0.2255x2 +
0.9038x

x F(x)
0 0
1 0.8415
2 0.9093
3 0.1411
4 -0.7568
5 -0.9589
6 -0.2794

f(x) = -0.001521x6 – 0.003130x5 + 0.07321x4 – 0.3577x3 + 0.2255x2 +


0.9038x

Substituting x = 2.5, we find that f(2.5) = 0.5965.


Example 3
 Hitung nilai tabel distribusi ‘Student t’ pada derajat bebas dengan  = 4%,
jika diketahui:
t10% = 1,476 ; t2,5% = 2,571
t5% = 2,015 ;t1% = 3,365 dgn interpolasi Newton orde 2 dan orde 3!

Interpolasi Newton Orde 2:  butuh 3 titik


 x0 = 5 f(x0) = 2,015
x1 = 2,5 f(x1) = 2,571
x2 = 1 f(x2) = 3,365
 b0 = f(x0) = 2,015

f x1   f x0  2,571  2,015


b1    0,222
x1  x0  2,5  5
f x2   f x1  f x1   f x0  3,365  2,571 2,571  2,015

b2 
x2  x1  x1  x0  1  2,5

2,5  5
  0,077
x2  x0  15
 f2(x) = b0 + b1(x-x0) + b2(x-x0)(x-x1)
= 2,015 + (-0,222) (4-5) +
0,077 (4-5)(4-2,5)
= 2,121
Interpolasi Newton Orde 3:  butuh 4 titik
 x0 = 5 f(x0) = 2,015
x1 = 2,5 f(x1) = 2,571
x2 = 1 f(x2) = 3,365
x3 = 10 f(x3) = 1,476
 b0 = f(x0) = 2,015
b1 = -0,222  f[x1,x0]
b2 = 0,077  f[x2,x1,x0]
1,476  3,365 3,365  2,571

10  1 1  2,5
 0,077
10  2,5
b3 
10  5
0,043  0,077

5
 0,007
 f3(x) = b0 + b1(x-x0) + b2(x-x0)(x-x1) +
b3(x-x0)(x-x1)(x-x2)
= 2,015 + (-0,222)(4-5) +
0,077 (4-5)(4-2,5) +
(-0,007)(4-5)(4-2,5)(4-1)
= 2,015 + 0,222 + 0,1155 + 0,0315
= 2,153
Problem
Find a polynomial of degree two which
takes the following values:
x 1 2 3 4
f(x) 6 11 18 27

Hence evaluate
P( x  3.5)
Solution
x  x0 x  x0 x  x1  2
P2 ( x)  f 0  f 0  2
 f0.
h 2h
k xk fk f k 2 f k
0 1 6
5
1 2 11 2
7
2 3 18 2
9
3 4 27
 x  1 x  2 
S

P2 ( x )  6  5( x  1)  2
2
 x 2  2x  3

 P2 (3.5)  (3.5)  2(3.5)  3  20.25


2

Alternatively, P ( x)  f  mf  m m  11  2 f .
2 0 0 2 0
2h
mm  11 
 6  5m   2  m  4m  6.
2

xm  x0  mh, 3.5  1  m or m  2.5


 P2 (2.5)  (2.5)  4(2.5)  6  20.25
2
Kesalahan Interpolasi Beda
Terbagi Newton
 Rn = |f[xn+1,xn,xn-1,…,x0](x-x0)(x-x1)…(x-xn)|
 Menghitung R1
Perlu 3 titik (karena ada xn+1)
R1 = |f[x2,x1,x0](x-x0)(x-x1)|
 Menghitung R2
Perlu 4 titik sebagai harga awal
R2 = |f[x3,x2,x1,x0](x-x0)(x-x1)(x-x2)|
Kesalahan Interpolasi Beda
Terbagi Newton (Ex.)
 Berdasarkan contoh:
R1 = |f[x2,x1,x0](x-x0)(x-x1)|
= |0.077 (4-5)(4-2.5)|
= 0.1155
R2 = |f[x3,x2,x1,x0](x-x0)(x-x1)(x-x2)|
= |-0.007 (4-5)(4-2.5)(4-1)|
= 0.0315
Interpolasi Lagrange
 Interpolasi Lagrange pada dasarnya dilakukan
untuk menghindari perhitungan dari differensiasi
terbagi hingga (Interpolasi Newton)
 Rumus:
n
fn x    Li x .f x i 
i 0

dengan n x  xj
Li x   
j 0 xi  x j
j i
Interpolasi Lagrange
 Pendekatan orde ke-1
f1(x) = L0(x)f(x0) + L1(x)f(x1)
x  x1 x  x0
L0 x   L1 x  
x0  x1 x1  x0

x  x1 x  x0
 f1 x   f x0   f x1 
x0  x1 x1  x0
Interpolasi Lagrange
 Pendekatan orde ke-2
f2(x) = L0(x)f(x0) + L1(x)f(x1) + L2(x)f(x2)
 x  x1  x  x2   x  x0  x  x2 
L0 x      L1 x    
 
x  x  x  x  x1  x0  x1  x2 
i 0
n 2
 0 1  0 2  i 1
n 2

j i j i

 x  x0  x  x1 
L2 x    
 
i 2  x2  x0  x2  x1 
n 2
j i

 x  x1  x  x2   x  x0  x  x2   x  x0  x  x1 
 f2 x    

f x0   
 

 
f x1   


 f x2 
 x0  x1  x0  x2   x1  x0  x1  x2   x2  x0  x2  x1 
Interpolasi Lagrange
 Pendekatan orde ke-3
f3(x) = L0(x)f(x0) + L1(x)f(x1) + L2(x)f(x2) + L3(x)f(x3)
 x  x1  x  x2  x  x3   x  x0  x  x2  x  x3 
f2 x   
 








f x0    
 

f x1  

 x 0  x1  x 0  x2  x 0  x 3   x1  x 0  x1  x2  x1  x3 
 x  x0  x  x1  x  x3   x  x0  x  x1  x  x2 
 x  x  x  x  x  x f x2    x  x  x  x  x  x f x3 
       
 2 0  2 1  2 3   3 0  3 1  3 2 
Contoh : Berapa nilai distribusi t pada  = 4 %?

 Pendekatan orde ke-1


f1(x) = L0(x)f(x0) + L1(x)f(x1)
x  x1 x  x0
f1 x   f x0   f x1 
x0  x1 x1  x0
 45   4  2,5 
  2,571   2,015
 2,5  5   5  2,5 
 2,237
 Pendekatan orde ke-2
f2(x) = L0(x)f(x0) + L1(x)f(x1) + L2(x)f(x2)
 x  x1  x  x2   x  x0  x  x2   x  x0  x  x1 
 f2 x    

f x0   
 

 f x1   


 f x2 
 x0  x1  x0  x2   x1  x0  x1  x2   x2  x0  x2  x1 

 4  5  4  10   4  2,5  4  10   4  2,5  4  5 
   2,571    2,015    1,476
 2,5  5  2,5  10   5  2,5  5  10   10  2,5  10  5 
 2,214
Contoh
Carilah nilai dari ln 2 dengan metode interpolasi polinomial
Lagrange Orde dua berdasar data sebagai berikut ln 1 = 0,
ln 4 = 1,3862944 dan
ln 6 = 1,7917595. Untuk membandingkan hasil yang
diperoleh, hitung pula besar kesalahan (diketahui nilai
eksak dari ln 2 = 0,69314718).
Jawab : Dari soal di atas dapat diperoleh data sbg brkt.. x0
= 1  f (x0 ) = 0
x1 = 4  f (x1 ) = 1,3862944
x2 = 6  f (x2 ) = 1,7917595
 Dari data yg diketahui masukkan ke persamaan
interpolasi lagrange orde 2.

F2 (2)= [(2-4)/(1-4) . (2-6)/(1-6)] (0) + [(2-1)/(4-1). (2-


6)/(4-6)] (1,3862944) + [(2-1)/(6-1). (2-4)/(6-4)]
(1,7917595)
F2 (2)= 0,56584437
Besar kesalahan adalah
Et = {( 0,69314718-0,56584437) / 0,69314718 } x 100%
Et = 18,4 %.
xf (x)

Problem
Find a polynomial of degree two which
takes the following values.
x 1 2 4
f (x) 6 11 27
Interpolate f (3).
Solution
Since the x-values are not equi-spaced
then
we have to apply Langrangian formula.
S

n
Ln ( x)   Pni f i
i 0 , where
 x  x i j j
( x) 
 x  x 
Pni
i j i j

2
L2 ( x)   P2i f i  P20 ( x) f 0  P21 ( x) f1  P22 ( x) f 2 .
i 0
Now,
 x  x 
j 0 j  x  x1  x  x2  ( x  2) ( x  4)
 0  f0  6
 x  x 
P20 f 0 f
j 0 0 j x0  x1 x0  x2  (1  2) (1  4)

 2 x  12 x  16
2
S

 x  x 
j 1 j x  x0 x  x2  f  ( x  1) ( x  4) 11
 
 x  x 
P21 f1 f
j 1 1 j
1
x1  x0 x1  x2  1 (2  1) (2  4)
1

  11x 2  55 x  44
2

 j 2
x  x j   x  x0  x  x1  ( x  1) ( x  2)
P22 f 2  f2  f2   27
 j 2 x2  x j  x2  x0 x2  x1  (2  1) (4  2)


1
2

9 x 2  27 x  18 
Collecting the expressions above we’ve
L2 ( x)  2 x  12 x  16  11x  55 x  44  9 x  27 x  18
2 1 2 1 2
2 2
L2 ( x)  x  2 x  3
2
 f ( x)  x  2 x  3
2

and f (3)  3  2(3)  3  18.


2
PENCOCOKAN KURVA (curve fitting)
 Dalam Pencocokan Kurva (Curve Fitting), n buah
pasangan bilangan diberikan ((x1,y1), (x2,y2),
…(xn, yn)). Pasangan bilangan ini kemungkinan
adalah hasil pengukuran/pengamatan di
lapangan pada suatu besaran tertentu.
 Tujuan adalah mencari suatu fungsi tertentu
sehingga kita dapat menghubungkan setiap
pasangan bilangan yang diukur tersebut, f(xj) 
yj. Dengan kata lain jika fungsi tersebut
diplotkan, grafik yang dihasilkan akan mendekati
pasangan-pasangan bilangan tersebut.
 Salah satu cara yang dilakukan untuk mencari
fungsi dalam rangka pencocokan kurva untuk n
buah pasangan data adalah dengan
meminimalkan ketidakcocokan antara titik-titik
data dengan kurva tersebut.
 Tujuan untuk meminimalkan ketidakcocokan
tersebut dinamakan Regresi Kwadrat Terkecil.
 Regresi Kwadrat Terkecil
 Regresi Linear
 Regresi Polinomial
 REGRESI LINEAR
Dalam Regresi Linear, n buah pasangan data
dicocokkan dengan sebuah garis lurus (linear).
Persamaan garis lurus tersebut dapat dituliskan sbb:
y= a0 + a1 x + E, dimana
a0: intercept, titik potong terhadap sb-y
a1: kemiringan, gradien
E : kesalahan (ketidak cocokan) antara data
dengan model garis lurus yang dipilih.
Persamaan di atas dapat ditulis dalam bentuk : 
E = y - a0 - a1 x
 Dari sini dapat dilihat bahwa kesalahan E adalah
selisih harga y sebernya dengan harga approksimasi
a0 + a1 x.
 REGRESI LINEAR
E = y - a0 - a1 x
 Ada beberapa cara untuk untuk mendapatkan
kecocokan terbaik (Best Fit) seperti
meminimalkan
1. jumlah kesalahan residual, E
2. jumlah absolute kesalahan residual, |E|
3. Jumlah kwadrat kesalahan residual, E2
 Dari ketiga cara ini yang terbaik adalah
meminimalkan jumlah kwadrat kesalahan residual
(E2), salah satu keuntungannya adalah
menghasilkan garis yang unik untuk setiap
sebaran data. Selanjutnya teknik ini disebut
Pencocokan Kuadrat Terkecil (Least Squares Fit).
 Pencocokan Kuadrat Terkecil
 Koefisien a0 dan a1 dalam persamaan
linear sebeleumnya akan ditentukan
dengan meminimalkan jumlah kuadrat
residual berikut:
n n
S r   Ei2   ( yi  a0  a1 x) 2
i 1 i 1

 Meminimalkan berarti (dari teori Kalkulus):


S r n
 2 ( yi  a0  a1 xi )  0
a0 i 1
S r n
 2 [( yi  a0  a1 xi ) xi )  0
a1 i 1
 Pencocokan Kuadrat Terkecil
 Koefisien a0 dan a1 akan diperoleh dengan menyelesaikan
persamaan pada slide sebelumnya. Dengan demikian akan
diperoleh:
n n n
n xi yi   xi  yi
a1  i 1 i 1 i 1
2
 n
 n
n x    xi  2
i
i 1  i 1 
a0  y  a1 x ;
n n

y i x i
y i 1
; x i 1
n n
 KUANTIFIKASI KESALAHAN REGRESI LINEAR
Kesalahan standar (Standard Deviation)
antara model prediksi dengan sebaran data
dapat dikuantifikasi dengan rumus berikut:

Sy/x 
(y i  a0  a1 x) 2

n2
 KUANTIFIKASI KESALAHAN REGRESI LINEAR
Disamping jumlah kuadrat residual (sr) yang dibicarakan
sebelumnya terdapat suatu besaran jumlah kuadrat disekitar
rata2 st =  (y-yi)2. Selisih besaran ini dengan sr menunjukkan
perbaikan yang didapat dengan regresi linear. Di bawah ini
diperkenalkan besaran koefisien determinasi dan koefisien
korelasi yang menunjukkan kesempurnaan pencocokan kurva
dgn regresi linear.

Koef. Determinasi

r2 
 ( y  yi ) 2
  i 0 1
( y  a  a x ) 2

 ( y  yi ) 2

Koef. Korelasi r; 0 r  1; Semakin besar semakin


baik. r=1  pencocokan kurva sempurna
 Pencocokan Kuadrat Terkecil
 Contoh Soal:
Carilah persamaan garis regresi untuk mencocokkan
pasangan bilangan pada data berikut, dan carilah juga
kesalahan taksiran standar

i xi yi
1 1 0,5
2 2 2,5
3 3 2,0
4 4 4,0
5 5 3,5
6 6 6,0
7 7 5,5
 Pencocokan Kuadrat Terkecil
 Contoh:
Carilah persamaan garis regresi …

i xi yi xi yi xi2 yi-a0-a1xi
1 1 0,5 … … …
2 2 2,5 … … …
3 3 2,0 … … …
4 4 4,0 … … …
5 5 3,5 … … …
6 6 6,0 … … …
7 7 5,5 … … …
= … = … = … = … = …
 Pencocokan Kuadrat Terkecil
 Contoh: (lanjutan)
Carilah persamaan garis regresi …. Silahkan
melengkapi tabel sebelumnya dan akan diperoleh:

n7  xi yi  119,5  i  140


x 2

 xi  28 i
y  24 x  28  4
7 y  24  3,4285
7

 a1  
 a0  
Example 2
Fit a straight line to the data shown
and compute the standard deviation.

Solution

The intercept a and slope b of the interpolant can now be determined from

the regression line is f (x) = 2.927 + 0.6431x, which is shown in the figure
together with the data points.
Example 3
Determine the parameters a and b so that fits the following
data in the least-squares sense.
(1) fit ln yi and
(2) Compute the standard deviation in this case.

Solution :
(1) The problem is to fit the function ln( ae) bx
= lna +bx to the data

We are now dealing with linear regression, where the parameters


to be found are A = lna and b.
We get

A
Therefore, a = e = 3.790
and the fitting function
becomes
The plots of f (x) and the data
points are shown in the figure.

52
(2). Here is the computation of standard deviation:

As pointed out before, this is an approximate solution of the stated


problem, because we did not fit yi, but ln yi . Judging by the plot, the
fit seems to be quite good.
Exercise
We start the evaluation of the standard deviation by computing the
residuals:

The sum of the squares of the residuals is

so that the standard deviation becomes

54
Acknowledgement
These slides have been adapted
from:

Kong, Q., Siauw, T., & Bayen, A. M. (2021).


Python Programming and Numerical Methods: A
Guide for Engineers and Scientists. Academic
Press. ISBN: 978-0-12-819549-9

Kiusalaas, J. (2013). Numerical Methods in


Engineering with Python 3. United
Kingdom: Cambridge University Press.
ISBN:9781107033856
Additional materials :
Chapra, S.C (2015). Numerical Methods for Engineers. 6st
Edition. McGraw-Hill Companies, Inc . New York. ISBN. 978-
981-4670-87

Anda mungkin juga menyukai