Julia Differential Equations Guide — ODE, PDE, and DifferentialEquations.jl
In this tutorial, you will learn about Julia Differential Equations Guide. We cover key concepts, practical examples, and best practices to help you master this topic.
Julia DifferentialEquations.jl provides efficient solvers for ordinary differential equations (ODEProblem), stochastic differential equations (SDEProblem), and delay differential equations (DDEProblem) -- with adaptive timestepping, event callbacks, and automated method selection.
Solving ODEs
using DifferentialEquations
# Define ODE: du/dt = -0.5u
function lorenz!(du, u, p, t)
du[1] = 10.0 * (u[2] - u[1])
du[2] = u[1] * (28.0 - u[3]) - u[2]
du[3] = u[1] * u[2] - (8/3) * u[3]
end
# Initial conditions
u0 = [1.0, 0.0, 0.0]
tspan = (0.0, 100.0)
# Define and solve
prob = ODEProblem(lorenz!, u0, tspan)
sol = solve(prob)
# Plot
using Plots
plot(sol, vars=(1, 2, 3), title="Lorenz Attractor")
Parameter Estimation
using DifferentialEquations, Optim
# ODE with parameters
function ode!(du, u, p, t)
du[1] = -p[1] * u[1]
end
u0 = [1.0]
tspan = (0.0, 10.0)
prob = ODEProblem(ode!, u0, tspan)
# Generate data
sol_true = solve(prob, p=[0.5])
data = sol_true(0:0.5:10) # sample
# Loss function
function loss(p)
sol = solve(prob, p=p)
pred = sol(0:0.5:10)
return sum((pred .- data) .^ 2)
end
# Optimize
result = optimize(loss, [1.0])
println("Estimated p = ", result.minimizer)
Stochastic Differential Equations
using DifferentialEquations
# SDE: dX = μX dt + σX dW
function sde!(du, u, p, t)
du[1] = 0.1 * u[1] # drift
end
function sde_g!(du, u, p, t)
du[1] = 0.2 * u[1] # diffusion
end
u0 = [1.0]
tspan = (0.0, 1.0)
prob = SDEProblem(sde!, sde_g!, u0, tspan)
sol = solve(prob, EM(), dt=0.001)
Event Handling
using DifferentialEquations
# ODE with callbacks
function bounce!(du, u, p, t)
du[1] = u[2]
du[2] = -9.81 # gravity
end
# Event: ball hits ground (u[1] = 0)
function condition(u, t, integrator)
u[1]
end
# Affect: reverse velocity with restitution
function affect!(integrator)
integrator.u[2] = -0.8 * integrator.u[2]
end
cb = ContinuousCallback(condition, affect!)
prob = ODEProblem(bounce!, [0.0, 10.0], (0.0, 5.0))
sol = solve(prob, callback=cb, dt=0.01)
Common Mistakes
1. Stiff systems with non-stiff solvers
Use alg_hint = :stiff or choose Rosenbrock23, TRBDF2 for stiff problems. Tsit5 is for non-stiff.
2. Inconsistent array types
State vectors must be consistent type. Use @. for element-wise operations. Float32 vs Float64 matters for performance.
3. Not providing good initial timestep
Adaptive solvers benefit from dt or dtmin/dtmax hints. Very large tspan without dt hint causes slow startup.
Practice Questions
1. How do you define an ODE problem?
ODEProblem(ode_function!, u0, tspan) where ode_function! modifies du in-place.
2. How do you solve an ODE?
solve(prob) with automatic algorithm selection. Specify solver: solve(prob, Tsit5()).
3. How do you handle events?
Use ContinuousCallback(condition, affect!) where condition triggers when crossing zero.
FAQ
{{< faq question="What solver should I use?" >}}
Start with Tsit5() for non-stiff and Rosenbrock23() for stiff. Use auto() for automatic selection.
{{< /faq >}}
{{< faq question="Can I solve PDEs?" >}} Yes. Use MethodOfLines.jl for PDEs via MOL, or ModelingToolkit.jl for PDESystem with finite difference/element methods. {{< /faq >}}
{{< faq question="How do I parallelize ODE solves?" >}}
Use EnsembleProblem for Monte Carlo simulations. EnsembleThreads() for parallel solving over parameter sets.
{{< /faq >}}
What's Next
Now continue learning more Julia topics.
| Topic | Description | Link |
|---|---|---|
| Symbolics | Symbolic computation | {{< ref "27-symbolics" >}} |
| GPU Computing | GPU programming in Julia | {{< ref "28-gpu" >}} |
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro