/ concept-collection / numbl-web-ide
Sign in
concept-collection / numbl-web-ide
numbl-web-ide / src / sampleWorkspace.ts
142 lines · 4.3 KBBlameHistoryRaw
1// Files seeded into a new sample project.
3const readme = `# numbl sample project
5This project runs MATLAB-syntax \`.m\` files in your browser with
6[numbl](https://numbl.org). Open a script and press the **▶** button in the
7tab bar:
9- \`waves.m\` — damped oscillations: printed output goes to the **Output**
10 panel at the bottom, the plot opens as **Figure 1** in the side bar to the
11 right. Calls \`damped_wave.m\` (functions next to the script are found
12 automatically, like MATLAB's path rules).
13- \`scripts/surface_demo.m\` — a 3-D surface. Uses \`addpath\` to call a
14 function from the \`lib/\` folder.
15- \`scripts/animation.m\` — figures update live during the run (\`drawnow\`).
16- \`scripts/write_results.m\` — writes a file; it appears in the Explorer
17 when the run finishes.
18- \`scripts/mip_demo.m\` — installs the \`inpoly\` package from the
19 [mip](https://mip.sh) registry on first run (\`mip load --install\`) and
20 uses it. Installed packages are cached in your browser, shared by all
21 projects, and refreshed automatically after 30 minutes of inactivity.
23Edits are saved with **Ctrl+S** and persist in your browser. Runs use the
24current editor contents, saved or not. Press **⏹** to stop a runaway run.
25`;
27const wavesM = `% Damped harmonic oscillation — press the run button (▶) above.
28f = 2.5; % frequency (Hz)
29tau = 0.8; % decay time constant (s)
31t = linspace(0, 3, 600);
32y = damped_wave(t, f, tau);
34fprintf('samples: %d\\n', numel(t));
35fprintf('peak amplitude: %.4f\\n', max(abs(y)));
36fprintf('rms amplitude: %.4f\\n', sqrt(mean(y.^2)));
38figure;
39plot(t, y);
40hold on;
41plot(t, exp(-t / tau), '--');
42plot(t, -exp(-t / tau), '--');
43hold off;
44title('Damped oscillation');
45xlabel('t (s)');
46ylabel('amplitude');
47legend('signal', 'envelope', '-envelope');
48`;
50const dampedWaveM = `function y = damped_wave(t, f, tau)
51% DAMPED_WAVE Exponentially decaying sine wave.
52y = exp(-t / tau) .* sin(2 * pi * f * t);
53end
54`;
56const surfaceDemoM = `% 3-D surface demo. mexican_hat lives in ../lib, so put that folder on
57% the search path first (files next to the running script are found
58% automatically; other folders follow MATLAB's addpath rules).
59addpath('../lib');
61[X, Y] = meshgrid(linspace(-3, 3, 80), linspace(-3, 3, 80));
62Z = mexican_hat(X, Y);
64figure;
65surf(X, Y, Z);
66title('Mexican hat');
67xlabel('x');
68ylabel('y');
70disp('surface rendered — drag to rotate');
71`;
73const mexicanHatM = `function z = mexican_hat(x, y)
74% MEXICAN_HAT Ricker wavelet surface.
75r2 = x.^2 + y.^2;
76z = (1 - r2) .* exp(-r2 / 2);
77end
78`;
80const animationM = `% Live figure updates: drawnow flushes the figure mid-run.
81% This one takes a while — try the stop button (⏹) in the tab bar.
82x = linspace(0, 4 * pi, 400);
83figure;
84for k = 1:200
85 plot(x, sin(x - k / 4) .* exp(-x / 8));
86 axis([0 4*pi -1 1]);
87 title(sprintf('frame %d / 200', k));
88 drawnow;
89 pause(0.04);
90end
91disp('animation done');
92`;
94const writeResultsM = `% Scripts can write files: results/summary.txt shows up in the Explorer
95% when the run finishes.
96mkdir('results');
97fid = fopen('results/summary.txt', 'w');
98fprintf(fid, 'n, n^2, sqrt(n)\\n');
99for n = 1:10
100 fprintf(fid, '%d, %d, %.4f\\n', n, n^2, sqrt(n));
101end
102fclose(fid);
103disp('wrote results/summary.txt');
104`;
106const mipDemoM = `% mip package demo: installs the inpoly package (point-in-polygon test)
107% from the mip registry (https://mip.sh) on first run, then reuses the
108% cached install. See https://mip.sh for available packages.
109mip load --install inpoly
111% a star-shaped polygon
112k = (0:9)';
113r = 1 + 0.6 * (-1).^k;
114node = [r .* cos(pi/5 * k + pi/2), r .* sin(pi/5 * k + pi/2)];
116% classify random points
117rng(42);
118pts = 4 * rand(2000, 2) - 2;
119inside = inpoly2(pts, node);
120fprintf('points inside: %d / %d\\n', sum(inside), length(inside));
122figure;
123plot(pts(inside, 1), pts(inside, 2), '.');
124hold on;
125plot(pts(~inside, 1), pts(~inside, 2), '.');
126plot([node(:, 1); node(1, 1)], [node(:, 2); node(1, 2)]);
127hold off;
128axis equal;
129title('inpoly2: points inside a star');
130legend('inside', 'outside', 'polygon');
131`;
133export const sampleWorkspace: Record<string, string> = {
134 '/README.md': readme,
135 '/waves.m': wavesM,
136 '/damped_wave.m': dampedWaveM,
137 '/scripts/surface_demo.m': surfaceDemoM,
138 '/scripts/animation.m': animationM,
139 '/scripts/write_results.m': writeResultsM,
140 '/scripts/mip_demo.m': mipDemoM,
141 '/lib/mexican_hat.m': mexicanHatM,
142};
moveopenescclose