mirror of
https://gitlab.rlp.net/mobitar/julia_course.git
synced 2024-11-16 13:28:10 +00:00
Done day 3
This commit is contained in:
parent
7ad01da0ea
commit
62c99e26eb
1 changed files with 98 additions and 1 deletions
|
@ -388,8 +388,95 @@ savefig("resources/test_figure_export.pdf")
|
||||||
# ╔═╡ f44cf458-4ec1-4e2b-b839-f3981683a151
|
# ╔═╡ f44cf458-4ec1-4e2b-b839-f3981683a151
|
||||||
md"""
|
md"""
|
||||||
# Data fitting
|
# Data fitting
|
||||||
|
Using data fitting, you can determine a model that does describe your measurements.
|
||||||
|
|
||||||
|
It is best explained using an example!
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ 25362ad6-5b49-4900-8026-c2d54197ba94
|
||||||
|
md"""
|
||||||
|
## Example
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ d6c9be8a-1025-449c-86c8-7d92ad240965
|
||||||
|
# Defining a model.
|
||||||
|
# Here, we did choose a linear model `f(x) = m * x + c`
|
||||||
|
@. linear_model(x, p) = p[1] * x + p[2]
|
||||||
|
|
||||||
|
# ╔═╡ 9befeddf-24fa-4af8-9b1d-df1a9865a9ff
|
||||||
|
# Initial guess of the parameters in `p`
|
||||||
|
# ⚠️ THE TYPE OF THIS VECTOR HAS TO BE FLOAT FOR NOW (because of a bug).
|
||||||
|
p0 = [1.0, 1.0]
|
||||||
|
|
||||||
|
# ╔═╡ e4c81d90-8d06-4c38-9886-252aa7285558
|
||||||
|
# You have to strip units and remove errors
|
||||||
|
fit = curve_fit(
|
||||||
|
linear_model, # First argument is the model
|
||||||
|
ustrip.(Measurements.value.(df_I_B_with_err.I)), # Measured x values
|
||||||
|
ustrip.(Measurements.value.(df_I_B_with_err.B)), # Measured y values
|
||||||
|
p0
|
||||||
|
)
|
||||||
|
|
||||||
|
# ╔═╡ ee858e78-6b69-485f-95d5-d735956e26d1
|
||||||
|
# Our fit parameters
|
||||||
|
param = fit.param
|
||||||
|
|
||||||
|
# ╔═╡ 51a17b24-b71b-4670-9bc5-de7576d6fc75
|
||||||
|
# The error of the fit parameters
|
||||||
|
sigma = stderror(fit)
|
||||||
|
|
||||||
|
# ╔═╡ 5957fce3-dcbd-4fc5-83b7-6730b6412dbd
|
||||||
|
begin
|
||||||
|
custom_scatter(
|
||||||
|
df_I_B_with_err, "I", "B";
|
||||||
|
label="B(I)",
|
||||||
|
markershape=:diamond,
|
||||||
|
markersize=3,
|
||||||
|
)
|
||||||
|
|
||||||
|
x = Measurements.value.(ustrip.(LinRange(
|
||||||
|
minimum(df_I_B_with_err.I),
|
||||||
|
maximum(df_I_B_with_err.I),
|
||||||
|
250
|
||||||
|
)))
|
||||||
|
|
||||||
|
plot!(
|
||||||
|
x, linear_model(x, param),
|
||||||
|
label="Linear fit",
|
||||||
|
linewidth=3, # Change line width
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
# ╔═╡ 385c3771-757c-49a7-8b8c-916e76524082
|
||||||
|
md"""
|
||||||
|
## Automation
|
||||||
|
Again, you can automate this process!
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ ef6274cf-f1fb-4f86-becf-eeb181ed92fc
|
||||||
|
function automated_fit(model, df, x_column_name, y_column_name, p0=[1.0, 1.0])
|
||||||
|
fit = curve_fit(
|
||||||
|
model,
|
||||||
|
ustrip.(Measurements.value.(df[!, x_column_name])),
|
||||||
|
ustrip.(Measurements.value.(df[!, y_column_name])),
|
||||||
|
p0
|
||||||
|
)
|
||||||
|
|
||||||
|
param = fit.param
|
||||||
|
sigma = stderror(fit)
|
||||||
|
|
||||||
|
return (param, sigma)
|
||||||
|
end
|
||||||
|
|
||||||
|
# ╔═╡ 409a9f5a-138a-491f-9789-520926be9dbd
|
||||||
|
# Test automation function
|
||||||
|
param2, sigma2 = automated_fit(
|
||||||
|
linear_model,
|
||||||
|
df_I_B_with_err,
|
||||||
|
"I",
|
||||||
|
"B",
|
||||||
|
)
|
||||||
|
|
||||||
# ╔═╡ 00000000-0000-0000-0000-000000000001
|
# ╔═╡ 00000000-0000-0000-0000-000000000001
|
||||||
PLUTO_PROJECT_TOML_CONTENTS = """
|
PLUTO_PROJECT_TOML_CONTENTS = """
|
||||||
[deps]
|
[deps]
|
||||||
|
@ -1747,8 +1834,18 @@ version = "0.9.1+5"
|
||||||
# ╠═6ab56f2d-759d-44ac-8427-55f85942615e
|
# ╠═6ab56f2d-759d-44ac-8427-55f85942615e
|
||||||
# ╟─1d524a9b-84e9-4ffd-b104-331055c78845
|
# ╟─1d524a9b-84e9-4ffd-b104-331055c78845
|
||||||
# ╠═1e14de84-7d87-4df4-8c88-2307cb2262ba
|
# ╠═1e14de84-7d87-4df4-8c88-2307cb2262ba
|
||||||
# ╠═f44cf458-4ec1-4e2b-b839-f3981683a151
|
# ╟─f44cf458-4ec1-4e2b-b839-f3981683a151
|
||||||
# ╠═c985a351-cbc7-4385-b5f7-b709eee47092
|
# ╠═c985a351-cbc7-4385-b5f7-b709eee47092
|
||||||
|
# ╟─25362ad6-5b49-4900-8026-c2d54197ba94
|
||||||
|
# ╠═d6c9be8a-1025-449c-86c8-7d92ad240965
|
||||||
|
# ╠═9befeddf-24fa-4af8-9b1d-df1a9865a9ff
|
||||||
|
# ╠═e4c81d90-8d06-4c38-9886-252aa7285558
|
||||||
|
# ╠═ee858e78-6b69-485f-95d5-d735956e26d1
|
||||||
|
# ╠═51a17b24-b71b-4670-9bc5-de7576d6fc75
|
||||||
|
# ╠═5957fce3-dcbd-4fc5-83b7-6730b6412dbd
|
||||||
|
# ╟─385c3771-757c-49a7-8b8c-916e76524082
|
||||||
|
# ╠═ef6274cf-f1fb-4f86-becf-eeb181ed92fc
|
||||||
|
# ╠═409a9f5a-138a-491f-9789-520926be9dbd
|
||||||
# ╟─32f4633c-af89-11ec-0059-5392abfd3bc3
|
# ╟─32f4633c-af89-11ec-0059-5392abfd3bc3
|
||||||
# ╟─00000000-0000-0000-0000-000000000001
|
# ╟─00000000-0000-0000-0000-000000000001
|
||||||
# ╟─00000000-0000-0000-0000-000000000002
|
# ╟─00000000-0000-0000-0000-000000000002
|
||||||
|
|
Loading…
Reference in a new issue