/ concept-collection / numbl-web-ide
Sign in
concept-collection / numbl-web-ide
numbl-web-ide / src / sampleWorkspace.ts
110 lines · 3.2 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.
19Edits are saved with **Ctrl+S** and persist in your browser. Runs use the
20current editor contents, saved or not. Press **⏹** to stop a runaway run.
21`;
23const wavesM = `% Damped harmonic oscillation — press the run button (▶) above.
24f = 2.5; % frequency (Hz)
25tau = 0.8; % decay time constant (s)
27t = linspace(0, 3, 600);
28y = damped_wave(t, f, tau);
30fprintf('samples: %d\\n', numel(t));
31fprintf('peak amplitude: %.4f\\n', max(abs(y)));
32fprintf('rms amplitude: %.4f\\n', sqrt(mean(y.^2)));
34figure;
35plot(t, y);
36hold on;
37plot(t, exp(-t / tau), '--');
38plot(t, -exp(-t / tau), '--');
39hold off;
40title('Damped oscillation');
41xlabel('t (s)');
42ylabel('amplitude');
43legend('signal', 'envelope', '-envelope');
44`;
46const dampedWaveM = `function y = damped_wave(t, f, tau)
47% DAMPED_WAVE Exponentially decaying sine wave.
48y = exp(-t / tau) .* sin(2 * pi * f * t);
49end
50`;
52const surfaceDemoM = `% 3-D surface demo. mexican_hat lives in ../lib, so put that folder on
53% the search path first (files next to the running script are found
54% automatically; other folders follow MATLAB's addpath rules).
55addpath('../lib');
57[X, Y] = meshgrid(linspace(-3, 3, 80), linspace(-3, 3, 80));
58Z = mexican_hat(X, Y);
60figure;
61surf(X, Y, Z);
62title('Mexican hat');
63xlabel('x');
64ylabel('y');
66disp('surface rendered — drag to rotate');
67`;
69const mexicanHatM = `function z = mexican_hat(x, y)
70% MEXICAN_HAT Ricker wavelet surface.
71r2 = x.^2 + y.^2;
72z = (1 - r2) .* exp(-r2 / 2);
73end
74`;
76const animationM = `% Live figure updates: drawnow flushes the figure mid-run.
77% This one takes a while — try the stop button (⏹) in the tab bar.
78x = linspace(0, 4 * pi, 400);
79figure;
80for k = 1:200
81 plot(x, sin(x - k / 4) .* exp(-x / 8));
82 axis([0 4*pi -1 1]);
83 title(sprintf('frame %d / 200', k));
84 drawnow;
85 pause(0.04);
86end
87disp('animation done');
88`;
90const writeResultsM = `% Scripts can write files: results/summary.txt shows up in the Explorer
91% when the run finishes.
92mkdir('results');
93fid = fopen('results/summary.txt', 'w');
94fprintf(fid, 'n, n^2, sqrt(n)\\n');
95for n = 1:10
96 fprintf(fid, '%d, %d, %.4f\\n', n, n^2, sqrt(n));
97end
98fclose(fid);
99disp('wrote results/summary.txt');
100`;
102export const sampleWorkspace: Record<string, string> = {
103 '/README.md': readme,
104 '/waves.m': wavesM,
105 '/damped_wave.m': dampedWaveM,
106 '/scripts/surface_demo.m': surfaceDemoM,
107 '/scripts/animation.m': animationM,
108 '/scripts/write_results.m': writeResultsM,
109 '/lib/mexican_hat.m': mexicanHatM,
110};
moveopenescclose