1
0
mirror of https://gitlab.com/MisterBiggs/julia-for-matlabbers.git synced 2025-06-16 07:06:48 +00:00
2021-06-01 16:45:02 -07:00

313 lines
16 KiB
Julia

### A Pluto.jl notebook ###
# v0.14.7
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 el = $(esc(element))
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : missing
el
end
end
# ╔═╡ 7d7820ff-c0f5-41ee-be29-5006e7f4361d
begin
import Pkg
# Pkg.add("PlutoUI");
using PlutoUI
# Pkg.add("Plots")
using Plots
# Pkg.add("LaTeXStrings")
using LaTeXStrings
end
# ╔═╡ bb461e00-c0aa-11eb-2c7d-1bd1591779c6
md"""
# Julia for People That Were Unfortunate Enough to Learn MATLAB
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.
!!! 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.
"""
# ╔═╡ 307cbf7a-1ac5-47a5-8031-3458f9dd1887
PlutoUI.TableOfContents(aside=true)
# ╔═╡ 11ed58ac-1289-4b64-88a8-17cb7f0b6cd2
md"""
## 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
A major pain point for large Matlab projects is managing installed Toolboxes and Packages. In Matlab once something is installed its available globally and doesn't require being added to a file. There are quite a few downsides to this the largest in my opinion being that its very easy to crowd your namespace. To make it even worse Matlab usually doesn't even warn you about the issue so if two packages offer a function of the same name its just going to choose one and hope it works out. Another big issue this can cause is version conflicts, this is why theres so many issues trying to run code between Matlab versions.
Julia has a very intuitive package manager. For our uses I won't dig into version management or enviroments since they aren't really needed for smaller projects.
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.
!!! 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
md"""
Dragging the bar below automatically updates the plot thanks to Plutos reactivity.
"""
# ╔═╡ 16a7201a-4ce0-4102-9874-96733514ef08
md"""
!!! warning
This page is just HTML and doesn't have a Julia Backend to allow interactivity. The button in the top right corner has instructions on how to run this notebook yourself.
"""
# ╔═╡ a6ea55b5-3d1e-4f3c-8b5b-bb6138b6b316
@bind x Slider(1:5, show_value=true)
# ╔═╡ 863587ab-5ead-4ad0-839f-09e798bc6c93
begin
plot(y -> y^x, title=L"f(x) = x^%$x", label="")
end
# ╔═╡ 3cad3f5f-f6a5-405b-aeb0-495ebd8cca45
md"""
### Growing Arrays
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.
!!! note "Indexing Syntax"
Array index syntax uses square brackets `[]` in Julia as opposed to paranthesis in Matlab `()`.
"""
# ╔═╡ 0a4dc275-04b3-43e3-8a0d-cb921d769a0a
a = [99, 1, 3, 5]
# ╔═╡ 193c6a29-4f53-4c96-acc7-b5359043b471
sort(a)
# ╔═╡ e9a17e1a-652d-452e-8918-86c5adf699eb
a # a is still unsorted since sort(a) doesn't sort in place.
# ╔═╡ 6fa90db6-9d2b-47bc-9f41-925b447912e8
sort!(a)
# ╔═╡ 7ef8ad0b-4724-43d3-86e2-c854c4fab9c4
a # a is now sorted since sort!(a) sorts in place.
# ╔═╡ 58c883b6-3682-424d-a1e9-8ac561bb0dbf
a[end+1] = 5 # Can't grow array by indexing like in Matlab
# ╔═╡ a74e7cad-8dae-41cd-b77a-65ba082956a3
md"""
### Functions
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:
```julia
P = polyfit(X,Y,N)
[P,S] = polyfit(X,Y,N)
[P,S,MU] = polyfit(X,Y,N)
```
all return different things. In this case its simple to tell what is going on but if you had no idea what the polyfit function did in the first place its totally unintuitive what the extra returned values are. In other languages if a function has optional returns its usually idiomatic to return those values with an optional keyword argument or you just have a completely different function to get the different data. In the case of polyfit calculating S and MU are unrelated to calculating P so its not like the data was already calculated and can be easily returned which is what you would generally expect of optional arguments. To make things even worse `S` is designed to be an input to a different function, `POLYVAL`, so why wouldn't you just make polyval take `X`, `Y`, and `N` as inputs? Matlab is dumb `/endrant`.
Another weird thing is where functions can be placed in Matlab. In livescripts they can only be placed at the end of the file, and theres also the ability to make files that are only a function. In Julia functions can be anywhere and files can contain as many functions as you want and can be imported by any other file. The function syntax in Julia is also very sane.
"""
# ╔═╡ f4d655a2-8539-43f7-9356-56a3e3f9de91
g(x) = x + 1 # Simple functions can be made in one line
# ╔═╡ 289727e4-fb29-4ba8-97c5-cce62da725bc
function g(x, y)
z = x^2 + y^2
return z # Returns can either be the last line of a function or use the return keyword. Returning something ends a function so any lines after a return statement would not execute.
end # Functions have to have an end
# ╔═╡ ec37197c-f18e-48c2-8266-867e28cfc138
g() = g(6, 9.1) # Multiple functions can share names.
# This is also the wrong way to have default values for input arguments.
# g(x=6, y=9.1) ... would be the correct way to have default inputs that dont need to be specified.
# ╔═╡ 3c921f2d-3972-4b88-990d-36ced0764bc5
# Julia will always use the closest matching function.
function g(x::Int, y::Int) # x and y need to be integers
z = x+y # last line is returned, so really could just be x+y without the z
end
# ╔═╡ b7ae33bc-5edd-4741-a578-d210521b1698
g(14) # Since there are 4 different methods for g Julia will find and use the closest match. This is called multiple dispatch and can be very complicated but is incredibly powerful.
# ╔═╡ 75eeaff0-4fd3-4745-b042-af8ba12a0506
g(5.0, 6.0) # Normal g function used since inputs are floats
# ╔═╡ 8688fe79-00ad-497f-a466-dc86af9e03b0
g() # Uses normal g function since there is no match for g that has a Int and a Float specified as inputs.
# ╔═╡ 9440e0bc-7920-4b81-b88a-0b1046d24b4c
g(5,6) # Integer method used since inputs are both integers.
# ╔═╡ 86c23c4e-ab5b-4404-b248-c980a4098676
md"""
!!! 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
md"""
### Broadcasting
Matlab generally does broadcasting for you which can be dangerous since sometimes its unclear what a function or operator will do when used on arrays. Julia uses the same `.` syntax to do broadcasting but it has to be used anytime you want broadcasting and is very powerful in Julia. all operators support broadcasting `.+ .* .^ .==` and even functions support broadcasting by placing the `.` at the end of the function name `sqrt.(b)`. This is awesome because you can write your function as you normally would for one number, and then if it needs to run on a vector Julia takes care of making your function into a very fast and efficient vectorized function! You can still make your own functions that take in vectors since there may be situations where you can write faster code. This means that almost no function in the Julia standard library accepts vectors unlike Matlab. Obviously functions like `maximum` that find the max value in an array take them, but functions like `sin`, `cos`, or `sqrt` all have to use the `.` syntax to work on vectors.
"""
# ╔═╡ 011bb616-3752-432f-adfd-edaecc083cf0
b = 1:5 # Ranges are similar to Matlab, but will discussed later in the text
# ╔═╡ 6124f6b4-6315-4d74-9067-4553ce1d54bc
sin(b) # gotta broadcast!
# ╔═╡ 95e30983-f305-41aa-a1e2-f38fa0d0c1c3
sin.(b) # Nice!
# ╔═╡ 384fe37e-a184-4c3f-8b25-3acfee485cfc
b .^ 2 # broadcast operators
# ╔═╡ 3738b701-1ac6-414e-b1c2-57a1b4a2f53e
b .== b # Compares values element by element
# ╔═╡ ae1134f5-b5cc-49a8-aee0-99ec118299f9
b == b # Compares the entire objects against eachother
# ╔═╡ 3ef67004-2f85-4aa1-a1b5-5cbf62b292f7
md"""
### Strict Evaluation
Matlab evaluates arrays when they are made which is very common in programming languages, but Julia lazily computes values. This means that when you create a range like `1:.1:10` it doesn't create an array immediatly but waits until the array is needed. This has many memory advantages and many functions in Julia use the same behavior. These lazy objects generally behave as arrays but if they don't the `collect` function will turn them into an array.
"""
# ╔═╡ 1025e3a8-59c9-4725-bc1a-a0b94e6bd6af
1:10
# ╔═╡ e3c28497-29e9-447e-aba0-650aaaa52d88
typeof(1:10)
# ╔═╡ 4fd4f72d-45e0-4760-bc0b-5ebb78636e2c
enumerate(1:.25:2) # returns an enumerate object
# ╔═╡ cf125fe2-c754-4218-92c9-3f491f3109c1
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:
# ╟─bb461e00-c0aa-11eb-2c7d-1bd1591779c6
# ╟─307cbf7a-1ac5-47a5-8031-3458f9dd1887
# ╟─11ed58ac-1289-4b64-88a8-17cb7f0b6cd2
# ╠═7d7820ff-c0f5-41ee-be29-5006e7f4361d
# ╟─d15dbf3e-45a8-454f-83eb-e9ad5eac5d24
# ╟─16a7201a-4ce0-4102-9874-96733514ef08
# ╟─a6ea55b5-3d1e-4f3c-8b5b-bb6138b6b316
# ╟─863587ab-5ead-4ad0-839f-09e798bc6c93
# ╟─3cad3f5f-f6a5-405b-aeb0-495ebd8cca45
# ╠═0a4dc275-04b3-43e3-8a0d-cb921d769a0a
# ╠═193c6a29-4f53-4c96-acc7-b5359043b471
# ╠═e9a17e1a-652d-452e-8918-86c5adf699eb
# ╠═6fa90db6-9d2b-47bc-9f41-925b447912e8
# ╠═7ef8ad0b-4724-43d3-86e2-c854c4fab9c4
# ╠═58c883b6-3682-424d-a1e9-8ac561bb0dbf
# ╟─a74e7cad-8dae-41cd-b77a-65ba082956a3
# ╠═f4d655a2-8539-43f7-9356-56a3e3f9de91
# ╠═289727e4-fb29-4ba8-97c5-cce62da725bc
# ╠═ec37197c-f18e-48c2-8266-867e28cfc138
# ╠═3c921f2d-3972-4b88-990d-36ced0764bc5
# ╠═b7ae33bc-5edd-4741-a578-d210521b1698
# ╠═75eeaff0-4fd3-4745-b042-af8ba12a0506
# ╠═8688fe79-00ad-497f-a466-dc86af9e03b0
# ╠═9440e0bc-7920-4b81-b88a-0b1046d24b4c
# ╟─86c23c4e-ab5b-4404-b248-c980a4098676
# ╟─e239604f-2926-455b-a20c-0d9c283cc60a
# ╠═011bb616-3752-432f-adfd-edaecc083cf0
# ╠═6124f6b4-6315-4d74-9067-4553ce1d54bc
# ╠═95e30983-f305-41aa-a1e2-f38fa0d0c1c3
# ╠═384fe37e-a184-4c3f-8b25-3acfee485cfc
# ╠═3738b701-1ac6-414e-b1c2-57a1b4a2f53e
# ╠═ae1134f5-b5cc-49a8-aee0-99ec118299f9
# ╟─3ef67004-2f85-4aa1-a1b5-5cbf62b292f7
# ╠═1025e3a8-59c9-4725-bc1a-a0b94e6bd6af
# ╠═e3c28497-29e9-447e-aba0-650aaaa52d88
# ╠═4fd4f72d-45e0-4760-bc0b-5ebb78636e2c
# ╠═cf125fe2-c754-4218-92c9-3f491f3109c1
# ╟─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