diff --git a/intro/index.html b/intro/index.html new file mode 100644 index 0000000..9b10883 --- /dev/null +++ b/intro/index.html @@ -0,0 +1,74 @@ + + + + + + ⚡ Pluto.jl ⚡ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/intro.jl b/intro/intro.jl similarity index 71% rename from intro.jl rename to intro/intro.jl index e436275..0be2dba 100644 --- a/intro.jl +++ b/intro/intro.jl @@ -21,6 +21,8 @@ begin using PlutoUI # Pkg.add("Plots") using Plots + Pkg.add("LaTeXStrings") + using LaTeXStrings end # ╔═╡ bb461e00-c0aa-11eb-2c7d-1bd1591779c6 @@ -29,12 +31,17 @@ md""" Julia is an incredily performant _(2 order of magnitude faster than Matlab)_, Dynamic _(Date types dont need to be declared unlike C)_, Composable _(The compiler is smart enough to allow functions you write to work for multiple different types of data)_, and most importantly for our use case Julia has very simple syntax that focuses on engineering / science applications. -> The code in this notebook is running in something called Pluto.jl. Pluto notebooks are similar to Matlab Livescript but have a stricter focus on cells and are immutable meaning that a variable can only be declared once and cannot be changed in other cells. This feels very restricting at first but guarantees that the code you are currently looking at is correct, and allows reactive updating of dependant code. It's important to note that these are Pluto.jl features and not Julia features. +!!! info "Pluto Notebooks" + The code in this notebook is running in something called Pluto.jl. Pluto notebooks are similar to Matlab Livescript but have a stricter focus on cells and are immutable meaning that a variable can only be declared once and cannot be changed in other cells. This feels very restricting at first but guarantees that the code you are currently looking at is correct, and allows reactive updating of dependant code. It's important to note that these are Pluto.jl features and not Julia features. """ # ╔═╡ 11ed58ac-1289-4b64-88a8-17cb7f0b6cd2 md""" -## Things Matlab Does Really Weirdly that no one Else Does. +## Intro - Things Matlab Does Really Weirdly that no one Else Does + +To get started I want to focus on things that Matlab does that are outlandish and make little sense and compare them to things Julia does. Julia is obviously not perfect, but being that it's one of the newest and fastest growing languages it's easy to argue that the syntax and language design of Julia are the most modern and sane currently available in programming especially if you are focussing on engineering or science workloads. + +In future chapters I will focus more on how Julia shines rather than why Matlab doesn't. ### Enviroments @@ -44,7 +51,8 @@ Julia has a very intuitive package manager. For our uses I won't dig into versio The best way to install packages is to open the Julia REPL (basically Matlabs command window) and press `]` which puts you in Package mode. You can then `add`, `update` or `remove` packages. You can then press backspace to return to the normal REPL. Once a package is added to Julia you have to bring it into your enviroment to use it by using the `using` keyword. Below is an example that adds `PlutoUI` to make Pluto more reactive, and `Plots` which is Julias very powerful plotting library. -> import Pkg and the Pkg.add() code is a way to install packages inside code and is there to make it easy to install the packages in case they weren't already installed on your system. +!!! warning "Package Management" + `import Pkg` and the `Pkg.add()` code is a way to install packages inside a script and is there to make it easy to install the packages in case they weren't already installed on your system. """ # ╔═╡ d15dbf3e-45a8-454f-83eb-e9ad5eac5d24 @@ -56,7 +64,9 @@ Dragging the bar below automatically updates the plot thanks to Plutos reactivit @bind x Slider(1:5, show_value=true) # ╔═╡ 863587ab-5ead-4ad0-839f-09e798bc6c93 -plot(y -> y^x) +begin + plot(y -> y^x, title=L"f(x) = x^%$x", label="") +end # ╔═╡ 3cad3f5f-f6a5-405b-aeb0-495ebd8cca45 md""" @@ -64,7 +74,8 @@ md""" In Matlab its very common to grow an array in a for loop using `array(end+1)` _(You really should never do this since it creates a new array every loop which gets very inefficent)_. In Julia you use `push!` for adding a single number to the end of a `Vector` and `append!` to add collections to the end of a `Vector`. You'll notice that `!` at the end of the push and append functions. In Julia the exclamation point at the end of a function name means that the function modifies the function inputs in place compared to returning a new copy of the object. -> Array index syntax uses square brackets `[]` in Julia. +!!! note "Indexing Syntax" + Array index syntax uses square brackets `[]` in Julia as opposed to paranthesis in Matlab `()`. """ # ╔═╡ 0a4dc275-04b3-43e3-8a0d-cb921d769a0a @@ -91,7 +102,7 @@ md""" Matlab functions have a ton of weird behaviors. The weirdest being that optional function returns are done base on the amount of return values so: -```matlab +```julia P = polyfit(X,Y,N) [P,S] = polyfit(X,Y,N) [P,S,MU] = polyfit(X,Y,N) @@ -137,7 +148,8 @@ g(5,6) # Integer method used since inputs are both integers. # ╔═╡ 86c23c4e-ab5b-4404-b248-c980a4098676 md""" -> Something to note in Julia when an array is passed into a function a copy is not made. This means if array `x` is passed into `f(x)`, any changes made to the array will apply to x outside of the array. If you intend to modify the passed in array be sure to add a bang `!` to the end of function name to keep your code idiomatic. +!!! note + In Julia when an array is passed into a function a copy the actual array gets passed in, not a copy. This means if array `x` is passed into `f(x)`, any changes made to the array will apply to `x` outside of the array. If you intend to modify the passed in array be sure to add a bang `!` to the end of function name to keep your code idiomatic. """ # ╔═╡ e239604f-2926-455b-a20c-0d9c283cc60a @@ -187,6 +199,61 @@ collect(enumerate(1:.25:2)) # Returns an an array of tuples as expected # ╔═╡ 4802d903-e87c-4ef0-a184-e4ae7af69a28 md""" ### Random Small Differences + +#### Plotting + +Plots use very similar syntax to Matlab with the exception that there is no `hold` or `figure` keyword the bang `!` syntax is used to modify plots and if a plot method is called without a bang then it creates a new plot object. Its often best practice to wrap everything for a plot in a `begin` and `end` so that the whole plot runs in order every time. + +!!! warning + Pluto only allows one "thing" per cell, but this is a Pluto quirk not Julia. So every cell needs a `begin` or `let` or `function` if there is more than one line but Pluto will also automatically do this for you. +""" + +# ╔═╡ a4665e30-4c64-46d1-8349-a49f40763a0b +plot(sin) + +# ╔═╡ 09303ffe-6cf8-4273-b697-9fbd7cb82cc1 +plot(cos) # Makes a new plot! + +# ╔═╡ e3b51038-2e57-4a5a-9b5d-7bf7148d700b +begin + plot(sin, label="sin") + plot!(cos, label="cos") + title!("sin and cos functions") +end + +# ╔═╡ 55e0d022-faca-40f8-a4a9-3a501eb5d19e +plot!(atan, label="atan") # Returns a new plot with all the elements of the old plot. + +# ╔═╡ dc89d08d-29bf-4e04-8470-c79c88ab4689 +md""" +!!! note + The Julia plotting library is massive and supports many frontends that are useful for various use cases such as printing in the terminal or making interactive plots for websites. This will all be covered in a future post. +""" + +# ╔═╡ b30e6f73-fe46-45e7-898e-dd1205dffe68 +md""" +#### Keyword Arguments + +Matlab uses an odd syntax where a string argument is passed before the value of the optional argument. Julia and most other languages allow passing in the optional keywords by the following syntax: +```julia +plot(x, y, title="y data vs x data", label="Data") +``` + +#### Semicolons + +Julia doesn't automatically print script values except in certain circumstances like in Pluto or in the REPL. This means that lines do not need to be terminated with a semicolon in regular scripts. Semicolons can be used to combine multiple lines into one. `plot(sin); title!("sin function")` + +#### `...` Syntax + +Matlab uses ... to continue lines of code. In Julia this is called the splat operator and is used to pass a collection into arguments of a function: + +```julia +xs = [1 2 3] + +# Following functions are identical! +f(xs...) +f(1, 2, 3) +``` """ # ╔═╡ Cell order: @@ -195,7 +262,7 @@ md""" # ╠═7d7820ff-c0f5-41ee-be29-5006e7f4361d # ╟─d15dbf3e-45a8-454f-83eb-e9ad5eac5d24 # ╟─a6ea55b5-3d1e-4f3c-8b5b-bb6138b6b316 -# ╟─863587ab-5ead-4ad0-839f-09e798bc6c93 +# ╠═863587ab-5ead-4ad0-839f-09e798bc6c93 # ╟─3cad3f5f-f6a5-405b-aeb0-495ebd8cca45 # ╠═0a4dc275-04b3-43e3-8a0d-cb921d769a0a # ╠═193c6a29-4f53-4c96-acc7-b5359043b471 @@ -225,4 +292,10 @@ md""" # ╠═e3c28497-29e9-447e-aba0-650aaaa52d88 # ╠═4fd4f72d-45e0-4760-bc0b-5ebb78636e2c # ╠═cf125fe2-c754-4218-92c9-3f491f3109c1 -# ╠═4802d903-e87c-4ef0-a184-e4ae7af69a28 +# ╟─4802d903-e87c-4ef0-a184-e4ae7af69a28 +# ╠═a4665e30-4c64-46d1-8349-a49f40763a0b +# ╠═09303ffe-6cf8-4273-b697-9fbd7cb82cc1 +# ╠═e3b51038-2e57-4a5a-9b5d-7bf7148d700b +# ╠═55e0d022-faca-40f8-a4a9-3a501eb5d19e +# ╟─dc89d08d-29bf-4e04-8470-c79c88ab4689 +# ╟─b30e6f73-fe46-45e7-898e-dd1205dffe68