forked from omyllymaki/gauss-newton-solver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_fit_curve.py
54 lines (39 loc) · 1.31 KB
/
sample_fit_curve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import logging
import matplotlib.pyplot as plt
import numpy as np
from gn_solver import GNSolver
logging.basicConfig(level=logging.INFO)
NOISE = 3
COEFFICIENTS = [-0.001, 0.1, 0.1, 2, 15]
def func(x, coeff):
return coeff[0] * x ** 3 + coeff[1] * x ** 2 + coeff[2] * x + coeff[3] + coeff[4] * np.sin(x)
def residual_func(y_fit, y):
return y_fit - y
def main():
x = np.arange(1, 100)
y = func(x, COEFFICIENTS)
yn = y + NOISE * np.random.randn(len(x))
solver = GNSolver(fit_function=func,
residual_function=residual_func,
max_iter=100,
tolerance_difference=0)
init_guess = 1000000 * np.random.random(len(COEFFICIENTS))
_ = solver.fit(x, yn, init_guess)
fit = solver.get_estimate()
residual = solver.get_residual()
plt.subplot(1, 2, 1)
plt.plot(x, y, label="Original, noiseless signal", linewidth=2)
plt.plot(x, yn, label="Noisy signal", linewidth=2)
plt.plot(x, fit, label="Fit", linewidth=2)
plt.plot(x, residual, label="Residual", linewidth=2)
plt.title("Gauss-Newton: curve fitting example")
plt.xlabel("X")
plt.ylabel("Y")
plt.grid()
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(solver.rmse_list, "-o")
plt.yscale('log')
plt.show()
if __name__ == "__main__":
main()