Anda di halaman 1dari 8

Interpolasi

Rio Agustian Gilang Fernando, 4211420004, Fisika

Fakultas Matematika dan Ilmu Pengetahuan Alam

Universitas Negeri Semarang

1. Interpolasi Newton

Bentuk polinomial newton adalah sebagai berikut.

pn (x) = a0 + a1 (x − x0 ) + a2 (x − x0 )(x − x1 ) + …

+an (x − x0 )(x − x1 ) … (x − xn )

Nilai dari koefisien an adalah


y1 − y0
a1 = = f [x1 , x0 ]
x1 − x0

y 2 −y 1 y 1 −y 0
− f [x2 , x1 ] − f [x1 , x0 ]
x2 −x1 x1 −x0
a2 = = = f [x2 , x1 , x0 ]
x2 − x0 x2 − x0

Dengan demikian, dapat dibuat sebuah matriks dengan nilai


y0 f [x1 , x0 ] f [x2 , x1 , x0 ] f [x3 , x2 , x1 , x0 ] f [x4 , x3 , x2 , x1 , x0 ]

y1 f [x2 , x1 ] f [x3 , x2 , x1 ] f [x4 , x3 , x2 , x1 ] 0

y2 f [x3 , x2 ] f [x4 , x3 , x2 ] 0 0

y3 f [x4 , x3 ] 0 0 0

y4 0 0 0 0

1 import numpy as np
2 import matplotlib.pyplot as plt

1 # data yang akan diinterpolasi


2 x = np.array([1, 3, 5, 7])
3 y = np.array([7, 13, 5, -7])
4
5 n = len(y)
6 koef = np.zeros([n, n])
7
8 # kolom pertama dibuat sebagai y
9 koef[:, 0] = y
10
11 # menghitung fn[x1,x2,...,xn]
12 for j in range(1, n):
13 for i in range(n-j):
14 koef[i][j] = (koef[i+1][j-1] - koef[i][j-1]) / (x[i+j]-x[i])
15
16 # koefisien interpolasi Newton
17 x1 = koef[0, :]
18 print(x1)
[ 7. 3. -1.75 0.20833333]

Misal, interpolasi yang akan dipakai adalah interpolasi orde-3. Bentuk polinomialnya adalah
sebagai berikut.

pn (x) = a0 + a1 (x − x0 ) + a2 (x − x0 )(x − x1 ) + a3 (x − x0 )(x − x1 )(x − x3 )

Dapat dibuat iterasi dengan bentuk

n = banyak data - 1
p = a3
for k in range(1, n+1):
p = koef[n-k] + (x - x_data[n-k]) * p

Keluaranya untuk setiap iterasi adalah

Iterasi ke-1

f 1 (x) = a2 + (x − x2 )a3

Iterasi ke-2

f 2 (x) = a1 + (x − x1 )f 1 (x)

f 2 (x) = a1 + (x − x1 )(a2 + (x − x2 )a3 )

f 2 (x) = a1 + a2 (x − x1 ) + a3 (x − x1 )(x − x2 )

Iterasi ke-3
f 3 (x) = a0 + (x − x0 )f 2 (x)

f 3 (x) = a0 + (x − x0 )(a1 + a2 (x − x1 ) + a3 (x − x1 )(x − x2 ))

f 3 (x) = a0 + a1 (x − x0 ) + a2 (x − x0 )(x − x1 ) + a3 (x − x0 )(x − x1 )(x − x3 )

1 # interpolasi di titik "t"


2 t = np.linspace(0, 10, 20)
3
4 n = len(x) - 1
5 p = x1[n]
6 for k in range(1, n+1):
7 p = x1[n-k] + (t - x[n-k]) * p
8
9 # titik-titik hasil interpolasi
10 y_int = p
11
12 # plotting
13 plt.plot(x, y, "k+", markersize=10)
14 plt.plot(t, y_int); plt.show()
p p ( , y_ ); p ()

1 # menyederhanakan kode
2 def new_interp(x, y, t, plot=True):
3 """
4 Menghitung nilai interpolasi di titik yang ditentukan
5 menggunakan interpolasi Newton.
6
7 Parameters
8 ----------
9 x : variabel independen / x-axis
10 y : variabel dipenden / y-axis
11 t : titik interpolasi yang diinginkan
12 """
13 # menentukan koefisien polinom
14 n = len(y)
15 koef = np.zeros([n, n])
16 koef[:, 0] = y
17 for j in range(1, n):
18 for i in range(n-j):
19 koef[i][j] = (koef[i+1][j-1] - koef[i][j-1]) / (x[i+j]-x[i])
20 c = koef[0, :]
21
22 # menghitung interpolasi di "t"
23 n = len(x) - 1
24 p = c[n]
25 for k in range(1, n+1):
26 p = c[n-k] + (t - x[n-k]) * p
27
28 # plotting
29 if plot == True:
30 plt.scatter(x, y, color="red", marker="+", s=200)
31 plt.plot(t, p)
32 plt.show()
33 else:
34 return p
35
36 help(new_interp)

Help on function new_interp in module __main__:


new_interp(x, y, t, plot=True)
Menghitung nilai interpolasi di titik yang ditentukan
menggunakan interpolasi Newton.

Parameters
----------
x : variabel independen / x-axis
y : variabel dipenden / y-axis
t : titik interpolasi yang diinginkan

