Video Penjelasan: https://youtu.be/6QtTp9M5MVo
Open Code in Google Colaboratory
Metode Numerik
</center>
Turunan Numerik ¶
(C) Taufik Sutanto
taudata Analytics ~ https://taudata.blogspot.com/2022/04/mfdsnm-07.html
Turunan Numerik:¶
- Pendahuluan
- Pendekatan Turunan Numerik
- Analisis Error
- Ekstrapolasi Richardson
- Formula-formula turunan numerik
Sebelum dimulai¶
- Yang akan dibahas di Bab ini hanya pendekatan dari turunan "sebuah fungsi" di suatu titik
- Sistem "Ordinary Differential Equations" / Persamaan Differensial Biasa akan dibahas di Buku di Bab IX.
Ketika Limit Fungsi Real di Kalkulus Ternyata Bukan Dari Dunia Nyata¶
- Dari Kalkulus kita mendapatkan bahwa $$f'(x) = \lim_{h\rightarrow 0}\frac{f(x+h)-f(x)}{h}$$
- Secara logika kita memahami limit diatas sebagai "ketika h semakin dekat dengan 0, maka nilai limit fungsinya semakin mendekati kemiringan garis singgung fungsi f di x"
- Permasalahannya komputer tidak bisa menggunakan (menyimpan) bilangan real.
- Apakah pengertian limit diatas berlaku juga di sistem Floating Point?
- Misal $$f(x) = e^x$$
- Maka $f'(1) = e = 2.718281828459045 ...$
- Mari kita hitung $$\frac{f(x+h)-f(x)}{h}$$ untuk $h=10^{-1},10^{-2},...,10^{-10},...$
In [6]:
# Contoh Hal 312
import numpy as np
np.random.seed(99)
def f(x):
return np.exp(x)
X = 0.0 # f'(0)
eksak = 1.0
H = [10**-i for i in range(1,20)] # [0.1, 0.01, 0.001, 0.0001, 1e-05, 1e-06, 1e-07, 1e-08, 1e-09]
#H = [0.1]
limit = [(f(X+h)-f(X))/h for h in H]
E = [abs(eksak-l) for l in limit]
print ('h\t\t\tlimit\t\t\t error_Absolut')
for h, l, e in zip(H, limit, E):
print(h,'\t\t',l,'\t\t',e)
h limit error_Absolut 0.1 1.0517091807564771 0.051709180756477124 0.01 1.005016708416795 0.005016708416794913 0.001 1.0005001667083846 0.0005001667083845973 0.0001 1.000050001667141 5.0001667140975314e-05 1e-05 1.000005000006965 5.000006964905879e-06 1e-06 1.0000004999621837 4.999621836532242e-07 1e-07 1.0000000494336803 4.943368026033568e-08 1e-08 0.999999993922529 6.07747097092215e-09 1e-09 1.000000082740371 8.274037099909037e-08 1e-10 1.000000082740371 8.274037099909037e-08 1e-11 1.000000082740371 8.274037099909037e-08 1e-12 1.000088900582341 8.890058234101161e-05 1e-13 0.9992007221626409 0.0007992778373591136 1e-14 0.9992007221626409 0.0007992778373591136 1e-15 1.1102230246251565 0.11022302462515654 1e-16 0.0 1.0 1e-17 0.0 1.0 1e-18 0.0 1.0 1e-19 0.0 1.0
Komputer¶
- Pada contoh diatas kita melihat bahwa h mengecil belum tentu errornya juga mengecil
- Kita bisa mengira-ngira h terbaik pada kasus diatas, tapi bagaimana dengan fungsi yang lain?
Central Difference
In [8]:
# Better?
limit = [(f(X+h)-f(X-h))/(2*h) for h in H]
E = [abs(eksak-l) for l in limit]
print ('h\t\t\tlimit\t\t\t error_Absolut')
for h, l, e in zip(H,limit, E):
print(h,'\t\t',l,'\t\t',e)
h limit error_Absolut 0.1 1.001667500198441 0.0016675001984409743 0.01 1.0000166667499921 1.6666749992122476e-05 0.001 1.0000001666666813 1.6666668134490692e-07 0.0001 1.0000000016668897 1.6668897373506297e-09 1e-05 1.0000000000121023 1.2102319146833906e-11 1e-06 0.9999999999732445 2.6755486715046572e-11 1e-07 0.9999999994736442 5.26355847796367e-10 1e-08 0.9999999994736442 5.26355847796367e-10 1e-09 1.0000000272292198 2.7229219767832546e-08 1e-10 1.000000082740371 8.274037099909037e-08 1e-11 1.000000082740371 8.274037099909037e-08 1e-12 1.0000333894311098 3.3389431109753787e-05 1e-13 0.9997558336749535 0.00024416632504653535 1e-14 0.9992007221626409 0.0007992778373591136 1e-15 1.0547118733938987 0.05471187339389871 1e-16 0.5551115123125783 0.44488848768742173 1e-17 0.0 1.0 1e-18 0.0 1.0 1e-19 0.0 1.0
Taking the central Difference Further
In [10]:
# Really?
limit = [(-f(X+2*h)+8*f(X+h)-8*f(X-h)+f(X-2*h))/(12*h) for h in H]
E = [abs(eksak-l) for l in limit]
print ('h\t\t\tlimit\t\t\t error_Absolut')
for h, l, e in zip(H,limit, E):
print(h,'\t\t',l,'\t\t',e)
h limit error_Absolut 0.1 0.9999966626960979 3.3373039021178386e-06 0.01 0.9999999996666555 3.3334446314370325e-10 0.001 0.9999999999999546 4.54081217071689e-14 0.0001 1.0000000000002598 2.5979218776228663e-13 1e-05 0.999999999996374 3.6259883984257613e-12 1e-06 0.9999999999269852 7.301481641519558e-11 1e-07 0.9999999991960884 8.039116039526562e-10 1e-08 1.0000000022492015 2.2492014917219194e-09 1e-09 1.0000000179773612 1.7977361155274707e-08 1e-10 1.000000082740371 8.274037099909037e-08 1e-11 1.000000082740371 8.274037099909037e-08 1e-12 1.0000333894311098 3.3389431109753787e-05 1e-13 0.9998483522603387 0.00015164773966125367 1e-14 1.0029014655780581 0.002901465578058149 1e-15 1.0917193075480705 0.09171930754807045 1e-16 0.5551115123125783 0.44488848768742173 1e-17 0.0 1.0 1e-18 0.0 1.0 1e-19 0.0 1.0
Wow that's almost magical! It's time we understand where are these formulas came from.¶
- Derive Taylor expansion $f(x) = P_4(x)+E_4(x)$ untuk f(x+h) dan f(x-h)
- $f(x+h) - f(x-h) = 2f'(x)h + 2f^{(3)}(x)h^3/3! + 2f^{(5)}(c_1)h^5/5!$
- Lakukan hal yang sama, tapi h = 2h, sehingga
- $f(x+2h) - f(x-2h) = 4f'(x)h + 16f^{(3)}(x)h^3/3! + 64f^{(5)}(c_2)h^5/5!$
- Persamaan kedua dikurangi 8 kali persamaan pertama menghasilkan formula diatas.
- $-f(x+2h) + 8f(x+h) - 8f(x-h) + f(x-2h) = 12 f'(x)h + \frac{(16f^{(5)}(c_1) - 64 f^{(5)}(c_2))h^5}{120}$
- Jika dimisalkan
- $16 f^{(5)}(c_1) - 64 f^{(5)}(c_2) = -48f^{(5)}(c)$ untuk suatu $c\in [x-2h, x+2h]$, maka
- $f'(x) = \frac{-f(x+2h) + 8 f(x+h) -8 f(x-h) + f(x-2h)}{12h} + \frac{f^{(5)}(c)h^4}{30}$
- Hence our previous formula
Analisis Error, Teori dan Realitas
- Jika dibaca di buku, maka kita mendapatkan secara analitik batas error dari teorema 6.1 adalah:
- dan untuk teorema 6.2 adalah:
- But how useful are these corollary? Why? to what extend?
Richardson Extrapolation
- What Extrapolation? What do we mean by extrapolation in this contex?
- How do we use it?
In [4]:
# First you need to understand about List indexing in Python
A = [3, 4, 5, 6, 9, 8, 7]
print(A[-1], A[-2], A[-3])
7 8 9
In [11]:
# Contoh aplikasi
X = 0.0
h = 0.1 # h terbaik di contoh terakhir
D = []
# Lihat formulanya, kita butuh minimal 3 D untuk memulai iterasinya.
# Kita masih akan menggunakan f(x) yang sama dengan contoh sebelumnya
def R(x,h,k):
return (f(x+10**-k*h) - f(x-10**-k*h))/(2*h*10**-k)
print ('k\t\t\tD\t\t\t error_Absolut')
k = 0
d = R(X,h,k) # k = 0
D.append(d)
print(k,'\t\t', d,'\t\t',abs(1-d))
k = k+1
d = R(X,h,k) # k = 1
D.append(d)
print(k,'\t\t', d,'\t\t',abs(1-d))
k = k+1
d = R(X,h,k) # k = 2
D.append(d)
print(k,'\t\t', d,'\t\t',abs(1-d))
while abs(D[-1]-D[-2])<abs(D[-2]-D[-3]):
k = k+1
d = R(X,h,k)
D.append(d)
print(k,'\t\t', d,'\t\t',abs(1-d))
print('Final solution at k = ', k-2)
k D error_Absolut 0 1.001667500198441 0.0016675001984409743 1 1.000016666749992 1.666674999190043e-05 2 1.0000001666666813 1.6666668134490692e-07 3 1.0000000016668897 1.6668897373506297e-09 4 1.0000000000121023 1.2102319146833906e-11 5 0.9999999999732444 2.6755597737349035e-11 6 0.9999999994736442 5.26355847796367e-10 Final solution at k = 4
Numerical Differentiation Formulas
End of Module¶
Referensi:
- Johansson, R. (2018). Numerical Python: Scientific Computing and Data Science Applications with Numpy, SciPy and Matplotlib. Apress.
- John H. Mathews, Numerical Methods for Mathematics, Prentice Hall, 1992 [Referensi Utama]
- Heath, M. T. (2018). Scientific computing: an introductory survey (Vol. 80). SIAM.
- Conte, S. D., u0026amp; De Boor, C. (2017). Elementary numerical analysis: an algorithmic approach (Vol. 78). SIAM.
- https://static1.squarespace.com/static/5aff705c5ffd207cc87a512d/t/5b0378cc575d1f9eea15b56d/1526954189706/Numerical+Methods.pdf
- http://www.math.sjtu.edu.cn/faculty/xqzhang/2013fall.sc/Note4.pdf
Materi Bahasa Indonesia
Tidak ada komentar:
Posting Komentar
Relevant & Respectful Comments Only.