forked from CalculusWithJulia/CalculusWithJulia.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetting-started-with-julia.jmd
155 lines (104 loc) · 5.64 KB
/
getting-started-with-julia.jmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Getting started with Julia
Julia is a freely available, open-source programming language aimed at technical computing.
As it is open source, indeed with a liberal MIT license, it can be
installed for free on many types of computers (though not phones or
tablets).
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/CalculusWithJulia/CwJScratchPad.git/master)
We recommend taking advantage of [Binder](https://mybinder.org), which
provides a web-based interface to `Julia` built around `Jupyter`, a
wildly succesful platform for interacting with different open-source
software programs. Clicking the launch button above will open a web
page which provides a blank notebook, save for a package used by these
notes.
[COCALC](https://cocalc.com/) offers free access to Julia through the
web. The free version can be slow due to limits on the resources used,
but the monthly price for paid version is modest and the performance
is quite reasonable.
### Installing Julia locally
Binder is great and convenient, but it may not be the
best option. Fortunately, installing `Julia` is a not-so-difficult
option.
Binaries of `Julia` are provided at
[julialang.org](http://julialang.org/downloads/). Julia has an
official released version and a developmental version. Unless there is
a compelling reason, the latest released version should be downloaded
and installed for use.
The base `Julia` provides a *command-line interface*, or REPL
(read-evaluate-parse).
### Basic interactive usage
Once installed, `Julia` can be started by clicking on an icon or
typing `julia` at the command line. Either will open a *command line
interface* for a user to interact with a `Julia` process. The basic
workflow is easy: commands are typed then sent to a `Julia` process
when the "return" key is pressed for a complete expression. Then the
output is displayed.
A command is typed following the *prompt*. An example might be `2 + 2`. To send the command to the `Julia` interpreter the "return" key is pressed. A complete expression or expressions will then be parsed and evaluated (executed). If the expression is not complete, `julia`'s prompt will still accept input to complete the expression. Type `2 +` to see. (The expression `2 +` is not complete, as the infix operator `+` expects two arguments, one on its left and one on its right.)
```julia; eval=false
_
_ _ _(_)_ | Documentation: https://docs.julialang.org
(_) | (_) (_) |
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 1.0.0 (2018-08-08)
_/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |
julia> 2 + 2
4
```
Above, `julia>` is the prompt. These notes will not include the
prompt, so that copying-and-pasting can be more easily used. Input and
output cells display similarly, though with differences in
coloring. For example:
```julia;
2 + 2
```
Other interfaces to `Julia` are described briefly in [Julia interfaces](./julia_interfaces.html). The notebook interface provided through `IJulia` most closely matches the style of the notes.
## Add-on packages
`Julia` has well over a 1000 external, add-on packages that enhance the
offerings of base `Julia`. We refer to one, `CalculusWithJulia`, that is designed to accompany these notes. This package installs several other packages that provide the needed functionality. The package (and its dependencies) can be installed through:
```julia; eval=false
using Pkg
Pkg.add("CalculusWithJulia")
```
(Or the one liner `] add CalculusWithJulia`. Some additional details on packages is provided [here](./calculus_with_julia.html).)
Installation only needs to be done once, but to use a package it must be loaded into each new session. This can be done with this command:
```julia;
using CalculusWithJulia
```
## The basics of working with IJulia
The **very** basics of the Jupyter notebook interface provided by `IJulia` are covered here.
An `IJulia` notebook is made up of cells. Within a cell a collection of commands may be typed (one or more).
When a cell is executed (by the triangle icon or under the Cell menu) the contents of the cell are evaluated by the `Julia` kernel and any output is displayed below the cell. Typically this is just the output of the last command.
```julia;
2 + 2
3 + 3
```
If the last commands are separated by commas, then a "tuple" will be formed and each output will be displayed, separated by commas.
```julia;
2 + 2, 3 + 3
```
Comments can be made in a cell. Anything after a `#` will be ignored.
```julia;
2 + 2 # this is a comment, you can ignore me...
```
Graphics are provided by external packages. There is no built-in
graphing. We use the `Plots` package and its default backend. The
`Plots` package provides a common interface to several different
backends, so this choice is easily changed. The `GR` and
`plotly` backends are used in these notes, when possible; the `PyPlot` backend is used for some surface plots.
```julia;
using CalculusWithJulia # this loads Plots among other packages
```
With that in hand, to make a graph of a function over a range, we follow this pattern:
```julia;
plot(sin, 0, 2pi)
```
A few things:
* Cells are numbered in the order they were evaluated.
* This order need not be from top to bottom of the notebook.
* The evaluation of a cell happens within the state of the workspace,
which depends on what was evaluated earlier.
* The workspace can be cleared by the "Restart" menu item under
"Kernel". After restarting the "Run All" menu item under "Cell" can
be used to re-run all the commands in the notebook--from top to bottom. "Run all" will also
reload any packages.