mirror of
https://gitlab.com/MisterBiggs/julia-for-matlabbers.git
synced 2025-06-16 07:06:48 +00:00
191 lines
10 KiB
Julia
191 lines
10 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
|
|
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.
|
|
|
|
> 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.
|
|
|
|
### 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.
|
|
|
|
> 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.
|
|
"""
|
|
|
|
# ╔═╡ d15dbf3e-45a8-454f-83eb-e9ad5eac5d24
|
|
md"""
|
|
Dragging the bar below automatically updates the plot thanks to Plutos reactivity.
|
|
"""
|
|
|
|
# ╔═╡ a6ea55b5-3d1e-4f3c-8b5b-bb6138b6b316
|
|
@bind x Slider(1:5, show_value=true)
|
|
|
|
# ╔═╡ 863587ab-5ead-4ad0-839f-09e798bc6c93
|
|
plot(y -> y^x)
|
|
|
|
# ╔═╡ 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.
|
|
"""
|
|
|
|
# ╔═╡ 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:
|
|
|
|
```matlab
|
|
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.
|
|
|
|
# ╔═╡ 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 its needs to run on a vector Julia takes are of making your function into a very fast 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
|
|
|
|
# ╔═╡ 6124f6b4-6315-4d74-9067-4553ce1d54bc
|
|
sin(b) # gotta broadcast!
|
|
|
|
# ╔═╡ 95e30983-f305-41aa-a1e2-f38fa0d0c1c3
|
|
sin.(b) # Nice!
|
|
|
|
# ╔═╡ 384fe37e-a184-4c3f-8b25-3acfee485cfc
|
|
b .^ 2
|
|
|
|
# ╔═╡ 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
|
|
|
|
# ╔═╡ Cell order:
|
|
# ╟─bb461e00-c0aa-11eb-2c7d-1bd1591779c6
|
|
# ╟─11ed58ac-1289-4b64-88a8-17cb7f0b6cd2
|
|
# ╠═7d7820ff-c0f5-41ee-be29-5006e7f4361d
|
|
# ╟─d15dbf3e-45a8-454f-83eb-e9ad5eac5d24
|
|
# ╟─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
|
|
# ╠═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
|