Created
April 3, 2023 17:21
-
-
Save paulnakroshis/4511caadeba18380113b8e96a4ba37ea to your computer and use it in GitHub Desktop.
Example usage of LsqFit.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
fit_data(model, tdata, ydata, p0) | |
Fit the model function `model` to the data `ydata` at time points `tdata` | |
using initial parameters `p0`. | |
# Arguments | |
- `model::Function`: The model function to fit. | |
- `tdata::Vector`: A vector of time points. | |
- `ydata::Vector`: A vector of corresponding data points. | |
- `p0::Vector`: A vector of initial parameter values. | |
# Returns | |
A tuple `(fit_params, σ)` of the fitted parameter values and their standard deviations. | |
# Example | |
```julia | |
model(x, p...) = p[1] .* exp.(-p[2] .* x) .* sin.(p[3] .* x .+ p[4]) | |
tdata = range(0, 10, length=100) | |
ydata = model(tdata, 1.0, 0.5, 2π, π/2) + randn(size(tdata)) | |
p0 = [1.0, 1.0, 2π, π/2] | |
fit_params, σ = fit_data(model, tdata, ydata, p0) | |
""" | |
function fit_data(model, tdata, ydata, p0) | |
fit = curve_fit(model, tdata, ydata, p0) | |
fit_params = fit.param | |
σ = stderror(fit) | |
return fit_params, σ # returns a tuple of fit parameters and their standard deviations | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment