6b31a9aSync .m workspace files from the notebook directory into the sessionJeremy Magland 2 "cells": [
3 {
4 "cell_type": "markdown",
5 "id": "intro-title",
6 "metadata": {},
7 "source": [
8 "# MATLAB syntax, entirely in your browser\n",
9 "\n",
10 "This notebook runs on the **numbl kernel for JupyterLite**. Every cell executes\n",
11 "in a Web Worker in *your* browser tab — there is no server and no kernel\n",
12 "process behind this page, and nothing to install.\n",
13 "\n",
14 "The engine is [numbl](https://github.com/flatironinstitute/numbl), an\n",
15 "open-source MATLAB-syntax language implementation in TypeScript.\n",
16 "\n",
17 "Run the cells below with **Shift+Enter**."
18 ]
21 "cell_type": "code",
22 "execution_count": null,
23 "id": "intro-matrix",
24 "metadata": {},
25 "outputs": [],
26 "source": [
27 "A = [4 2 1; 2 5 3; 1 3 6]"
28 ]
29 },
30 {
31 "cell_type": "code",
32 "execution_count": null,
33 "id": "intro-solve",
34 "metadata": {},
35 "outputs": [],
36 "source": [
37 "% Variables persist across cells; solve a linear system with backslash\n",
38 "b = [1; 2; 3];\n",
39 "x = A \\ b\n",
40 "residual = norm(A*x - b)"
41 ]
42 },
43 {
44 "cell_type": "code",
45 "execution_count": null,
46 "id": "intro-indexing",
47 "metadata": {},
48 "outputs": [],
49 "source": [
50 "% MATLAB indexing: rows, and logical masks\n",
51 "A(2, :)\n",
52 "A(A > 3)'"
53 ]
54 },
55 {
56 "cell_type": "code",
57 "execution_count": null,
58 "id": "intro-loop",
59 "metadata": {},
60 "outputs": [],
61 "source": [
62 "total = 0;\n",
63 "for k = 1:10\n",
64 " total = total + k^2;\n",
65 "end\n",
66 "fprintf('sum of squares 1..10 = %d\\n', total);"
67 ]
68 },
69 {
70 "cell_type": "code",
71 "execution_count": null,
72 "id": "intro-anon",
73 "metadata": {},
74 "outputs": [],
75 "source": [
76 "% Anonymous functions work in cells (named functions belong in .m files)\n",
77 "f = @(t) exp(-t) .* cos(2*pi*t);\n",
78 "vals = arrayfun(f, 0:0.5:2)"
79 ]
80 },
81 {
82 "cell_type": "markdown",
83 "id": "1e814d6c",
84 "source": "## Named functions live in `.m` files\n\nnumbl's cells work like the MATLAB console: you can't define a *named*\nfunction directly in a cell. Instead, put it in a `.m` file next to the\nnotebook — this kernel syncs `.m` files from the file browser into the\nsession before every cell runs, so functions defined there are callable\nimmediately, and edits take effect the next time you run a cell.\n\nThis notebook ships with [statsutils.m](./statsutils.m) — open it, and try\nediting it (e.g. add a `median` field) while the cell below is still there.",
85 "metadata": {}
86 },
87 {
88 "cell_type": "code",
89 "id": "55d6bb19",
90 "source": "data = [2 4 4 4 5 5 7 9];\ns = statsutils(data);\nfprintf('mean=%.4f std=%.4f range=%.4f\\n', s.mean, s.std, s.range);",
91 "metadata": {},
92 "execution_count": null,
93 "outputs": []
94 },
95 {
96 "cell_type": "markdown",
97 "id": "intro-next",
98 "metadata": {},
99 "source": [
100 "Next: [plotting](./02-plotting.ipynb) and\n",
101 "[installing packages](./03-packages.ipynb) — both also fully client-side."
102 ]
103 }
104 ],
105 "metadata": {
106 "kernelspec": {
107 "display_name": "MATLAB (numbl)",
108 "language": "matlab",
109 "name": "numbl"
110 },
111 "language_info": {
112 "codemirror_mode": "octave",
113 "file_extension": ".m",
114 "mimetype": "text/x-octave",
115 "name": "matlab",
116 "pygments_lexer": "matlab"
117 }
118 },
119 "nbformat": 4,
120 "nbformat_minor": 5
121}