On this page

SIR in NuML: your first NuML program

Manual contents | Model logic | Dashboard configuration | Platforms | Where NuML runs

This tutorial writes the classic SIR epidemic entirely in NuML inside a DynSS app. No JavaScript class is edited; the whole model — equations, sliders, graph, and clock — is one block of text. If you have read the SD manual's SDSir tutorial, this is the same model expressed in the language instead of the API.

1. Open a DynSS app

Open dynss2 (or any DynSS-family app) and click Model Definition to reveal the NuML code editor. The app provides a bank of dashboard components your program can mobilize by name: sliders Slider1 through Slider35, switches Switch1 through Switch4, four time-graph slots (TimeGraph, TimeGraph1, TimeGraph2, TimeGraph3), four phase-graph slots, tables, and more — see the component inventory. Components you do not reference stay hidden.

2. The complete program

Type (or paste) the following. Every NuML program begins with the string 'numl' on its own line.

'numl'
// model
deriv(S) @= -beta * S * I / N
deriv(I) @= beta * S * I / N - sigma * I
deriv(R) @= sigma * I

initial(S) @= N - I0
initial(I) @= I0
initial(R) @= 0

update(S) @= TimeGraph
update(I, R) @= TimeGraph1

N @= Slider1
beta @= Slider2
I0 @= Slider3
sigma @= Slider4

// configuration
TimeGraph #= [legend("S"), title("SIR Model -- Susceptible"), ylabel("Population")]
TimeGraph1 #= [legend("I", "R"), title("SIR Model -- Infected and Recovered"), ylabel("Population")]
Slider1 #= [legend("N"), min(1000), max(1000000), value(100000), step(1000)]
Slider2 #= [legend("Beta"), min(0), max(10), value(4), step(0.1)]
Slider3 #= [legend("I0"), min(0), max(10), value(1), step(0.1)]
Slider4 #= [legend("Sigma"), min(0), max(10), value(2), step(0.1)]
integration #= [maxTime(15), dt(0.5), method("RK4"), autorun]

3. Reading the model logic

The @= statements define the simulation:

Write all multiplication explicitly (beta * S, never beta S or 2S), and prefix JavaScript math with Math. (Math.exp(-r)); NuML's own primops such as sin, normal, and binomial need no prefix.

4. Reading the configuration

The #= statements shape the dashboard and the clock:

5. Run it

Press Save/Run (the editor's apply action) and then Run. With the defaults (N = 100,000, beta = 4, I0 = 1, sigma = 2, so R0 = 2):

If the editor reports Numl Error(s), the message names the offending line; the most common causes are implied multiplication, a missing initial, and a phantom variable — see rules and pitfalls.

6. Experiments

7. Where to go next