Differential Equations Solution
uses DifferentialEquations.jl
to solve the Problems. Build the problem and call generateDEcodes
, work done!
generateDEcodes
Overview of generateDEcodes
and generateJuMPcodes
generateDEcodes
and generateJuMPcodes
using the same way to build problem, and they both can solve Fixed Value(Example1) and Free End(Example2). But generateDEcodes
cannot deal with End constraint and Add variable limit. Meanwhile, generateDEcodes
have lower accuracy than generateJuMPcodes
. Maybe accuracy can be improved by pass different solver parameters. You can have a try.
Example | generateJuMPcodes | generateDEcodes |
---|---|---|
1. Fixed Value | ✅ | ✅ |
2. Free Value | ✅ | ✅ |
3. Constraint | ✅ | ❌ |
4. Multiple x and u |
✅ | ✅ |
5. variable limit | ✅ | ❌ |
✅ = supported
❌ = not supported
To solve:
Just define variables and build functions. Call generateDEcodes
and get the results.
The analytical solution of
$$x_1(t) = 0.5t^3-1.75t^2+t+1$$
and we can campare the difference between them by using Mean Square Error(MSE).
using OptControl, Statistics, ModelingToolkit
@variables t u x[1:2]
f = [0 1; 0 0] * x + [0, 1] * u
L = 0.5 * u^2
t0 = [1.0, 1.0]
tf = [0.0, 0.0]
tspan = (0.0, 2.0)
sol = generateDEcodes(L, f, x, u, tspan, t0, tf)
nu = [sol.u[i][1] for i in 1:length(sol.u)]
xs = collect(range(tspan[1], tspan[2], length=length(sol.u)))
an = @.(0.5 * xs^3 - 1.75 * xs^2 + xs + 1)
mean((an - nu).^2)
To solve:
If variable is free, use nothing
.
The analytical solution of
and get MSE.
using OptControl, Statistics, ModelingToolkit
@variables t u x[1:2]
f = [0 1; 0 0] * x + [0, 1] * u
L = 0.5 * u^2
t0 = [1.0, 1.0]
tf = [0.0, nothing]
tspan = (0.0, 1.0)
N = 100
sol = generateDEcodes(L, f, x, u, tspan, t0, tf)
nu = [sol.u[i][1] for i in 1:length(sol.u)]
xs = collect(range(tspan[1], tspan[2], length=length(sol.u)))
an = @.(xs^3 - 3.0 * xs^2 + xs + 1)
mean((an - nu).^2)
To solve:
The analytical solution of
and get MSE.
using OptControl, Statistics, ModelingToolkit
@variables t u[1:2] x[1:2]
f = [0 1; 0 0] * x + [1 0; 0 1] * u
L = 0.5 * (u[1]^2 + u[2]^2)
t0 = [1.0, 1.0]
tf = [0.0, nothing]
tspan = (0.0, 2.0)
sol = generateDEcodes(L, f, x, u, tspan, t0, tf)
nu1 = [sol.u[i][5] for i in 1:length(sol.u)]
nu2 = [sol.u[i][6] for i in 1:length(sol.u)]
xs = collect(range(tspan[1], tspan[2], length=length(sol.u)))
an_u2 = @.(9 / 14 * xs - 9 / 7)
res1 = mean((-9 / 14 .- nu1).^2)
res2 = mean((an_u2 - nu2).^2)
(res1,res2)