Julia seems a lot like Matlab, at first

but look for hints of greater depth

  • richer numerical type system
  • Unicode variables and source text
  • better function-definition syntax
  • universal identity operator $I$
  • metaprogramming: transforming then evaluating chunks of code FOO BAR $\int_0^1 x \; dx$

Solving Ax=b with backslash operator

In [1]:
A = randn(4,4)
x = rand(4)
b = A*x
 = A\b
@show norm(A* - b)
norm(A * x̂ - b) = 2.3551386880256624e-16
Out[1]:
2.3551386880256624e-16

SVD

In [2]:
U,Σ,V = svd(A);
In [3]:
U
Out[3]:
4×4 Array{Float64,2}:
  0.344883    0.242207   0.838568     0.345248 
 -0.634428    0.10505    0.477184    -0.598967 
 -0.0641412   0.962321  -0.262824     0.0273296
 -0.688799   -0.065096   0.00482939   0.722008 
In [ ]:
U[:,1]
In [ ]:
# show how to type and show Σ
In [4]:
err = norm(U*diagm(Σ)*V'-A)
Out[4]:
9.468584479444362e-16
In [5]:
typeof(err)
Out[5]:
Float64

Eigenvalues of a symmetric matrix

In [6]:
A = randn(4,4) |> t -> t + t'  # pipe A through map A -> A + A' to symmetrize it
λ = eigmax(A);
@show det(A - λ*I)             # metaprogramming: print expression then evaluate it
;
det(A - λ * I) = 1.3111137536079468e-13
In [7]:
typeof(I)
Out[7]:
UniformScaling{Int64}

Integrating Lorenz equations

In [8]:
using ODE
using PyPlot
(.:15138): Gtk-WARNING **: Theme parsing error: gtk.css:68:35: The style property GtkButton:child-displacement-x is deprecated and shouldn't be used anymore. It will be removed in a future version

(.:15138): Gtk-WARNING **: Theme parsing error: gtk.css:69:35: The style property GtkButton:child-displacement-y is deprecated and shouldn't be used anymore. It will be removed in a future version

(.:15138): Gtk-WARNING **: Theme parsing error: gtk.css:73:46: The style property GtkScrolledWindow:scrollbars-within-bevel is deprecated and shouldn't be used anymore. It will be removed in a future version

** (.:15138): WARNING **: Couldn't connect to accessibility bus: Failed to connect to socket /tmp/dbus-WMSc4PD06G: Connection refused
In [9]:
# define Lorenz equations
function f(t, x)
    σ = 10
    β = 8/3
    ρ = 28
    [σ*(x[2]-x[1]); x[1]*(ρ-x[3]); x[1]*x[2] - β*x[3]]
end

# run f once
f(0, [0; 0; 0])

# integrate 
t = 0:0.01:20.0
x₀ = [0.1; 0.0; 0.0]
t,x = ode45(f, x₀, t)

x = hcat(x...)'         # rearrange storage of x (don't ask...)

# plot
plot3D(x[:,1], x[:,2], x[:,3], "b-")
xlabel("x")
ylabel("y")
zlabel("z")
xlim(-25,25)
ylim(-25,25)
zlim(0,60)
;

Fibonnaci function

In [ ]:
f(n) = n < 2 ? 1 : f(n-1) + f(n-2)  # recursive function definition
for i = 0:10
    println("f($i) = $(f(i))")      # string interpolation
end
In [ ]: