Skip to content

Commit

Permalink
content: talk structure
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnivin committed Oct 22, 2024
1 parent f96d5b2 commit 1e8c725
Show file tree
Hide file tree
Showing 3 changed files with 283 additions and 219 deletions.
38 changes: 18 additions & 20 deletions demo/demo/parameters_values_lmfit.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import numpy as np

from lmfit import Minimizer, create_params, report_fit, minimize
from lmfit import Minimizer, create_params, report_fit

import matplotlib.pyplot as plt
import matplotlib

matplotlib.use("Agg")
matplotlib.use("Agg")


# create data to be fitted
Expand All @@ -20,42 +20,40 @@
)


# define objective function: returns the array to be minimized
def fcn2min(params, x, data):
"""Model a decaying sine wave and subtract data."""
v = params.valuesdict()

model = v["amp"] * np.sin(x * v["omega"] + v["shift"]) * np.exp(-x * x * v["decay"])
def residual(parameters, x, data):
model = (
parameters["amp"]
* np.sin(x * parameters["omega"] + parameters["shift"])
* np.exp(-x * x * parameters["decay"])
)
return model - data


# create a set of Parameters
params = create_params(
parameters_initial = create_params(
amp=dict(value=10, min=0),
decay=0.1,
omega=3.0,
shift=dict(value=0.0, min=-np.pi / 2.0, max=np.pi / 2),
)

initial_model = (
params["amp"].value
* np.sin(x * params["omega"].value + params["shift"].value)
* np.exp(-x * x * params["decay"].value)
parameters_initial["amp"].value
* np.sin(x * parameters_initial["omega"].value + parameters_initial["shift"].value)
* np.exp(-x * x * parameters_initial["decay"].value)
)
# do fit, here with the default leastsq algorithm
minner = Minimizer(fcn2min, params, fcn_args=(x, data))
result = minner.minimize(method="least_squares")

# calculate final result
minimizer = Minimizer(residual, parameters_initial, fcn_args=(x, data))
result = minimizer.minimize(method="least_squares")

# calculate final
final = data + result.residual

# write error report
report_fit(result)

# try to plot results

plt.plot(x, data, "+", label="data", color="red")
plt.scatter(x, data, label="data", color="red")
plt.plot(x, initial_model, label="initial", linestyle="--", color="green")
plt.plot(x, final, label="final", color="blue")
plt.legend()
plt.savefig("parameters_values_lmfit.png")
plt.savefig("parameters_values_lmfit.png", bbox_inches="tight", pad_inches=0.03)
Loading

0 comments on commit 1e8c725

Please sign in to comment.