by Eugeniy E. Mikhailov and Greg Bentsen
Homework 3 due Monday Sept 21 at 11:59pm
Agenda
Today
We will begin using AI tools this Wednesday
We have set up a local model in the HPC cluster for the class to use.
Please set up the AI tool ASAP. We will expect you to have it set up prior to class on Wednesday.
If you have any trouble please come to our office hours


Suppose we measured the dependence of an experimental parameter on another parameter , yielding a set of datapoints .
We have a model that aims to predict the -values based on the -values, i.e.
For example, this could be a simple linear model: with .
Suppose we measured the dependence of an experimental parameter on another parameter , yielding a set of datapoints .
We have a model that aims to predict the -values based on the -values, i.e.
For example, this could be a simple linear model: with .
Goal: We want to extract the unknown model parameters (i.e. find the best parameters ) by fitting the model function to the data.
Define a way to estimate the goodness of fit.
Our goal is simple: find the optimal parameters that minimize (least-squares fit).

curve_fit function from the SciPy
optimize library.
Example fitting code using curve_fit
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# Define Lorentzian function
def lorentzian(x, A, x0, gamma):
return A / (1 + ((x - x0) / gamma) ** 2)
# Load experimental data
data = np.loadtxt('lorentzian_data.csv', delimiter=',', skiprows=1)
x_data = data[:, 0]
y_data = data[:, 1]
# Initial guess for the parameters [A, x0, gamma]
initial_guess = [10.0, 2.0, 1.0]
# Fit the Lorentzian function to the data
popt, pcov = curve_fit(lorentzian, x_data, y_data, p0=initial_guess)
# Extract fitted parameters
A_fit, x0_fit, gamma_fit = popt
# Plot the resulting fit
x_fit = np.linspace(-30, 30, 1000)
y_fit = lorentzian(x_fit, A_fit, x0_fit, gamma_fit)
plt.figure(figsize=(10, 6))
plt.scatter(x_data, y_data, label='Data', alpha=0.7)
plt.plot(x_fit, y_fit, 'r-', linewidth=2, label='Fit')
plt.savefig('lorentz_fit.png')
plt.show()We obtain two return values from curve_fit:
popt = the optimal parameter values
pcov = the covariance matrix for the parameters
The diagonal elements of pcov give you the variances
of the parameters
.
To find the uncertainty, just take the square root:
pcovSee https://en.wikipedia.org/wiki/Covariance_matrix for more information