mirror of
https://gitlab.rlp.net/mobitar/julia_course.git
synced 2024-11-16 13:28:10 +00:00
Done day 2
This commit is contained in:
parent
f7be7a84f4
commit
05394cbd4d
4 changed files with 237 additions and 7 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,3 +1,5 @@
|
|||
Manifest.toml
|
||||
|
||||
Solutions*.jl
|
||||
|
||||
Day_2/resources/new_test.txt
|
199
Day_2/Day_2.jl
199
Day_2/Day_2.jl
|
@ -4,9 +4,21 @@
|
|||
using Markdown
|
||||
using InteractiveUtils
|
||||
|
||||
# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
|
||||
macro bind(def, element)
|
||||
quote
|
||||
local iv = try Base.loaded_modules[Base.PkgId(Base.UUID("6e696c72-6542-2067-7265-42206c756150"), "AbstractPlutoDingetjes")].Bonds.initial_value catch; b -> missing; end
|
||||
local el = $(esc(element))
|
||||
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : iv(el)
|
||||
el
|
||||
end
|
||||
end
|
||||
|
||||
# ╔═╡ b3260050-4542-489d-b015-17092166095d
|
||||
# Import the plotting package we are going to use in this course
|
||||
using Plots
|
||||
begin
|
||||
# Import the plotting package we are going to use in this course
|
||||
using Plots
|
||||
end
|
||||
|
||||
# ╔═╡ 755b4cb8-e3ea-4142-bffd-e6db5f971d7c
|
||||
# Oh, no, you found my secret! 😱
|
||||
|
@ -280,18 +292,172 @@ More importantly, you can define your own types with *structs* and define how `j
|
|||
We will get into that later when structs are explained.
|
||||
"""
|
||||
|
||||
# ╔═╡ bc24a7a5-9638-4502-b24a-fecc406d9658
|
||||
md"""
|
||||
# I/O
|
||||
I/O stand for input/output. In this section, we will learn the basics about reading and writing files.
|
||||
|
||||
I/O knowledge will especially help us to read some files containing data to analyse.
|
||||
"""
|
||||
|
||||
# ╔═╡ 274342cb-19a9-4e71-a23e-7c7da18a8022
|
||||
md"""
|
||||
## Reading files
|
||||
"""
|
||||
|
||||
# ╔═╡ 1e75c110-005c-494f-9569-5eca01cb0545
|
||||
begin
|
||||
# Initialize an empty array of the type String
|
||||
# If you just use `[]` without `String`, Julia initializes an empty array with the type `Any` because it can not know what the array will contain.
|
||||
# We did already learn that array with the type `Any` are harmful for performance 🐢
|
||||
lines = String[]
|
||||
|
||||
# Open a file by specifying the path to the file and the mode in which it is opened with.
|
||||
# The mode "r" stands for read-only. This means that we can only read but not change the content of the file.
|
||||
# For more modes, check the documentation of `open` 📚️
|
||||
open("resources/test.txt", "r") do io
|
||||
lines = readlines(io)
|
||||
end
|
||||
# The file is automatically closed
|
||||
end
|
||||
|
||||
# ╔═╡ 4a10ce80-0053-44c8-aee8-6a1982749fa2
|
||||
# `lines` is now a vector of strings. Each string is one line in the file that was read.
|
||||
lines
|
||||
|
||||
# ╔═╡ 2e94396b-89e0-44d7-aec4-303ee782a8ee
|
||||
md"""
|
||||
## Writing files
|
||||
"""
|
||||
|
||||
# ╔═╡ 866e5958-a2bd-4625-aaa7-9e049fe8bbe2
|
||||
begin
|
||||
# `lines` is copied here to avoid problems with Pluto
|
||||
# If you are not using Pluto, you can modify `lines` directly
|
||||
new_lines = copy(lines)
|
||||
|
||||
# Modify one line
|
||||
new_lines[3] = "Meaningful line"
|
||||
|
||||
# Open a new file in write mode (w)
|
||||
open("resources/new_test.txt", "w") do io
|
||||
# Write every line into the file
|
||||
for line in new_lines
|
||||
# ⚠️ Make sure you give `println` the argument `io`!
|
||||
println(io, line)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# ╔═╡ d75af522-aed2-11ec-0953-bff6f6282a47
|
||||
md"""
|
||||
# Plotting
|
||||
Especially after learning about mutating and pure functions, we are now ready to dive into the world of plotting with Julia 🤿
|
||||
"""
|
||||
|
||||
# ╔═╡ 53b254bb-2394-4cdc-9b1b-8461196698dc
|
||||
# ╔═╡ d3f262fd-4403-4b8e-8e42-6185c299eaf3
|
||||
# Choose a backend
|
||||
plotly()
|
||||
|
||||
# ╔═╡ bc24a7a5-9638-4502-b24a-fecc406d9658
|
||||
# ╔═╡ 3742316b-19c5-476b-8a25-dbe5b0c6587c
|
||||
md"""
|
||||
## Line plots
|
||||
After the setup for plotting, lets see a simple example.
|
||||
"""
|
||||
|
||||
# ╔═╡ 1bb93b3b-1939-408c-9afa-39f7fc556b9b
|
||||
# Similar to `range`. Read the documentation of `LinRange` to see the difference 📚️
|
||||
x = LinRange(0, 2 * π, 250)
|
||||
|
||||
# ╔═╡ ee53e8fc-a342-43ee-948e-10c8494afbad
|
||||
# Broadcasting the function to all elements of x
|
||||
sin_y = sin.(x)
|
||||
|
||||
# ╔═╡ aa2e51f7-9dd3-4f77-9503-da536c6a8f7b
|
||||
plot(x, sin_y)
|
||||
|
||||
# ╔═╡ 5f9f92e2-1755-4d66-bc19-53355d5ce01f
|
||||
md"""
|
||||
Pretty easy, right? 😍
|
||||
|
||||
What if we want more than one plot in one figure? 🤔
|
||||
"""
|
||||
|
||||
# ╔═╡ 1d038daa-ee1f-4058-8be3-c42dd8236206
|
||||
cos_y = cos.(x)
|
||||
|
||||
# ╔═╡ a93e1083-b0dd-41bb-a377-d8436bd028ed
|
||||
begin
|
||||
plot(x, sin_y)
|
||||
|
||||
# Every plot after the first one has to use an exclamation mark because it is mutating the original plot
|
||||
plot!(x, cos_y)
|
||||
end
|
||||
|
||||
# ╔═╡ 64134500-58ed-45b5-884e-e909c99f6e66
|
||||
md"""
|
||||
Especially for scientific usage, this plot is not enough. It is missing a lot of important elements. Some of them are:
|
||||
- Axis labels
|
||||
- Proper legend
|
||||
- Title
|
||||
|
||||
So lets add some more details into the `sin` plot!
|
||||
"""
|
||||
|
||||
# ╔═╡ c5e15ad6-8d0b-4c01-9687-0dea750469a1
|
||||
plot(
|
||||
x, sin_y,
|
||||
xlabel="x",
|
||||
ylabel="sin(x)",
|
||||
label="sin",
|
||||
title="Plotting example",
|
||||
)
|
||||
|
||||
# ╔═╡ ca292895-ea2a-4fc0-975f-d9a8d40ac068
|
||||
# Setting more options
|
||||
plot(
|
||||
x, sin_y,
|
||||
xlabel="x",
|
||||
ylabel="sin(x)",
|
||||
label="sin",
|
||||
title="Simple example",
|
||||
color="red", # Change color
|
||||
linestyle=:dash, # Change linestyle
|
||||
)
|
||||
|
||||
# ╔═╡ 7cc3b76d-6388-4499-8647-9d1fccc6a8c4
|
||||
md"""
|
||||
For more information about all possible attributes and values, visit this link:
|
||||
|
||||
https://docs.juliaplots.org/latest/attributes/
|
||||
"""
|
||||
|
||||
# ╔═╡ c61b154c-0e2c-4c19-b5fc-17a68a25ccb0
|
||||
md"""
|
||||
## Interactive plots
|
||||
"""
|
||||
|
||||
# ╔═╡ 91383d53-1a7e-42f2-a10b-a71b20aa4186
|
||||
# This is how you implement a slider
|
||||
@bind a Slider(0.1:0.1:2)
|
||||
|
||||
# ╔═╡ 2f3f0391-6898-4f3d-aa8a-ba33070fc59f
|
||||
md"""
|
||||
`a =` $a
|
||||
"""
|
||||
|
||||
# ╔═╡ 42074ac6-bf37-4551-8769-3c4d2dfb0298
|
||||
plot(
|
||||
x, sin.(x ./ a),
|
||||
xlabel="x",
|
||||
ylabel="sin(x / a)",
|
||||
label="a = $a",
|
||||
)
|
||||
|
||||
# ╔═╡ 62a4765d-d426-442d-92c2-44efb389c7a9
|
||||
md"""
|
||||
Plotting to be continued 😉
|
||||
"""
|
||||
|
||||
# ╔═╡ 00000000-0000-0000-0000-000000000001
|
||||
PLUTO_PROJECT_TOML_CONTENTS = """
|
||||
|
@ -1252,10 +1418,31 @@ version = "0.9.1+5"
|
|||
# ╠═db3865d7-95ae-4c38-8e24-9271d8341294
|
||||
# ╠═ea67cee1-540b-453f-a484-a303c89316cb
|
||||
# ╟─49980a06-0e1c-4fc9-8e9d-630021f33cf3
|
||||
# ╟─bc24a7a5-9638-4502-b24a-fecc406d9658
|
||||
# ╟─274342cb-19a9-4e71-a23e-7c7da18a8022
|
||||
# ╠═1e75c110-005c-494f-9569-5eca01cb0545
|
||||
# ╠═4a10ce80-0053-44c8-aee8-6a1982749fa2
|
||||
# ╟─2e94396b-89e0-44d7-aec4-303ee782a8ee
|
||||
# ╠═866e5958-a2bd-4625-aaa7-9e049fe8bbe2
|
||||
# ╟─d75af522-aed2-11ec-0953-bff6f6282a47
|
||||
# ╠═b3260050-4542-489d-b015-17092166095d
|
||||
# ╠═53b254bb-2394-4cdc-9b1b-8461196698dc
|
||||
# ╠═bc24a7a5-9638-4502-b24a-fecc406d9658
|
||||
# ╠═d3f262fd-4403-4b8e-8e42-6185c299eaf3
|
||||
# ╟─3742316b-19c5-476b-8a25-dbe5b0c6587c
|
||||
# ╠═1bb93b3b-1939-408c-9afa-39f7fc556b9b
|
||||
# ╠═ee53e8fc-a342-43ee-948e-10c8494afbad
|
||||
# ╠═aa2e51f7-9dd3-4f77-9503-da536c6a8f7b
|
||||
# ╟─5f9f92e2-1755-4d66-bc19-53355d5ce01f
|
||||
# ╠═1d038daa-ee1f-4058-8be3-c42dd8236206
|
||||
# ╠═a93e1083-b0dd-41bb-a377-d8436bd028ed
|
||||
# ╟─64134500-58ed-45b5-884e-e909c99f6e66
|
||||
# ╠═c5e15ad6-8d0b-4c01-9687-0dea750469a1
|
||||
# ╠═ca292895-ea2a-4fc0-975f-d9a8d40ac068
|
||||
# ╟─7cc3b76d-6388-4499-8647-9d1fccc6a8c4
|
||||
# ╟─c61b154c-0e2c-4c19-b5fc-17a68a25ccb0
|
||||
# ╠═91383d53-1a7e-42f2-a10b-a71b20aa4186
|
||||
# ╟─2f3f0391-6898-4f3d-aa8a-ba33070fc59f
|
||||
# ╠═42074ac6-bf37-4551-8769-3c4d2dfb0298
|
||||
# ╟─62a4765d-d426-442d-92c2-44efb389c7a9
|
||||
# ╟─755b4cb8-e3ea-4142-bffd-e6db5f971d7c
|
||||
# ╟─00000000-0000-0000-0000-000000000001
|
||||
# ╟─00000000-0000-0000-0000-000000000002
|
||||
|
|
|
@ -42,6 +42,38 @@ The function `universal_pop` should be able to deal with (and only with) the fol
|
|||
# Your code starts here
|
||||
|
||||
|
||||
# ╔═╡ a7f0b201-a4fc-4f8b-8277-c7cbace12c60
|
||||
md"""
|
||||
# DNA analysis
|
||||
The file `resources/DNA.txt` has only one line.
|
||||
|
||||
Read the file and count the nucleotides 'A', 'T', 'G' and 'C' in this file. Calculate the occurancy percentage for each nucleotide.
|
||||
"""
|
||||
|
||||
# ╔═╡ 0bde137a-bc78-4204-971a-999c66eada47
|
||||
# Your code starts here
|
||||
|
||||
|
||||
# ╔═╡ f6981f2f-03ca-4f54-8ba1-bb1ad3bc201a
|
||||
md"""
|
||||
# Line plots
|
||||
Plot the following functions in one figure with $x \in [-4π, 4π]$
|
||||
|
||||
- $a \sinh(b x) + d$
|
||||
- $a \cosh(b x) + d$
|
||||
- $a \tanh(b x) + d$
|
||||
- $a e^{b x - c} + d$
|
||||
- $a e^{(b x - c)^2} + d$
|
||||
|
||||
Make sure you use unique linestyles, colors and labels.
|
||||
|
||||
First, implement 4 sliders for the variables `a`, `b`, `c` and `d` in the interval $[-1, 1]$.
|
||||
"""
|
||||
|
||||
# ╔═╡ 5959e8ce-6526-439e-8d5f-a45e3f2d9b26
|
||||
# Your code starts here
|
||||
|
||||
|
||||
# ╔═╡ 00000000-0000-0000-0000-000000000001
|
||||
PLUTO_PROJECT_TOML_CONTENTS = """
|
||||
[deps]
|
||||
|
@ -58,8 +90,12 @@ manifest_format = "2.0"
|
|||
"""
|
||||
|
||||
# ╔═╡ Cell order:
|
||||
# ╠═ce10609f-94ee-4b39-95b8-a2573c60b0e7
|
||||
# ╟─ce10609f-94ee-4b39-95b8-a2573c60b0e7
|
||||
# ╟─148125d2-aed5-11ec-2e5b-715891a04c69
|
||||
# ╠═1dcfef68-48c3-4e54-9ac7-d0eba40621c8
|
||||
# ╟─a7f0b201-a4fc-4f8b-8277-c7cbace12c60
|
||||
# ╠═0bde137a-bc78-4204-971a-999c66eada47
|
||||
# ╟─f6981f2f-03ca-4f54-8ba1-bb1ad3bc201a
|
||||
# ╠═5959e8ce-6526-439e-8d5f-a45e3f2d9b26
|
||||
# ╟─00000000-0000-0000-0000-000000000001
|
||||
# ╟─00000000-0000-0000-0000-000000000002
|
||||
|
|
5
Day_2/resources/test.txt
Normal file
5
Day_2/resources/test.txt
Normal file
|
@ -0,0 +1,5 @@
|
|||
This is a test file
|
||||
This is the second line
|
||||
Blabla
|
||||
42
|
||||
Julia is awesome!
|
Loading…
Reference in a new issue