# First Steps

- [DifferentialEquations.jl docs](https://diffeq.sciml.ai/dev/index.html)

## Building an ODE model

How to define your model, state variables, initial (and/or boundary) conditions, and parameters.

As a simple example, the concentration of a decaying nuclear isotope could be described as an exponential decay:

$$
\frac{d}{dt}C(t) = - \lambda C(t)
$$

**State variable(s)**
- $C(t)$: The concentration of a decaying nuclear isotope.

**Parameter(s)**
- $\lambda$: The rate constant of decay. The half-life $t_{\frac{1}{2}} = \frac{ln2}{\lambda}$

For our ODE model to be compatible to the `DifferentialEquations.jl` ecosystem, the function for the right hand side should follow one of the following two formats:

- Out-of-place form: `f(u, p, t)` where `u` is the state variable(s), `p` is the parameter(s), and `t` is the independent variable (usually time). The output is the right hand side (RHS) of the differential equation system.
- In-place form: `f!(du, u, p, t)`, where the output is saved to `du`. The rest is the same as the out of place form. The in-place form has potential performance benefits since it allocates less arrays than the out-of-place form.


```julia
using DifferentialEquations

# The Exponential decay ODE model
expdecay(u, p, t) = p * u


p = -1.0 # Parameter
u0 = 1.0 # Initial condition
tspan = (0.0, 2.0) # Simulation start and end time points
prob = ODEProblem(expdecay, u0, tspan, p) # Define the problem
sol = solve(prob) # Solve the problem
```

We can then use `Plots.jl` to visualized the solution. `DifferentialEquations.jl` has defined a plot recipe so that one can call `plot(sol)` directly.


```julia
using Plots
plot(sol) # Visualize the solution
```

## The SIR model

A more complicated example is the [SIR model](https://www.maa.org/press/periodicals/loci/joma/the-sir-model-for-spread-of-disease-the-differential-equation-model) describing infectious disease spreading. There are more state variables and parameters.

$$
\begin{align}
\frac{d}{dt}S(t) &= - \beta S(t)I(t)  \\
\frac{d}{dt}I(t) &= \beta S(t)I(t)  - \gamma I(t)  \\
\frac{d}{dt}R(t) &= \gamma I(t)
\end{align}
$$

**State variable(s)**

- $S(t)$ : the fraction of susceptible people
- $I(t)$ : the fraction of infectious people
- $R(t)$ : the fraction of recovered (or removed) people

**Parameter(s)**

- $\beta$ : the rate of infection when susceptible and infectious people meet
- $\gamma$ : the rate of recovery of infectious people


```julia
using DifferentialEquations
using Plots

# SIR model
# In-place form
function sir!(du, u, p ,t)
	s, i, r = u
	β, γ = p
	v1 = β * s * i
	v2 = γ * i
    du[1] = -v1
    du[2] = v1 - v2
    du[3] = v2
	return nothing
end

# Parameters of the SIR model
p = (β = 1.0, γ = 0.3)
u0 = [0.99, 0.01, 0.00]  # s, i, r
tspan = (0.0, 20.0)

# Define a problem
prob = ODEProblem(sir!, u0, tspan, p)

# Solve the problem
sol = solve(prob)

# Visualize the solution
plot(sol, label=["S" "I" "R"], legend=:right)
```

0895721d5ad2a2985a0c94f5dde51cb0ab779a81
ipynb
Jupyter Notebook
docs/intro/01-first-steps.ipynb
sosiristseng/juliabook-diffeq
eedb41e2506f953bc3d37c095dd2c69a26a0799a
MIT
docs/intro/01-first-steps.ipynb
sosiristseng/juliabook-diffeq
eedb41e2506f953bc3d37c095dd2c69a26a0799a
MIT
2022-03-15T11:26:37.000Z
2022-03-27T02:39:34.000Z
docs/intro/01-first-steps.ipynb
sosiristseng/juliabook-diffeq
eedb41e2506f953bc3d37c095dd2c69a26a0799a
MIT
Qwen/Qwen-72B
1. YES
2. YES