Skip to content

Commit b01bccd

Browse files
committed
Update NB 6.0 with f-strings; update .gitignore
1 parent 3673e26 commit b01bccd

File tree

2 files changed

+33
-30
lines changed

2 files changed

+33
-30
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ Norris.dat
2121
download
2222
import-file-test.ipynb
2323
**/Untitled.ipynb
24-
student_feedback
24+
student_feedback
25+
**/www.itl.nist.gov/

notebooks/6.0-Fitting-complex-functions.ipynb

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,10 @@
134134
"alpha_m_hat = np.sqrt(sw/Delta_prime)\n",
135135
"\n",
136136
"# Print results\n",
137-
"print(\"Intercept estimate with given uncertainty: ({0:.0f} ± {1:.0f}) mV\".format(c_hat, alpha_c_hat))\n",
138-
"print(\"Slope estimate with given uncertainty: ({0:.2f} ± {1:.2f}) mV/Hz\".format(m_hat, alpha_m_hat))\n",
137+
"print(\"Intercept estimate with given uncertainty: \"\n",
138+
" f\"({c_hat:.0f} ± {alpha_c_hat:.0f}) mV\")\n",
139+
"print(\"Slope estimate with given uncertainty: \"\n",
140+
" f\"({m_hat:.2f} ± {alpha_m_hat:.2f}) mV/Hz\")\n",
139141
"print()\n",
140142
"\n",
141143
"# Plot data and fit\n",
@@ -179,8 +181,8 @@
179181
"p, V = np.polyfit(frequency, voltage, 1, w=1/alpha_voltage, cov='unscaled')\n",
180182
"\n",
181183
"# Print results\n",
182-
"print(\"Intercept estimate: ({0:.0f} ± {1:.0f}) mV\".format(p[1], np.sqrt(V[1][1])))\n",
183-
"print(\"Slope estimate: ({0:.2f} ± {1:.2f}) mV/Hz\".format(p[0], np.sqrt(V[0][0])))"
184+
"print(f\"Intercept estimate: ({p[1]:.0f} ± {np.sqrt(V[1][1]):.0f}) mV\")\n",
185+
"print(f\"Slope estimate: ({p[0]:.2f} ± {np.sqrt(V[0][0]):.2f}) mV/Hz\")"
184186
]
185187
},
186188
{
@@ -250,8 +252,8 @@
250252
"p, V = np.polyfit(load, disp, 1, cov=True)\n",
251253
"\n",
252254
"# Show optimized fit parameters and uncertainties\n",
253-
"print(\"p[0] = ({0:.3f} ± {1:.3f}) x 1e-7\".format(1e7*p[0], 1e7*np.sqrt(V[0][0])))\n",
254-
"print(\"p[1] = ({0:.1f} ± {1:.1f}) x 1e-3\".format(1e3*p[1], 1e3*np.sqrt(V[1][1])))\n",
255+
"print(f\"p[0] = ({1e7*p[0]:.3f} ± {1e7*np.sqrt(V[0][0]):.3f}) x 1e-7\")\n",
256+
"print(f\"p[1] = ({1e3*p[1]:.1f} ± {1e3*np.sqrt(V[1][1]):.1f}) x 1e-3\")\n",
255257
"\n",
256258
"# Show fit with residuals\n",
257259
"from matplotlib.gridspec import GridSpec\n",
@@ -319,9 +321,9 @@
319321
"p2, V2 = np.polyfit(load, disp, 2, cov=True)\n",
320322
"\n",
321323
"# Show optimized fit parameters and uncertainties\n",
322-
"print(\"p2[0] = ({0:.2f} ± {1:.2f}) x 1e-15\".format(1e15*p2[0], 1e15*np.sqrt(V2[0][0])))\n",
323-
"print(\"p2[1] = ({0:.3f} ± {1:.3f}) x 1e-7\".format(1e7*p2[1], 1e7*np.sqrt(V2[1][1])))\n",
324-
"print(\"p2[2] = ({0:.1f} ± {1:.1f}) x 1e-3\".format(1e3*p2[2], 1e3*np.sqrt(V2[2][2])))\n",
324+
"print(f\"p2[0] = ({1e15*p2[0]:.2f} ± {1e15*np.sqrt(V2[0][0]):.2f}) x 1e-15\")\n",
325+
"print(f\"p2[1] = ({1e7*p2[1]:.3f} ± {1e7*np.sqrt(V2[1][1]):.3f}) x 1e-7\")\n",
326+
"print(f\"p2[2] = ({1e3*p2[2]:.1f} ± {1e3*np.sqrt(V2[2][2]):.1f}) x 1e-3\")\n",
325327
"\n",
326328
"# Show fit with residuals\n",
327329
"from matplotlib.gridspec import GridSpec\n",
@@ -353,7 +355,7 @@
353355
"\n",
354356
"# Make the residual plot with a dotted zero line\n",
355357
"# Need to adjust ylim, yticks, ylabel for readability\n",
356-
"ax_res = fig.add_subplot(gs[1])\n",
358+
"fig.add_subplot(gs[1])\n",
357359
"plt.plot(load, residuals, 'k.')\n",
358360
"plt.plot(load_range, [0,0], 'k:')\n",
359361
"plt.xlim(load_range[0], load_range[1])\n",
@@ -390,25 +392,25 @@
390392
"outputs": [],
391393
"source": [
392394
"# Define model\n",
393-
"def LCR_model(t, V_bgd, V0, T, phi, tau):\n",
395+
"def lcr_model(t, V_bgd, V0, T, phi, tau):\n",
394396
" return V_bgd + V0*np.cos(2*np.pi*t/T + phi)*np.exp(-t/tau)\n",
395397
"\n",
396398
"# Define time points\n",
397-
"t = np.linspace(40,950,2000);\n",
399+
"t = np.linspace(40,950,2000)\n",
398400
"\n",
399401
"# Define model parameters\n",
400-
"V_bgd = 0.3;\n",
401-
"V0 = 8;\n",
402-
"T = 39;\n",
403-
"phi = 4.5;\n",
404-
"tau = 200;\n",
402+
"V_bgd = 0.3\n",
403+
"V0 = 8\n",
404+
"T = 39\n",
405+
"phi = 4.5\n",
406+
"tau = 200\n",
405407
"\n",
406408
"# Define noise amplitude\n",
407-
"alpha_V = 0.075;\n",
409+
"alpha_V = 0.075\n",
408410
"\n",
409411
"# Simulate data\n",
410412
"# Compute ideal values\n",
411-
"V_ideal = LCR_model(t, V_bgd, V0, T, phi, tau)\n",
413+
"V_ideal = lcr_model(t, V_bgd, V0, T, phi, tau)\n",
412414
"\n",
413415
"# Generate noise with amplitude alpha_V\n",
414416
"rg = default_rng(0)\n",
@@ -469,7 +471,7 @@
469471
"\n",
470472
"# Plot simulation and preliminary model\n",
471473
"plt.plot(t, V_sim, 'k.')\n",
472-
"plt.plot(t, LCR_model(t, V_bgd_init, V0_init, T_init, phi_init, tau_init), 'r-')\n",
474+
"plt.plot(t, lcr_model(t, V_bgd_init, V0_init, T_init, phi_init, tau_init), 'r-')\n",
473475
"plt.xlabel('Time (µs)')\n",
474476
"plt.ylabel('Voltage (mV)')\n",
475477
"plt.show()"
@@ -481,7 +483,7 @@
481483
"source": [
482484
"### Fit the model to the data and examine the residuals\n",
483485
"\n",
484-
"These parameters do not fit the simulated data very well, but they are close enough for the solver to find a best-fit solution that is close to the simulation. We take `alpha_V` as the measurement uncertainty and set `absolute_sigma=True` to tell the solver that it should assume the uncertainty is known independently when calculating the parameter uncertainties. The value of `V0_opt` is negative because `phi_opt` is different from `phi_init` by π; otherwise, all of the best-fit parameter values are within one standard error of the associated simulation input parameter. We show the fit and the residuals as separate plots here because it is usually better to inspect them both at full scale before combining them in a multipanel figure."
486+
"These parameters do not fit the simulated data very well, but they are close enough for the solver to find a best-fit solution that is close to the simulation. We take `alpha_V` as the measurement uncertainty and set `absolute_sigma=True` to tell the solver that it should assume the uncertainty is known independently when calculating the parameter uncertainties. The value of `V0_opt` is negative because `phi_opt` is different from `phi_init` by π; otherwise, all the best-fit parameter values are within one standard error of the associated simulation input parameter. We show the fit and the residuals as separate plots here because it is usually better to inspect them both at full scale before combining them in a multipanel figure."
485487
]
486488
},
487489
{
@@ -493,7 +495,7 @@
493495
"# Fit the model to the data with curve_fit\n",
494496
"from scipy.optimize import curve_fit\n",
495497
"\n",
496-
"pOpt, pCov = curve_fit(LCR_model, t, V_sim, \n",
498+
"pOpt, pCov = curve_fit(lcr_model, t, V_sim, \n",
497499
" p0=[V_bgd_init, V0_init, T_init, phi_init, tau_init],\n",
498500
" sigma=alpha_V*np.ones(t.size),\n",
499501
" absolute_sigma=True)\n",
@@ -517,21 +519,21 @@
517519
"alpha_tau = alpha_vec[4]\n",
518520
"\n",
519521
"# Show optimized fit parameters and uncertainties\n",
520-
"print(\"V = {0:.4f} ± {1:.4f}\".format(V_bgd_opt, alpha_V_bgd))\n",
521-
"print(\"V0 = {0:.2f} ± {1:.2f}\".format(V0_opt, alpha_V0))\n",
522-
"print(\"T = {0:.3f} ± {1:.3f}\".format(T_opt, alpha_T))\n",
523-
"print(\"phi = {0:.3f} ± {1:.3f}\".format(phi_opt, alpha_phi))\n",
524-
"print(\"tau = {0:.1f} ± {1:.1f}\".format(tau_opt, alpha_tau))\n",
522+
"print(f\"V = {V_bgd_opt:.4f} ± {alpha_V_bgd:.4f}\")\n",
523+
"print(f\"V0 = {V0_opt:.2f} ± {alpha_V0:.2f}\")\n",
524+
"print(f\"T = {T_opt:.3f} ± {alpha_T:.3f}\")\n",
525+
"print(f\"phi = {phi_opt:.3f} ± {alpha_phi:.3f}\")\n",
526+
"print(f\"tau = {tau_opt:.1f} ± {alpha_tau:.1f}\")\n",
525527
"\n",
526528
"# Plot data\n",
527529
"plt.plot(t, V_sim, 'k.')\n",
528-
"plt.plot(t, LCR_model(t, V_bgd_opt, V0_opt, T_opt, phi_opt, tau_opt), 'r-')\n",
530+
"plt.plot(t, lcr_model(t, V_bgd_opt, V0_opt, T_opt, phi_opt, tau_opt), 'r-')\n",
529531
"plt.xlabel('Time (µs)')\n",
530532
"plt.ylabel('Voltage (mV)')\n",
531533
"plt.show()\n",
532534
"\n",
533535
"# Plot the residuals in a separate figure\n",
534-
"plt.plot(t, (V_sim - LCR_model(t, V_bgd_opt, V0_opt, T_opt, phi_opt, tau_opt))/alpha_V, 'k.')\n",
536+
"plt.plot(t, (V_sim - lcr_model(t, V_bgd_opt, V0_opt, T_opt, phi_opt, tau_opt))/alpha_V, 'k.')\n",
535537
"plt.xlabel('Time (µs)')\n",
536538
"plt.ylabel(f'Normalized\\nresidual (mV)')\n",
537539
"plt.show()"

0 commit comments

Comments
 (0)