-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_symfit.py
executable file
·82 lines (59 loc) · 1.5 KB
/
test_symfit.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/env python3
'''
test_symfit.py
Examples of using the sym_fit() wrapper function from fit.py
'''
from sympy import symbols
from fit import sym_fit
import operations as ops
x, c0, c1, c2 = symbols("x c0 c1 c2")
expr = c1 * x**2 + c0
# 2 * x^2 + 1
x_data = [0, 1, 2, 3, 4, 5]
y_data = [1, 3, 9, 19, 33, 51] #exact
y_err = [1.1, 3.1, 8.9, 19.8, 33.1, 50] #with some error
print("Attempting to fit c1 * x^2 + c2")
popt, err = sym_fit(expr, [c0, c1], x_data, y_data)
print("Fit to y_data")
print(popt)
print(err)
popt, err = sym_fit(expr, [c0, c1], x_data, y_err)
print("Fit to y_err")
print(popt)
print(err)
print("")
x_data = [1, 2, 3, 4, 5]
y_data = [3, 9, 19, 33, 51]
expr = c0
print("Attempting to fit just c0")
popt, err = sym_fit(expr, [c0], x_data, y_data)
print("Fit to y_data")
print(popt)
print(err)
print("")
x_data = [1, 2, 3, 4, 5]
y_data = [3, 9, 19, 33, 51]
expr = x
print("'fit' just x (no free parameters)")
popt, err = sym_fit(expr, [], x_data, y_data)
print("Fit to y_data")
print(popt)
print(err)
print("---")
x_data = range(-10, 10)
y_data = range(-10, 10)
expr = ops.log(x + c0) + c1
print("Attempting to c1 * log(x + c0) + c2 (multiple guesses)")
popt, err = sym_fit(expr, [c0, c1], x_data, y_data, 12)
print("Fit to y_data")
print(popt)
print(err)
print("")
x_data = [-1, 1, 2, 3]
y_data = [1, 2, 3, 4]
expr = ops.log(x) + c0
print("Attempting to log(x) + c0 (should fail on this domain)")
popt, err = sym_fit(expr, [c0], x_data, y_data)
print("Fit to y_data")
print(popt)
print(err)