concept-collection / sample-6
Initial commit
Jeremy Magland <magland@users.noreply.github.com> committed commit 0a4fb769c3fc Browse files
8 changed files+114−0
README.mdadded+22−0View file
@@ -0,0 +1,22 @@
1+# numbl sample project
2+
3+This project runs MATLAB-syntax `.m` files in your browser with
4+[numbl](https://numbl.org). Open a script and press the **▶** button in the
5+tab bar:
6+
7+- `waves.m` — damped oscillations: printed output goes to the **Output**
8+ panel at the bottom, the plot opens as **Figure 1** in the side bar to the
9+ right. Calls `damped_wave.m` (functions next to the script are found
10+ automatically, like MATLAB's path rules).
11+- `scripts/surface_demo.m` — a 3-D surface. Uses `addpath` to call a
12+ function from the `lib/` folder.
13+- `scripts/animation.m` — figures update live during the run (`drawnow`).
14+- `scripts/write_results.m` — writes a file; it appears in the Explorer
15+ when the run finishes.
16+- `scripts/mip_demo.m` — installs the `inpoly` package from the
17+ [mip](https://mip.sh) registry on first run (`mip load --install`) and
18+ uses it. Installed packages are cached in your browser, shared by all
19+ projects, and refreshed automatically after 30 minutes of inactivity.
20+
21+Edits are saved with **Ctrl+S** and persist in your browser. Runs use the
22+current editor contents, saved or not. Press **⏹** to stop a runaway run.
damped_wave.madded+4−0View file
@@ -0,0 +1,4 @@
1+function y = damped_wave(t, f, tau)
2+% DAMPED_WAVE Exponentially decaying sine wave.
3+y = exp(-t / tau) .* sin(2 * pi * f * t);
4+end
lib/mexican_hat.madded+5−0View file
@@ -0,0 +1,5 @@
1+function z = mexican_hat(x, y)
2+% MEXICAN_HAT Ricker wavelet surface.
3+r2 = x.^2 + y.^2;
4+z = (1 - r2) .* exp(-r2 / 2);
5+end
scripts/animation.madded+12−0View file
@@ -0,0 +1,12 @@
1+% Live figure updates: drawnow flushes the figure mid-run.
2+% This one takes a while — try the stop button (⏹) in the tab bar.
3+x = linspace(0, 4 * pi, 400);
4+figure;
5+for k = 1:200
6+ plot(x, sin(x - k / 4) .* exp(-x / 8));
7+ axis([0 4*pi -1 1]);
8+ title(sprintf('frame %d / 200', k));
9+ drawnow;
10+ pause(0.04);
11+end
12+disp('animation done');
scripts/mip_demo.madded+25−0View file
@@ -0,0 +1,25 @@
1+% mip package demo: installs the inpoly package (point-in-polygon test)
2+% from the mip registry (https://mip.sh) on first run, then reuses the
3+% cached install. See https://mip.sh for available packages.
4+mip load --install inpoly
5+
6+% a star-shaped polygon
7+k = (0:9)';
8+r = 1 + 0.6 * (-1).^k;
9+node = [r .* cos(pi/5 * k + pi/2), r .* sin(pi/5 * k + pi/2)];
10+
11+% classify random points
12+rng(42);
13+pts = 4 * rand(2000, 2) - 2;
14+inside = inpoly2(pts, node);
15+fprintf('points inside: %d / %d\n', sum(inside), length(inside));
16+
17+figure;
18+plot(pts(inside, 1), pts(inside, 2), '.');
19+hold on;
20+plot(pts(~inside, 1), pts(~inside, 2), '.');
21+plot([node(:, 1); node(1, 1)], [node(:, 2); node(1, 2)]);
22+hold off;
23+axis equal;
24+title('inpoly2: points inside a star');
25+legend('inside', 'outside', 'polygon');
scripts/surface_demo.madded+15−0View file
@@ -0,0 +1,15 @@
1+% 3-D surface demo. mexican_hat lives in ../lib, so put that folder on
2+% the search path first (files next to the running script are found
3+% automatically; other folders follow MATLAB's addpath rules).
4+addpath('../lib');
5+
6+[X, Y] = meshgrid(linspace(-3, 3, 80), linspace(-3, 3, 80));
7+Z = mexican_hat(X, Y);
8+
9+figure;
10+surf(X, Y, Z);
11+title('Mexican hat');
12+xlabel('x');
13+ylabel('y');
14+
15+disp('surface rendered — drag to rotate');
scripts/write_results.madded+10−0View file
@@ -0,0 +1,10 @@
1+% Scripts can write files: results/summary.txt shows up in the Explorer
2+% when the run finishes.
3+mkdir('results');
4+fid = fopen('results/summary.txt', 'w');
5+fprintf(fid, 'n, n^2, sqrt(n)\n');
6+for n = 1:10
7+ fprintf(fid, '%d, %d, %.4f\n', n, n^2, sqrt(n));
8+end
9+fclose(fid);
10+disp('wrote results/summary.txt');
waves.madded+21−0View file
@@ -0,0 +1,21 @@
1+% Damped harmonic oscillation — press the run button (▶) above.
2+f = 2.5; % frequency (Hz)
3+tau = 0.8; % decay time constant (s)
4+
5+t = linspace(0, 3, 600);
6+y = damped_wave(t, f, tau);
7+
8+fprintf('samples: %d\n', numel(t));
9+fprintf('peak amplitude: %.4f\n', max(abs(y)));
10+fprintf('rms amplitude: %.4f\n', sqrt(mean(y.^2)));
11+
12+figure;
13+plot(t, y);
14+hold on;
15+plot(t, exp(-t / tau), '--');
16+plot(t, -exp(-t / tau), '--');
17+hold off;
18+title('Damped oscillation');
19+xlabel('t (s)');
20+ylabel('amplitude');
21+legend('signal', 'envelope', '-envelope');