Video Penjelasan: https://youtu.be/4aQIvjd1Ypc
Open Code in Google Colaboratory
Metode Numerik
</center>
Curve Fitting
(C) Taufik Sutanto - 2022
taudata Analytics
(C) Taufik Sutanto - 2022
https://taudata.blogspot.com/2020/04/mfdsnm-06.html ¶
Outline:¶
- Pendahuluan
- Linear Least Squares
- Exercise 1
- Power Fit
- Exercise 2
- Curve Fitting
- Exercise 3
- Johannes Keppler 1601 - Jarak Matahari ke Merkurius, Venus, Bumi, Mars
- Third law of Planetary Motions : T=CXA
- C = 0.1997, A = 3/2
Garis Least Squares¶
- $y = f(x) = Ax + b$ ==> Interpolasi (kita sudah bahas ini)
- Namun eksperimen (permasalahan di dunia nyata/sains) hampir selalu mengandung kesalahan (error)
- $y_k = f(x_k) + \epsilon_k$ untuk $k \in I$ (deviasi / Residual)
- Kita sudah pernah menghitung deviasi pendekatan dan nilai sesungguhnya (error absolut dan relatif), namun bagaimana mengukur error di beberapa pengukuran?
* Contoh?
Least Squares Line
- Bukti?
- Contoh?
In [1]:
import numpy as np, matplotlib as mpl, matplotlib.pyplot as plt
from numpy import polyfit, poly1d
In [10]:
%matplotlib inline
x = np.linspace(-5, 5, 100)
y = 4 * x + 1.5
noise_y = y + np.random.randn(y.shape[-1]) * 2.5
p = plt.plot(x, noise_y, 'rx')
p = plt.plot(x, y, 'b:')
In [4]:
coeff = polyfit(x, noise_y, 1)
coeff
Out[4]:
array([3.92177515, 1.74768939])
In [8]:
X = [-1, 0, 1, 2, 3, 4, 5, 6]
Y = [10, 9, 7, 5, 4, 3, 0, -1]
polyfit(X, Y, 1)
Out[8]:
array([-1.60714286, 8.64285714])
In [11]:
p = plt.plot(x, noise_y, 'rx')
p = plt.plot(x, coeff[0] * x + coeff[1], 'k-')
p = plt.plot(x, y, 'b--')
In [12]:
# Lebih sederhana
f = poly1d(coeff)
p = plt.plot(x, noise_y, 'rx')
p = plt.plot(x, f(x))
In [13]:
print(f)
3.922 x + 1.748
Latihan ==> Buku Lat 5.1 No 2.b ==> $E_1(f)$ ¶
The Power Fit y=AXM
- Bukti? Contoh?
In [2]:
tk = [0.2, 0.4, 0.6, 0.8, 1.0]
dk = [0.196, 0.785, 1.766, 3.14, 4.907]
dktk2 = [d*t**2 for d,t in zip(dk, tk)]
tk4 = [t**4 for t in tk]
A = sum(dktk2)/sum(tk4)
2*A
Out[2]:
9.813329928498469
Latihan ==> Buku Lat 5.1 No 4.C ==> Persamaannya ¶
- ...
Curve Fitting - Data Linearization Method for $y = C e^{Ax}$¶
NonLinear Curve Fitting¶
In [14]:
import numpy as np
from scipy.optimize import minimize
def f(x):
A, C = x[0], x[1] # agar mudah dimengerti
return (C-1.5)**2 + (C*np.exp(A) - 2.5)**2 + (C*np.exp(2*A) - 3.5)**2 + (C*np.exp(3*A) - 5.0)**2 + (C*np.exp(4*A) - 7.5)**2
In [15]:
x0 = [1,1]
res = minimize(f, x0)
In [16]:
res.x
Out[16]:
array([0.38357506, 1.61086915])
Polynomial Fitting
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 [Refernsi 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.
Referensi/Materi:
Tidak ada komentar:
Posting Komentar
Relevant & Respectful Comments Only.