줄리아 미분방정식 패키지 DiffetentialEquations 튜토리얼
Description
DifferentialEquations.jl is one of the packages under the SciML group developed for the numerical solution of differential equations. The equations that can be solved with this package are as follows:
- Discrete equations (function maps, discrete stochastic (Gillespie/Markov) simulations)
- Ordinary Differential Equations (ODEs)
- Split and Partitioned ODEs (Symplectic integrators, IMEX Methods)
- Stochastic Differential Equations (SDEs)
- Stochastic differential-algebraic equations (SDAEs)
- Random differential equations (RODEs or RDEs)
- Differential algebraic equations (DAEs)
- Delay differential equations (DDEs)
- Neutral, retarded, and algebraic delay differential equations (NDDEs, RDDEs, and DDAEs)
- Stochastic delay differential equations (SDDEs)
- Experimental support for stochastic neutral, retarded, and algebraic delay differential equations (SNDDEs, SRDDEs, and SDDAEs)
- Mixed discrete and continuous equations (Hybrid Equations, Jump Diffusions)
- (Stochastic) partial differential equations ((S)PDEs) (with both finite difference and finite element methods)
For usage beyond tutorials, refer to the following.
- 🔒(25/03/23)Numerical solution of ODEs with external forces
- 🔒(25/03/25)Numerical solution of ODEs given data
Installation
Install by typing the following into the Julia REPL.
julia> using Pkg
julia> Pkg.add("DifferentialEquations")
Alternatively, press ] to enter Package Mode and type the following to install.
(@v1.10) pkg> add DifferentialEquations
ODEProblem
The solution of ordinary differential equations using DifferentialEquations.jl can be summarized simply as follows.
problem = ODEProblem(f, u0, tspan, p)
solution = solve(prob)
While this process will be covered in detail below, let’s first explore ODEProblem
. As the name implies, the ODEProblem
function defines an ordinary differential equation problem. It essentially takes four arguments, which we’ll examine one by one.
f
: A function of typeSciMLBase.ODEFunction
. When ordinary differential equations are expressed as below, it defines part . For memory efficiency, it can be defined in an in-place manner, such asf!(du, u, p, t)
.u0
: The initial condition. If is a scalar function, setu0 = 1.0
, and if it’s a vector function, setu0 = [1.0, 1.0, 1.0]
.tspan
: Input the domain of defined in the problem as a tuple:tspan = (t_start, t_end)
.p
: Parameters excluding the solution (e.g., external forces).
Suppose we are given the following ordinary differential equation.
Then, define the problem using the following code.
using DifferentialEquations
f(u, p, t) = u + p(t)
u0 = 0.0
tspan = (0.0, 1.0)
p(t) = sin(t)
prob = ODEProblem(f, u0, tspan, p)
By passing prob
as an argument to the solve
function, we can obtain the solution to the equation. Let’s now look at a more detailed example.
Solving Ordinary Differential Equations1
Consider the following exponential growth equation as a simple example of an ordinary differential equation.
While we already know the solution to this equation is of the form for some constant , we chose the simplest differential equation for this example. Specifically, let’s consider the following initial value problem.
First, define the right-hand side as function . Here, is a parameter, generally referring to the coefficients of the terms, and in this equation, it represents . Set the initial value and time range.
To explain more accurately, you’re defining the rule for updating . Define how is expressed as a function and input this into ODEProblem
. If is not input, it is set automatically. Essentially, after reformulating the differential equation to , define the part of . When the parameters are constants, need not be explicitly used inside the function.
using DifferentialEquations
f(u, p, t) = 4u
u0 = 0.5
tspan = (0.0, 1.0)
Using these, you can create the problem to solve, problem
, by inputting them into ODEProblem
.
julia> problem = ODEProblem(f, u0, tspan)
ODEProblem with uType Float64 and tType Float64. In-place: false
timespan: (0.0, 1.0)
u0: 0.5
Now, by inputting problem
into solve
, we get the numerical solution of the differential equation. The solver has provided the solution as a 7-dimensional vector.
julia> @time solution = solve(problem)
0.000074 seconds (192 allocations: 28.750 KiB)
retcode: Success
Interpolation: 3rd order Hermite
t: 7-element Vector{Float64}:
0.0
0.07581611843444613
0.21771610504885602
0.39336627353058856
0.6101928843972066
0.8638706427431251
1.0
u: 7-element Vector{Float64}:
0.5
0.581866090044407
0.7728154714821961
1.0981042913641448
1.6942469046842847
2.8139612733277692
3.6945246786578037
By using [`propert…