{ "cells": [ { "cell_type": "markdown", "id": "intro-title", "metadata": {}, "source": [ "# MATLAB syntax, entirely in your browser\n", "\n", "This notebook runs on the **numbl kernel for JupyterLite**. Every cell executes\n", "in a Web Worker in *your* browser tab — there is no server and no kernel\n", "process behind this page, and nothing to install.\n", "\n", "The engine is [numbl](https://github.com/flatironinstitute/numbl), an\n", "open-source MATLAB-syntax language implementation in TypeScript.\n", "\n", "Run 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 indexing: rows, and logical masks\n", "A(2, :)\n", "A(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." ] } ], "metadata": { "kernelspec": { "display_name": "MATLAB (numbl)", "language": "matlab", "name": "numbl" }, "language_info": { "codemirror_mode": "octave", "file_extension": ".m", "mimetype": "text/x-octave", "name": "matlab", "pygments_lexer": "matlab" } }, "nbformat": 4, "nbformat_minor": 5 }