This text is adapted from Numerical Methods by Greenbaum and Chartier and is meant as a Julia-based analogue to their chapter on Matlab.

Julia is a high-level programming language that can be used for any technical or numerical computing problem, but is especially well-suited to linear algebra computations. It is also a powerful general-purpose programming language in its own right.

In this course, we think that juliabox.com is the best way to run Julia. This uses the jupyter notebook interface that enables us to insert commands and see their results.

To use this, enter juliabox.com in your browser.

(Note, there is an older juliabox.org website that serves the same purpose, but please use juliabox.com for CS314 at Purdue in Fall 2016.)

You should see the following webpage.

Once you do that, you'll get to the file browser.

This allows you to create new notebooks (the New button at the upper-right...) or work through a directory structure. All the files are stored on Google's cloud and saved so that they'll be there if you return. You can upload files through the "Files" button at top.

The Tutorial is placed by default in every new juliabox setup. (Only once though, if you delete it, I don't know how to get it back right now.) This is a good place to start. If you click on the tutorial, you'll get a list of notebooks.

Once you click on a notebook (say the 00 - Start Tutorial.ipnb file), then you'll start a notebook!

Actually, this file is a notebook itself, so we can type commands just the same way!

These examples now follow your textbook. To create a new "cell", go to "Insert Cell Below" and you'll get your own area to type commands

In [12]:
1+2*3

Out[12]:
7
In [13]:
ans/4

Out[13]:
1.75

Section 2.2 - Vectors¶

At this point, we are going to switch back and show how to do the commands in the book

In [91]:
v = [1; 2; 3; 4]

Out[91]:
4-element Array{Int64,1}:
1
2
3
4
In [92]:
v = [1.; 2; 3; 4]

Out[92]:
4-element Array{Float64,1}:
1.0
2.0
3.0
4.0
In [93]:
w = [5 6 7 8]

Out[93]:
1x4 Array{Int64,2}:
5  6  7  8
In [94]:
w = map(Float64,[5 6 7 8]) # convert to Float64

Out[94]:
1x4 Array{Float64,2}:
5.0  6.0  7.0  8.0
In [95]:
v[2]

Out[95]:
2.0
In [96]:
w[3]

Out[96]:
7.0
In [97]:
v+w

LoadError: DimensionMismatch("dimensions must match")

in promote_shape at operators.jl:211
in + at arraymath.jl:96
In [98]:
v+w'

Out[98]:
4x1 Array{Float64,2}:
6.0
8.0
10.0
12.0
In [99]:
vec(v+w')

Out[99]:
4-element Array{Float64,1}:
6.0
8.0
10.0
12.0
In [100]:
v[1]+v[2]+v[3]+v[4]

Out[100]:
10.0
In [101]:
sumv = 0.
for i=1:4
sumv = sumv+v[i];
end
sumv

Out[101]:
10.0

(Section 2.3) Getting help¶

In [4]:
?sum

search: sum sum! sumabs summary sumabs2 sumabs! sum_kbn sumabs2! cumsum cumsum!


Out[4]:
sum(A, dims)

Sum elements of an array over the given dimensions.

sum(itr)

Returns the sum of all elements in a collection.

sum(f, itr)

Sum the results of calling function f on each element of itr.

David's note. Julia's documentation is currently not as good as Matlab's :(

Section 2.4 Matrices¶

In [7]:
A = [1 2 3; 4 5 6; 7 8 0]

Out[7]:
3x3 Array{Int64,2}:
1  2  3
4  5  6
7  8  0
In [8]:
b = [0; 1; 2]

Out[8]:
3-element Array{Int64,1}:
0
1
2
In [9]:
x = A\b

Out[9]:
3-element Array{Float64,1}:
0.666667
-0.333333
-0.0     

Show a full precision answer. This is slightly easier in Matlab.

In [11]:
for i=1:length(x)
@printf("%.16e\n", x[i])
end

6.6666666666666663e-01
-3.3333333333333331e-01
-0.0000000000000000e+00

In [107]:
x

Out[107]:
3-element Array{Float64,1}:
0.666667
-0.333333
-0.0     
In [108]:
b - A*x

Out[108]:
3-element Array{Float64,1}:
0.0
0.0
4.44089e-16

In [109]:
#Section 2.6 Comments
#Solve Ax = b
x = A\b # this solves Ax=b and stores the result for x

Out[109]:
3-element Array{Float64,1}:
0.666667
-0.333333
-0.0     

Warning¶

We are still editing the section below this!

In [114]:
#2.8 Creating your own functions
f(x) = x^2 + 2x

Out[114]:
f (generic function with 1 method)
In [115]:
f(5)

Out[115]:
35
In [116]:
map(f, 0:1:5)

Out[116]:
6-element Array{Int64,1}:
0
3
8
15
24
35
In [117]:
x -> x^2 + 2x #Anonymous function

Out[117]:
(anonymous function)
In [118]:
map(x -> x^2 + 2x, 0:1:5)

Out[118]:
6-element Array{Int64,1}:
0
3
8
15
24
35
In [119]:
#2.9 Printing
x = 0:.5:2
print(x)

0.0:0.5:2.0
In [124]:
@printf("x = %s" ,collect(x))

x = [0.0,0.5,1.0,1.5,2.0]
In [125]:
y = rand(5,3)
println("      Score1            Score2             Score3 ")
println("\$y")

      Score1            Score2             Score3
[0.32211092471714275 0.5075976888863105 0.916080364548784
0.2945150815604498 0.8095561821604225 0.038854828692170607
0.22095166747559625 0.7457964790998437 0.9383063722746723
0.5171533339610654 0.4778154207659637 0.03573940173467305
0.15285539705109175 0.7385150097727968 0.8777448778450923]

In [126]:
println("      x \t\t sqrt(x) \n=====================================")
for x=1:5
@printf("%f \t\t %f\n",x, sqrt(x))   #formating string
end

      x 		 sqrt(x)
=====================================
1.000000 		 1.000000
2.000000 		 1.414214
3.000000 		 1.732051
4.000000 		 2.000000
5.000000 		 2.236068

In [ ]:
#2.10 More Loops and Conditionals
using Plots
print("Enter initial xmin ")
print("Enter initial xmax ")
print("Enter tolerance ")

while xmax - xmin > tolerance
x = collect(xmin:(xmax-xmin)/100:xmax)
y = map(x->x^2, x)
plot!(x,y)
print("Enter new value for xmin ")

Enter initial xmin STDIN> 10

WARNING: No working GUI backend found for matplotlib.

Enter new value for xmin