Add advanced/ notebook tour of numbl features
Seven notebooks covering data types and operators, matrices and indexing,
control flow and functions (with fib.m/minmax.m workspace files), linear
algebra, data structures and strings, numerical methods, and a plotting
gallery. Every code cell was verified through the REPL execution path, and
the set was smoke-tested in a browser (including .m workspace sync from the
subfolder). Linked from the intro notebook and README.
12 changed files+830−3
.prettierignoremodified+1−1View file
@@ -7,4 +7,4 @@ _output
77 .yarn
88 yarn.lock
99 jupyterlite_numbl_kernel/labextension
10-demo/content/*.ipynb
10+demo/content/**/*.ipynb
README.mdmodified+4−1View file
@@ -57,7 +57,10 @@ jupyter lite build --contents my-notebooks --output-dir dist
5757
5858 The `demo/` directory in this repo contains the demo site sources
5959 (notebooks + requirements); `.github/workflows/deploy.yml` builds and
60-deploys it to GitHub Pages.
60+deploys it to GitHub Pages. The `demo/content/advanced/` folder holds a
61+systematic seven-notebook tour of the language (data types, matrices,
62+control flow, linear algebra, data structures, numerical methods, and
63+plotting).
6164
6265 ### Always-fresh content (demo choice)
6366
demo/content/01-intro.ipynbmodified+1−1View file
@@ -81,7 +81,7 @@
8181 "cell_type": "markdown",
8282 "id": "intro-next",
8383 "metadata": {},
84- "source": "Next: [plotting](./02-plotting.ipynb) and\n[installing packages](./03-packages.ipynb), both also fully client-side."
84+ "source": "Next: [plotting](./02-plotting.ipynb) and\n[installing packages](./03-packages.ipynb), both also fully client-side.\n\nFor a systematic tour of the language, see the **`advanced/`** folder:\n[data types and operators](./advanced/01-data-types-and-operators.ipynb),\n[matrices and indexing](./advanced/02-matrices-and-indexing.ipynb),\n[control flow and functions](./advanced/03-control-flow-and-functions.ipynb),\n[linear algebra](./advanced/04-linear-algebra.ipynb),\n[data structures and strings](./advanced/05-data-structures-and-strings.ipynb),\n[numerical methods](./advanced/06-numerical-methods.ipynb), and the\n[plotting gallery](./advanced/07-plotting-gallery.ipynb)."
8585 }
8686 ],
8787 "metadata": {
demo/content/advanced/01-data-types-and-operators.ipynbadded+113−0View file
@@ -0,0 +1,113 @@
1+{
2+ "cells": [
3+ {
4+ "cell_type": "markdown",
5+ "id": "dt-0",
6+ "metadata": {},
7+ "source": "# Data types and operators\n\nA tour of numbl's scalar types and operators. numbl uses MATLAB syntax, so\neverything here matches what you would type in MATLAB. Run each cell with\n**Shift+Enter**; unsuppressed expressions print their value."
8+ },
9+ {
10+ "cell_type": "markdown",
11+ "id": "dt-1",
12+ "metadata": {},
13+ "source": "## Numbers and arithmetic"
14+ },
15+ {
16+ "cell_type": "code",
17+ "id": "dt-2",
18+ "metadata": {},
19+ "execution_count": null,
20+ "outputs": [],
21+ "source": "x = 3.5;\ny = 2;\n[x + y, x - y, x * y, x / y]\nx ^ y\nmod(7, 3)"
22+ },
23+ {
24+ "cell_type": "markdown",
25+ "id": "dt-3",
26+ "metadata": {},
27+ "source": "## Integer types saturate"
28+ },
29+ {
30+ "cell_type": "code",
31+ "id": "dt-4",
32+ "metadata": {},
33+ "execution_count": null,
34+ "outputs": [],
35+ "source": "a = int8(200) % clamps to 127\nb = uint8(-5) % clamps to 0\nclass(a)\ndouble(a) + 0.5"
36+ },
37+ {
38+ "cell_type": "markdown",
39+ "id": "dt-5",
40+ "metadata": {},
41+ "source": "## Logical values and comparisons"
42+ },
43+ {
44+ "cell_type": "code",
45+ "id": "dt-6",
46+ "metadata": {},
47+ "execution_count": null,
48+ "outputs": [],
49+ "source": "v = [1 2 3 4 5];\nv > 2\nv == 3\nany(v > 4)\nall(v > 0)"
50+ },
51+ {
52+ "cell_type": "code",
53+ "id": "dt-7",
54+ "metadata": {},
55+ "execution_count": null,
56+ "outputs": [],
57+ "source": "t = true; f = false;\n[t && f, t || f, xor(t, f), ~t]"
58+ },
59+ {
60+ "cell_type": "markdown",
61+ "id": "dt-8",
62+ "metadata": {},
63+ "source": "## Complex numbers"
64+ },
65+ {
66+ "cell_type": "code",
67+ "id": "dt-9",
68+ "metadata": {},
69+ "execution_count": null,
70+ "outputs": [],
71+ "source": "z = 3 + 4i;\nabs(z)\nangle(z)\n[real(z), imag(z)]\nconj(z)"
72+ },
73+ {
74+ "cell_type": "markdown",
75+ "id": "dt-10",
76+ "metadata": {},
77+ "source": "## Special values and rounding"
78+ },
79+ {
80+ "cell_type": "code",
81+ "id": "dt-11",
82+ "metadata": {},
83+ "execution_count": null,
84+ "outputs": [],
85+ "source": "[1/0, -1/0, 0/0]\n[isnan(0/0), isinf(1/0)]\n[pi, eps, Inf]"
86+ },
87+ {
88+ "cell_type": "code",
89+ "id": "dt-12",
90+ "metadata": {},
91+ "execution_count": null,
92+ "outputs": [],
93+ "source": "[round(2.5), floor(-2.3), ceil(2.1), fix(-2.7), sign(-3)]"
94+ }
95+ ],
96+ "metadata": {
97+ "kernelspec": {
98+ "display_name": "numbl (MATLAB syntax)",
99+ "language": "numbl",
100+ "name": "numbl"
101+ },
102+ "language_info": {
103+ "codemirror_mode": "octave",
104+ "file_extension": ".m",
105+ "mimetype": "text/x-octave",
106+ "name": "numbl",
107+ "nbconvert_exporter": "script",
108+ "pygments_lexer": "matlab"
109+ }
110+ },
111+ "nbformat": 4,
112+ "nbformat_minor": 5
113+}
demo/content/advanced/02-matrices-and-indexing.ipynbadded+133−0View file
@@ -0,0 +1,133 @@
1+{
2+ "cells": [
3+ {
4+ "cell_type": "markdown",
5+ "id": "mx-0",
6+ "metadata": {},
7+ "source": "# Matrices and indexing\n\nMatrices are the core numbl data type. This notebook covers construction,\nreshaping, and MATLAB-style indexing."
8+ },
9+ {
10+ "cell_type": "markdown",
11+ "id": "mx-1",
12+ "metadata": {},
13+ "source": "## Constructing matrices"
14+ },
15+ {
16+ "cell_type": "code",
17+ "id": "mx-2",
18+ "metadata": {},
19+ "execution_count": null,
20+ "outputs": [],
21+ "source": "Z = zeros(2, 3)\nO = ones(2)\nI = eye(3)\nM = magic(4)"
22+ },
23+ {
24+ "cell_type": "markdown",
25+ "id": "mx-3",
26+ "metadata": {},
27+ "source": "## Ranges with the colon operator"
28+ },
29+ {
30+ "cell_type": "code",
31+ "id": "mx-4",
32+ "metadata": {},
33+ "execution_count": null,
34+ "outputs": [],
35+ "source": "1:5\n0:0.25:1\n10:-2:0"
36+ },
37+ {
38+ "cell_type": "markdown",
39+ "id": "mx-5",
40+ "metadata": {},
41+ "source": "## Size and reshaping"
42+ },
43+ {
44+ "cell_type": "code",
45+ "id": "mx-6",
46+ "metadata": {},
47+ "execution_count": null,
48+ "outputs": [],
49+ "source": "A = reshape(1:12, 3, 4)\nsize(A)\nnumel(A)\nA'"
50+ },
51+ {
52+ "cell_type": "markdown",
53+ "id": "mx-7",
54+ "metadata": {},
55+ "source": "## Concatenation"
56+ },
57+ {
58+ "cell_type": "code",
59+ "id": "mx-8",
60+ "metadata": {},
61+ "execution_count": null,
62+ "outputs": [],
63+ "source": "[1 2; 3 4]\n[ones(2, 2), zeros(2, 2)]"
64+ },
65+ {
66+ "cell_type": "markdown",
67+ "id": "mx-9",
68+ "metadata": {},
69+ "source": "## Indexing: subscripts, ranges, and `end`"
70+ },
71+ {
72+ "cell_type": "code",
73+ "id": "mx-10",
74+ "metadata": {},
75+ "execution_count": null,
76+ "outputs": [],
77+ "source": "M = magic(4);\nM(2, 3)\nM(:, 1)'\nM(end, :)\nM(1:2, 3:4)"
78+ },
79+ {
80+ "cell_type": "markdown",
81+ "id": "mx-11",
82+ "metadata": {},
83+ "source": "## Linear and logical indexing"
84+ },
85+ {
86+ "cell_type": "code",
87+ "id": "mx-12",
88+ "metadata": {},
89+ "execution_count": null,
90+ "outputs": [],
91+ "source": "M = magic(4);\nM(5)\nM(M > 10)'"
92+ },
93+ {
94+ "cell_type": "markdown",
95+ "id": "mx-13",
96+ "metadata": {},
97+ "source": "## Assignment, growth, and deletion"
98+ },
99+ {
100+ "cell_type": "code",
101+ "id": "mx-14",
102+ "metadata": {},
103+ "execution_count": null,
104+ "outputs": [],
105+ "source": "x = 1:5;\nx(2) = 20;\nx(8) = 99;\nx"
106+ },
107+ {
108+ "cell_type": "code",
109+ "id": "mx-15",
110+ "metadata": {},
111+ "execution_count": null,
112+ "outputs": [],
113+ "source": "y = 1:6;\ny(2:3) = [];\ny"
114+ }
115+ ],
116+ "metadata": {
117+ "kernelspec": {
118+ "display_name": "numbl (MATLAB syntax)",
119+ "language": "numbl",
120+ "name": "numbl"
121+ },
122+ "language_info": {
123+ "codemirror_mode": "octave",
124+ "file_extension": ".m",
125+ "mimetype": "text/x-octave",
126+ "name": "numbl",
127+ "nbconvert_exporter": "script",
128+ "pygments_lexer": "matlab"
129+ }
130+ },
131+ "nbformat": 4,
132+ "nbformat_minor": 5
133+}
demo/content/advanced/03-control-flow-and-functions.ipynbadded+105−0View file
@@ -0,0 +1,105 @@
1+{
2+ "cells": [
3+ {
4+ "cell_type": "markdown",
5+ "id": "cf-0",
6+ "metadata": {},
7+ "source": "# Control flow and functions\n\nBranches, loops, anonymous functions, and named functions kept in `.m`\nfiles alongside the notebook."
8+ },
9+ {
10+ "cell_type": "markdown",
11+ "id": "cf-1",
12+ "metadata": {},
13+ "source": "## if / elseif / else"
14+ },
15+ {
16+ "cell_type": "code",
17+ "id": "cf-2",
18+ "metadata": {},
19+ "execution_count": null,
20+ "outputs": [],
21+ "source": "x = 7;\nif mod(x, 2) == 0\n disp('even')\nelseif x > 5\n disp('odd and > 5')\nelse\n disp('odd and <= 5')\nend"
22+ },
23+ {
24+ "cell_type": "markdown",
25+ "id": "cf-3",
26+ "metadata": {},
27+ "source": "## for and while loops"
28+ },
29+ {
30+ "cell_type": "code",
31+ "id": "cf-4",
32+ "metadata": {},
33+ "execution_count": null,
34+ "outputs": [],
35+ "source": "total = 0;\nfor k = 1:10\n total = total + k;\nend\ntotal"
36+ },
37+ {
38+ "cell_type": "code",
39+ "id": "cf-5",
40+ "metadata": {},
41+ "execution_count": null,
42+ "outputs": [],
43+ "source": "n = 0; p = 1;\nwhile p < 1000\n n = n + 1;\n p = p * 2;\nend\nfprintf('2^%d = %d is the first power of two >= 1000\\n', n, p);"
44+ },
45+ {
46+ "cell_type": "markdown",
47+ "id": "cf-6",
48+ "metadata": {},
49+ "source": "## switch"
50+ },
51+ {
52+ "cell_type": "code",
53+ "id": "cf-7",
54+ "metadata": {},
55+ "execution_count": null,
56+ "outputs": [],
57+ "source": "grade = 'B';\nswitch grade\n case 'A'\n disp('excellent')\n case {'B', 'C'}\n disp('good')\n otherwise\n disp('try again')\nend"
58+ },
59+ {
60+ "cell_type": "markdown",
61+ "id": "cf-8",
62+ "metadata": {},
63+ "source": "## Anonymous functions, arrayfun, and cellfun"
64+ },
65+ {
66+ "cell_type": "code",
67+ "id": "cf-9",
68+ "metadata": {},
69+ "execution_count": null,
70+ "outputs": [],
71+ "source": "sq = @(x) x.^2;\nsq(1:5)\narrayfun(@(k) k^2, 1:5)\ncellfun(@numel, {'a', 'bb', 'ccc'})"
72+ },
73+ {
74+ "cell_type": "markdown",
75+ "id": "cf-10",
76+ "metadata": {},
77+ "source": "## Named functions in `.m` files\n\nThis notebook ships with [fib.m](./fib.m) and [minmax.m](./minmax.m). The\nkernel syncs `.m` files next to the notebook into the session, so you can\ncall them from cells (and edit them in the file browser)."
78+ },
79+ {
80+ "cell_type": "code",
81+ "id": "cf-11",
82+ "metadata": {},
83+ "execution_count": null,
84+ "outputs": [],
85+ "source": "fibs = arrayfun(@fib, 1:10)\n[lo, hi] = minmax([3 -1 7 2])"
86+ }
87+ ],
88+ "metadata": {
89+ "kernelspec": {
90+ "display_name": "numbl (MATLAB syntax)",
91+ "language": "numbl",
92+ "name": "numbl"
93+ },
94+ "language_info": {
95+ "codemirror_mode": "octave",
96+ "file_extension": ".m",
97+ "mimetype": "text/x-octave",
98+ "name": "numbl",
99+ "nbconvert_exporter": "script",
100+ "pygments_lexer": "matlab"
101+ }
102+ },
103+ "nbformat": 4,
104+ "nbformat_minor": 5
105+}
demo/content/advanced/04-linear-algebra.ipynbadded+97−0View file
@@ -0,0 +1,97 @@
1+{
2+ "cells": [
3+ {
4+ "cell_type": "markdown",
5+ "id": "la-0",
6+ "metadata": {},
7+ "source": "# Linear algebra\n\nnumbl uses LAPACK (native on the CLI, WebAssembly in the browser) for its\nmatrix factorizations."
8+ },
9+ {
10+ "cell_type": "markdown",
11+ "id": "la-1",
12+ "metadata": {},
13+ "source": "## Solving a linear system"
14+ },
15+ {
16+ "cell_type": "code",
17+ "id": "la-2",
18+ "metadata": {},
19+ "execution_count": null,
20+ "outputs": [],
21+ "source": "A = [4 -2 1; -2 4 -2; 1 -2 3];\nb = [1; 2; 3];\nx = A \\ b\nresidual = norm(A * x - b)"
22+ },
23+ {
24+ "cell_type": "markdown",
25+ "id": "la-3",
26+ "metadata": {},
27+ "source": "## Determinant, rank, trace, inverse"
28+ },
29+ {
30+ "cell_type": "code",
31+ "id": "la-4",
32+ "metadata": {},
33+ "execution_count": null,
34+ "outputs": [],
35+ "source": "A = [4 -2 1; -2 4 -2; 1 -2 3];\ndet(A)\nrank(A)\ntrace(A)\ninv(A)"
36+ },
37+ {
38+ "cell_type": "markdown",
39+ "id": "la-5",
40+ "metadata": {},
41+ "source": "## Eigenvalues and eigenvectors"
42+ },
43+ {
44+ "cell_type": "code",
45+ "id": "la-6",
46+ "metadata": {},
47+ "execution_count": null,
48+ "outputs": [],
49+ "source": "A = [4 -2 1; -2 4 -2; 1 -2 3];\n[V, D] = eig(A);\neigenvalues = diag(D)'"
50+ },
51+ {
52+ "cell_type": "markdown",
53+ "id": "la-7",
54+ "metadata": {},
55+ "source": "## Singular values and QR"
56+ },
57+ {
58+ "cell_type": "code",
59+ "id": "la-8",
60+ "metadata": {},
61+ "execution_count": null,
62+ "outputs": [],
63+ "source": "A = [4 -2 1; -2 4 -2; 1 -2 3];\ns = svd(A)\n[Q, R] = qr(A);\northogonality_error = norm(Q' * Q - eye(3))"
64+ },
65+ {
66+ "cell_type": "markdown",
67+ "id": "la-9",
68+ "metadata": {},
69+ "source": "## Vector and matrix norms"
70+ },
71+ {
72+ "cell_type": "code",
73+ "id": "la-10",
74+ "metadata": {},
75+ "execution_count": null,
76+ "outputs": [],
77+ "source": "A = [4 -2 1; -2 4 -2; 1 -2 3];\nnorm([3 4])\nnorm([3 4], Inf)\nnorm(A, 'fro')"
78+ }
79+ ],
80+ "metadata": {
81+ "kernelspec": {
82+ "display_name": "numbl (MATLAB syntax)",
83+ "language": "numbl",
84+ "name": "numbl"
85+ },
86+ "language_info": {
87+ "codemirror_mode": "octave",
88+ "file_extension": ".m",
89+ "mimetype": "text/x-octave",
90+ "name": "numbl",
91+ "nbconvert_exporter": "script",
92+ "pygments_lexer": "matlab"
93+ }
94+ },
95+ "nbformat": 4,
96+ "nbformat_minor": 5
97+}
demo/content/advanced/05-data-structures-and-strings.ipynbadded+125−0View file
@@ -0,0 +1,125 @@
1+{
2+ "cells": [
3+ {
4+ "cell_type": "markdown",
5+ "id": "ds-0",
6+ "metadata": {},
7+ "source": "# Data structures and strings\n\nCell arrays, structs, dictionaries, and text handling."
8+ },
9+ {
10+ "cell_type": "markdown",
11+ "id": "ds-1",
12+ "metadata": {},
13+ "source": "## Cell arrays hold mixed types"
14+ },
15+ {
16+ "cell_type": "code",
17+ "id": "ds-2",
18+ "metadata": {},
19+ "execution_count": null,
20+ "outputs": [],
21+ "source": "c = {42, 'hello', [1 2 3]};\nc{2}\nclass(c{3})\nnumel(c)"
22+ },
23+ {
24+ "cell_type": "markdown",
25+ "id": "ds-3",
26+ "metadata": {},
27+ "source": "## Structs"
28+ },
29+ {
30+ "cell_type": "code",
31+ "id": "ds-4",
32+ "metadata": {},
33+ "execution_count": null,
34+ "outputs": [],
35+ "source": "p.name = 'ada';\np.year = 1815;\np.tags = {'math', 'cs'};\nfieldnames(p)\np.tags{1}"
36+ },
37+ {
38+ "cell_type": "markdown",
39+ "id": "ds-5",
40+ "metadata": {},
41+ "source": "## Struct arrays"
42+ },
43+ {
44+ "cell_type": "code",
45+ "id": "ds-6",
46+ "metadata": {},
47+ "execution_count": null,
48+ "outputs": [],
49+ "source": "people(1).name = 'A'; people(1).age = 30;\npeople(2).name = 'B'; people(2).age = 25;\nages = [people.age]\nnames = {people.name}"
50+ },
51+ {
52+ "cell_type": "markdown",
53+ "id": "ds-7",
54+ "metadata": {},
55+ "source": "## Key/value maps with containers.Map"
56+ },
57+ {
58+ "cell_type": "code",
59+ "id": "ds-8",
60+ "metadata": {},
61+ "execution_count": null,
62+ "outputs": [],
63+ "source": "prices = containers.Map({'apple', 'banana', 'cherry'}, {3, 5, 7});\nprices('banana')\nkeys(prices)"
64+ },
65+ {
66+ "cell_type": "markdown",
67+ "id": "ds-9",
68+ "metadata": {},
69+ "source": "## Formatting and parsing text"
70+ },
71+ {
72+ "cell_type": "code",
73+ "id": "ds-10",
74+ "metadata": {},
75+ "execution_count": null,
76+ "outputs": [],
77+ "source": "s = sprintf('%d items at $%.2f = $%.2f', 3, 1.50, 4.50)\nnum2str(pi, 8)\nstr2double('3.14')"
78+ },
79+ {
80+ "cell_type": "markdown",
81+ "id": "ds-11",
82+ "metadata": {},
83+ "source": "## String functions"
84+ },
85+ {
86+ "cell_type": "code",
87+ "id": "ds-12",
88+ "metadata": {},
89+ "execution_count": null,
90+ "outputs": [],
91+ "source": "strsplit('a,b,c', ',')\nstrjoin({'x', 'y', 'z'}, '-')\nupper('numbl')\nstrrep('banana', 'a', 'o')"
92+ },
93+ {
94+ "cell_type": "markdown",
95+ "id": "ds-13",
96+ "metadata": {},
97+ "source": "## String arrays and regular expressions"
98+ },
99+ {
100+ "cell_type": "code",
101+ "id": "ds-14",
102+ "metadata": {},
103+ "execution_count": null,
104+ "outputs": [],
105+ "source": "names = [\"Ada\" \"Alan\" \"Grace\"];\nnames(2)\ncontains(names, \"A\")\nregexp('id=42; val=7', '\\d+', 'match')"
106+ }
107+ ],
108+ "metadata": {
109+ "kernelspec": {
110+ "display_name": "numbl (MATLAB syntax)",
111+ "language": "numbl",
112+ "name": "numbl"
113+ },
114+ "language_info": {
115+ "codemirror_mode": "octave",
116+ "file_extension": ".m",
117+ "mimetype": "text/x-octave",
118+ "name": "numbl",
119+ "nbconvert_exporter": "script",
120+ "pygments_lexer": "matlab"
121+ }
122+ },
123+ "nbformat": 4,
124+ "nbformat_minor": 5
125+}
demo/content/advanced/06-numerical-methods.ipynbadded+111−0View file
@@ -0,0 +1,111 @@
1+{
2+ "cells": [
3+ {
4+ "cell_type": "markdown",
5+ "id": "nm-0",
6+ "metadata": {},
7+ "source": "# Numerical methods\n\nPolynomials, interpolation, the FFT, statistics, and quadrature."
8+ },
9+ {
10+ "cell_type": "markdown",
11+ "id": "nm-1",
12+ "metadata": {},
13+ "source": "## Polynomials: fit, evaluate, roots"
14+ },
15+ {
16+ "cell_type": "code",
17+ "id": "nm-2",
18+ "metadata": {},
19+ "execution_count": null,
20+ "outputs": [],
21+ "source": "x = 0:0.5:5;\ny = 2 * x.^2 - 3 * x + 1;\np = polyfit(x, y, 2)\npolyval(p, 10)\nroots([1 -3 2])'"
22+ },
23+ {
24+ "cell_type": "markdown",
25+ "id": "nm-3",
26+ "metadata": {},
27+ "source": "## Interpolation"
28+ },
29+ {
30+ "cell_type": "code",
31+ "id": "nm-4",
32+ "metadata": {},
33+ "execution_count": null,
34+ "outputs": [],
35+ "source": "xs = 0:10;\nys = xs.^2;\ninterp1(xs, ys, 3.5)\ninterp1(xs, ys, [1.5 5.5 8.5])"
36+ },
37+ {
38+ "cell_type": "markdown",
39+ "id": "nm-5",
40+ "metadata": {},
41+ "source": "## The FFT"
42+ },
43+ {
44+ "cell_type": "code",
45+ "id": "nm-6",
46+ "metadata": {},
47+ "execution_count": null,
48+ "outputs": [],
49+ "source": "t = 0:0.01:1 - 0.01;\nsig = sin(2 * pi * 5 * t) + 0.5 * sin(2 * pi * 12 * t);\nF = abs(fft(sig));\n[~, idx] = max(F(1:50));\nfprintf('dominant frequency near %d Hz\\n', idx - 1);"
50+ },
51+ {
52+ "cell_type": "markdown",
53+ "id": "nm-7",
54+ "metadata": {},
55+ "source": "## Basic statistics"
56+ },
57+ {
58+ "cell_type": "code",
59+ "id": "nm-8",
60+ "metadata": {},
61+ "execution_count": null,
62+ "outputs": [],
63+ "source": "data = [4 8 15 16 23 42];\n[mean(data), median(data), std(data)]\n[min(data), max(data)]"
64+ },
65+ {
66+ "cell_type": "markdown",
67+ "id": "nm-9",
68+ "metadata": {},
69+ "source": "## Sorting, differences, cumulative sums"
70+ },
71+ {
72+ "cell_type": "code",
73+ "id": "nm-10",
74+ "metadata": {},
75+ "execution_count": null,
76+ "outputs": [],
77+ "source": "v = [3 1 4 1 5 9 2 6];\nsort(v)\nunique(v)\ncumsum(v)\ndiff(v)"
78+ },
79+ {
80+ "cell_type": "markdown",
81+ "id": "nm-11",
82+ "metadata": {},
83+ "source": "## Numerical integration with trapz"
84+ },
85+ {
86+ "cell_type": "code",
87+ "id": "nm-12",
88+ "metadata": {},
89+ "execution_count": null,
90+ "outputs": [],
91+ "source": "x = linspace(0, pi, 200);\napprox = trapz(x, sin(x)) % exact value is 2"
92+ }
93+ ],
94+ "metadata": {
95+ "kernelspec": {
96+ "display_name": "numbl (MATLAB syntax)",
97+ "language": "numbl",
98+ "name": "numbl"
99+ },
100+ "language_info": {
101+ "codemirror_mode": "octave",
102+ "file_extension": ".m",
103+ "mimetype": "text/x-octave",
104+ "name": "numbl",
105+ "nbconvert_exporter": "script",
106+ "pygments_lexer": "matlab"
107+ }
108+ },
109+ "nbformat": 4,
110+ "nbformat_minor": 5
111+}
demo/content/advanced/07-plotting-gallery.ipynbadded+127−0View file
@@ -0,0 +1,127 @@
1+{
2+ "cells": [
3+ {
4+ "cell_type": "markdown",
5+ "id": "pl-0",
6+ "metadata": {},
7+ "source": "# Plotting gallery\n\nEach cell renders its figures into the cell output. 3-D figures are\ninteractive (drag to rotate)."
8+ },
9+ {
10+ "cell_type": "markdown",
11+ "id": "pl-1",
12+ "metadata": {},
13+ "source": "## Line plots with styles and a legend"
14+ },
15+ {
16+ "cell_type": "code",
17+ "id": "pl-2",
18+ "metadata": {},
19+ "execution_count": null,
20+ "outputs": [],
21+ "source": "x = linspace(0, 2 * pi, 200);\nplot(x, sin(x), 'b-');\nhold on;\nplot(x, cos(x), 'r--');\nhold off;\nlegend('sin', 'cos');\nxlabel('x'); ylabel('y'); title('sine and cosine');"
22+ },
23+ {
24+ "cell_type": "markdown",
25+ "id": "pl-3",
26+ "metadata": {},
27+ "source": "## Scatter plots"
28+ },
29+ {
30+ "cell_type": "code",
31+ "id": "pl-4",
32+ "metadata": {},
33+ "execution_count": null,
34+ "outputs": [],
35+ "source": "rng(0);\nn = 80;\nxs = randn(1, n);\nys = xs + 0.4 * randn(1, n);\nscatter(xs, ys);\nxlabel('x'); ylabel('y'); title('correlated samples');"
36+ },
37+ {
38+ "cell_type": "markdown",
39+ "id": "pl-5",
40+ "metadata": {},
41+ "source": "## Bar charts and histograms"
42+ },
43+ {
44+ "cell_type": "code",
45+ "id": "pl-6",
46+ "metadata": {},
47+ "execution_count": null,
48+ "outputs": [],
49+ "source": "bar([5 3 8 2 7]);\ntitle('bar chart');"
50+ },
51+ {
52+ "cell_type": "code",
53+ "id": "pl-7",
54+ "metadata": {},
55+ "execution_count": null,
56+ "outputs": [],
57+ "source": "rng(0);\nhistogram(randn(1, 1000));\ntitle('1000 normal samples');"
58+ },
59+ {
60+ "cell_type": "markdown",
61+ "id": "pl-8",
62+ "metadata": {},
63+ "source": "## Surfaces and contours"
64+ },
65+ {
66+ "cell_type": "code",
67+ "id": "pl-9",
68+ "metadata": {},
69+ "execution_count": null,
70+ "outputs": [],
71+ "source": "[X, Y] = meshgrid(linspace(-3, 3, 40));\nZ = X .* exp(-X.^2 - Y.^2);\nsurf(Z);\ntitle('a surface (drag to rotate)');"
72+ },
73+ {
74+ "cell_type": "code",
75+ "id": "pl-10",
76+ "metadata": {},
77+ "execution_count": null,
78+ "outputs": [],
79+ "source": "contour(peaks(60));\ntitle('contours of peaks');"
80+ },
81+ {
82+ "cell_type": "markdown",
83+ "id": "pl-11",
84+ "metadata": {},
85+ "source": "## Images and colorbars"
86+ },
87+ {
88+ "cell_type": "code",
89+ "id": "pl-12",
90+ "metadata": {},
91+ "execution_count": null,
92+ "outputs": [],
93+ "source": "imagesc(magic(8));\ncolorbar;\ntitle('magic(8)');"
94+ },
95+ {
96+ "cell_type": "markdown",
97+ "id": "pl-13",
98+ "metadata": {},
99+ "source": "## Subplots"
100+ },
101+ {
102+ "cell_type": "code",
103+ "id": "pl-14",
104+ "metadata": {},
105+ "execution_count": null,
106+ "outputs": [],
107+ "source": "x = linspace(0, 2 * pi, 100);\nsubplot(2, 2, 1); plot(x, sin(x)); title('sin');\nsubplot(2, 2, 2); plot(x, cos(x)); title('cos');\nsubplot(2, 2, 3); plot(x, sin(2 * x)); title('sin(2x)');\nsubplot(2, 2, 4); plot(x, sin(x) .* cos(x)); title('sin*cos');"
108+ }
109+ ],
110+ "metadata": {
111+ "kernelspec": {
112+ "display_name": "numbl (MATLAB syntax)",
113+ "language": "numbl",
114+ "name": "numbl"
115+ },
116+ "language_info": {
117+ "codemirror_mode": "octave",
118+ "file_extension": ".m",
119+ "mimetype": "text/x-octave",
120+ "name": "numbl",
121+ "nbconvert_exporter": "script",
122+ "pygments_lexer": "matlab"
123+ }
124+ },
125+ "nbformat": 4,
126+ "nbformat_minor": 5
127+}
demo/content/advanced/fib.madded+8−0View file
@@ -0,0 +1,8 @@
1+function y = fib(n)
2+% Recursive Fibonacci. Named functions live in .m files, not in cells.
3+if n <= 2
4+ y = 1;
5+else
6+ y = fib(n - 1) + fib(n - 2);
7+end
8+end
demo/content/advanced/minmax.madded+5−0View file
@@ -0,0 +1,5 @@
1+function [lo, hi] = minmax(v)
2+% Returns two outputs.
3+lo = min(v);
4+hi = max(v);
5+end