{ "cells": [ { "cell_type": "markdown", "id": "intro-title", "metadata": {}, "source": "# numbl: MATLAB syntax, entirely in your browser\n\nThis notebook runs on the **numbl kernel for JupyterLite**. Every cell executes\nin a Web Worker in *your* browser tab, with no server and no kernel process\nbehind this page, and nothing to install.\n\n[numbl](https://numbl.org) is an open-source numerical-computing engine\nwritten in TypeScript. It uses MATLAB syntax, so `.m` code runs unchanged.\n\nRun the cells below with **Shift+Enter**." }, { "cell_type": "code", "execution_count": null, "id": "intro-matrix", "metadata": {}, "outputs": [], "source": [ "A = [4 2 1; 2 5 3; 1 3 6]" ] }, { "cell_type": "code", "execution_count": null, "id": "intro-solve", "metadata": {}, "outputs": [], "source": [ "% Variables persist across cells; solve a linear system with backslash\n", "b = [1; 2; 3];\n", "x = A \\ b\n", "residual = norm(A*x - b)" ] }, { "cell_type": "code", "execution_count": null, "id": "intro-indexing", "metadata": {}, "outputs": [], "source": "% MATLAB-style indexing: rows, and logical masks\nA(2, :)\nA(A > 3)'" }, { "cell_type": "code", "execution_count": null, "id": "intro-loop", "metadata": {}, "outputs": [], "source": [ "total = 0;\n", "for k = 1:10\n", " total = total + k^2;\n", "end\n", "fprintf('sum of squares 1..10 = %d\\n', total);" ] }, { "cell_type": "code", "execution_count": null, "id": "intro-anon", "metadata": {}, "outputs": [], "source": [ "% Anonymous functions work in cells (named functions belong in .m files)\n", "f = @(t) exp(-t) .* cos(2*pi*t);\n", "vals = arrayfun(f, 0:0.5:2)" ] }, { "cell_type": "markdown", "id": "1e814d6c", "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.", "metadata": {} }, { "cell_type": "code", "id": "55d6bb19", "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);", "metadata": {}, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "id": "intro-next", "metadata": {}, "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)." } ], "metadata": { "kernelspec": { "display_name": "numbl (MATLAB syntax)", "language": "numbl", "name": "numbl" }, "language_info": { "codemirror_mode": "octave", "file_extension": ".m", "mimetype": "text/x-octave", "name": "numbl", "pygments_lexer": "matlab" } }, "nbformat": 4, "nbformat_minor": 5 }