Skip to content

Commit 9189d3c

Browse files
authored
Scipy notebook: fixed the bug in the Gaussian exercise (#187)
1 parent a711d9b commit 9189d3c

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

library_scipy.ipynb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,8 +1687,12 @@
16871687
"metadata": {},
16881688
"outputs": [],
16891689
"source": [
1690+
"import numpy as np\n",
1691+
"\n",
16901692
"xdata = [ -10.0, -9.0, -8.0, -7.0, -6.0, -5.0, -4.0, -3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]\n",
1691-
"ydata = [1.2, 4.2, 6.7, 8.3, 10.6, 11.7, 13.5, 14.5, 15.7, 16.1, 16.6, 16.0, 15.4, 14.4, 14.2, 12.7, 10.3, 8.6, 6.1, 3.9, 2.1]"
1693+
"ydata = [1.2, 4.2, 6.7, 8.3, 10.6, 11.7, 13.5, 14.5, 15.7, 16.1, 16.6, 16.0, 15.4, 14.4, 14.2, 12.7, 10.3, 8.6, 6.1, 3.9, 2.1]\n",
1694+
"xdata = np.array(xdata)\n",
1695+
"ydata = np.array(ydata)"
16921696
]
16931697
},
16941698
{
@@ -1726,8 +1730,18 @@
17261730
"def solution_gaussian(): # do not change the function signature\n",
17271731
" # 2. TODO: define function to fit: the Gaussian.\n",
17281732
"\n",
1729-
" # 3. TODO: call the curve_fit function here and return the parameters and covariance matrix AS A TUPLE\n",
1730-
" pass"
1733+
" def gaussian_func(x, a, b):\n",
1734+
" \"\"\"\n",
1735+
" Use this function signature, and these parameters A and B.\n",
1736+
" a: coefficient of the Gaussian\n",
1737+
" b: coefficient of the x^2 term\n",
1738+
"\n",
1739+
" Will probably return an error if you change this.\n",
1740+
"\n",
1741+
" Returns: the Gaussian function using the parameters a and b\n",
1742+
" \"\"\"\n",
1743+
" \n",
1744+
" # 3. TODO: call the curve_fit function here and return the parameters and covariance matrix AS A TUPLE"
17311745
]
17321746
},
17331747
{

tutorial/tests/test_library_scipy.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,12 @@ def reference_gaussian():
118118
3.9,
119119
2.1,
120120
]
121+
xdata = np.array(xdata)
122+
ydata = np.array(ydata)
121123

122124
# 2. TODO: define the gaussian function here:
123-
def ref_gaussian_math(x, sigma, mu):
124-
return 1 / (sigma * np.sqrt(2 * np.pi)) * np.exp(-0.5 * ((x - mu) / sigma) ** 2)
125+
def ref_gaussian_math(x, a, b):
126+
return a * np.exp(-b * x**2)
125127

126128
# 3. TODO: call the curve_fit function here:
127129
parameters, covariance = curve_fit(ref_gaussian_math, xdata, ydata)

0 commit comments

Comments
 (0)