1 # tes function yang telah dibuat


2 x = np.random.uniform(0, 10, 4)
3 y = np.random.uniform(0, 10, 4)
4 t = np.linspace(-1, 10, 30)
5
6 new_interp(x, y, t, plot=True)

Referensi
1. Interpolasi Newton Dalam Python. Diunggah oleh Yoyok Adisetio Laksono, 20 April 2021.
https://youtu.be/qZtw1c_ZvKg

2. Math 452, Numerical Methods: Polynomial Interpolation Python Programs.


http://math.oit.edu/~paulr/Upper/Math_45x/Math_452/interpolation.pdf

3. Metode Numerik - 8. Polinom Newton (Coding menggunakan Python). Diunggah oleh


Kodepy Coding, 4 November 2020. https://youtu.be/EfvzERO4fkM

4. Munir, Rinaldi. (2021). Metode Numerik (Revisi Kelima). Bandung: Institut Teknologi
Bandung.

5. Kong, Q., Siauw, T., dan Bayen, A. (2020). Newton’s Polynomial Interpolation.
https://pythonnumericalmethods.berkeley.edu/notebooks/chapter17.05-Newtons-
Polynomial-Interpolation.html
2. Interpolasi Lagrange
Bentuk polinomial Lagrange adalah sebagai berikut.

pn (x) = y 0 L0 (x) + y 1 L1 (x) + y 2 L2 (x) + ⋯ + y n Ln (x)

dengan
n
x − xj
Li (x) = ∏
xi − xj
j=1,j≠i

Misal, interpolasi yang akan dipakai adalah inyerpolasi orde-3. Bentuk polinomialnya adalah
sebagai berikut.

pn (x) = y 0 L0 (x) + y 1 L1 (x) + y 2 L2 (x) + y 3 Ln (x)

Dapat dibuat iterasi dalam bentuk

n = banyak data
y_pol = 0
for i in range(n):
p = 1
for j in range(n):
if i != j:
p = p * (t - x[j])/(x[i] - x[j])
y_pol = y_pol + p * y[i]

Keluaranya untuk setiap iterasi j dengan i = 0 adalah

Iterasi ke-1
(x − x1 )
p1 (x) =
(x0 − x1 )

Iterasi ke-2
(x − x2 )
p2 (x) = p1 (x)
(x0 − x2 )

(x − x1 ) (x − x2 )
p2 (x) =
(x0 − x1 ) (x0 − x1 )

Iterasi ke-3
(x − x3 )
p3 (x) = p2 (x)
(x0 − x3 )

(x − x1 ) (x − x2 ) (x − x3 )
p3 (x) =
(x0 − x1 ) (x0 − x2 ) (x0 − x3 )

Keluaranya untuk setiap iterasi j dengan i = 1 adalah


Iterasi ke-1

(x − x0 )
p1 (x) =
(x1 − x0 )

Iterasi ke-2
(x − x0 ) (x − x2 )
p2 (x) =
(x1 − x0 ) (x1 − x1 )

Iterasi ke-3
(x − x0 ) (x − x2 ) (x − x3 )
p3 (x) =
(x1 − x0 ) (x1 − x2 ) (x1 − x3 )

1 def lag_interp(x, y, t, plot=True):


2 """
3 Menghitung nilai interpolasi di titik yang ditentukan
4 menggunakan interpolasi Lagrange.
5
6 Parameters
7 ----------
8 x : variabel independen / x-axis
9 y : variabel dipenden / y-axis
10 t : titik interpolasi yang diinginkan
11 """
12 n = len(x)
13 y_pol = 0
14 for i in range(n):
15 p = 1
16 for j in range(n):
17 if i != j:
18 p = p * (t - x[j])/(x[i] - x[j])
19 y_pol = y_pol + p * y[i]
20
21 # plotting
22 if plot == True:
23 plt.scatter(x, y, color="red", marker="+", s=200)
24 plt.plot(t, y_pol)
25 plt.show()
26 else:
27 return y_pol
28
29 help(lag_interp)

Help on function lag_interp in module __main__:

lag_interp(x, y, t, plot=True)
Menghitung nilai interpolasi di titik yang ditentukan
menggunakan interpolasi Lagrange.

Parameters
----------
x : variabel independen / x-axis
y : variabel dipenden / y-axis
t : titik interpolasi yang diinginkan

1 # tes function yang telah dibuat


2 x = np.random.uniform(0, 10, 5)
3 y = np.random.uniform(0, 10, 5)
4 t = np.linspace(-1, 10, 100)
5
6 lag_interp(x, y, t)

Referensi
1. Code Sansar. Python Program for Lagrange Interpolation Method (with Output).
https://www.codesansar.com/numerical-methods/python-program-lagrange-interpolation-
method.htm

2. Lambers, Jim. (2010). Lagrange Interpolation.


https://www.math.usm.edu/lambers/mat772/fall10/lecture5.pdf

3. Munir, Rinaldi. (2021). Metode Numerik (Revisi Kelima). Bandung: Institut Teknologi
Bandung.

4. Kong, Q., Siauw, T., dan Bayen, A. (2020). Lagrange Polynomial Interpolation.
https://pythonnumericalmethods.berkeley.edu/notebooks/chapter17.04-Lagrange-
Polynomial-Interpolation.html

Anda mungkin juga menyukai