I browsed the website of ModelingToolkit.jl and did not find an example that covers a very simple math problem.
Let’s say that I’d like to maximize y by choosing a value of x:
y = a - b x^2
where a = c + dx - e.
The math is simple. The solution is x^* = \frac{d}{2b} where y is maximized.
I understand that using SymPy.jl is probably the more common approach. However, I’d like to explore ModelingToolkit.jl for its potentially better speed. I’d appreciate if someone could provide an example code to the simple optimization problem above.
For this problem, there isn’t much difference in the approach between Symbolics and Sympy, as the derivative is a linear function in the unknown, and Symbolics.solve_for (used below handles those with ease.)
julia> using Symbolics
julia> @variables a b c x
4-element Vector{Num}:
a
b
c
x
julia> ex = a*x^2 + b*x + c
c + a*(x^2) + b*x
julia> ex′ = Symbolics.derivative(ex, x)
b + 2a*x
julia> Symbolics.solve_for(ex′ ~ 0, x)
(-1//2)*b*(a^-1)
Whereas, you might see:
julia> using SymPy
julia> @syms a b c x
(a, b, c, x)
julia> ex = a*x^2 + b*x + c
2
a⋅x + b⋅x + c
julia> ex′ = diff(ex, x)
2⋅a⋅x + b
julia> solve(ex′, x)
1-element Vector{Sym}:
-b/(2*a)
That’s the symbolic way, and then symbolic-numeric:
using ModelingToolkit, GalacticOptim
@variables x y
@parameters b c d e
a = c + d*x - e
loss = a - b*x^2
sys = OptimizationSystem(-loss,[x],[b,c,d,e])
u0 = [
x=>0.0
]
p = [
b => 1.2
c => 0.2
d => 0.3
e => 0.5
]
prob = OptimizationProblem(sys,u0,p,grad=true)
sol = solve(prob,BFGS())
sol.u # [0.12499999999999999]
Thank you very much! At the moment I focus more on the symbolic math. The plan is to run numeric simulation later on, probably in a few weeks (or a few months or years, if I turn out to progress very slowly). Your example codes will be very helpful once I started numerical simulation, I think!
I see that your codes, there was one equation, ex = a*x^2 + b*x + c, for example.
In the example of y=a−bx^2, where a=c+dx−e, I wonder if it is possible to input two equations in Julia, and ask Julia to solve it?
Of course I could substitute a into y myself, and feed the resulted equation to Julia, using your example codes. My interest is to ask Julia to solve more complex problems where there are more equations involved. That’s why I’m interested in learning how I can feed more than one equations to Julia, and ask Julia to solve them.
In general, I have been more interested in packages that are written in Julia, because I think such packages are going to have more speed advantages. However, does it mean that SymPy is at the moment a more capable package than Symbolics, for example, because Symbolics only has linear equations for now?
A side note is that the results from SymPy are much more readable than from Symbolics.