using DrWatson
@quickactivate "biomath_des"Example of Solving Nonlinear System
Let’s use the NonlinearSolve.jl package to find the roots of the system of equations
\[ f(x,y) = (x^2 + y^2 - 1, x^2 - y^2 + 0.5) \]
using ModelingToolkit, NonlinearSolve@variables x y
@parameters σ ρ\[ \begin{equation} \left[ \begin{array}{c} \sigma \\ \rho \\ \end{array} \right] \end{equation} \]
eqs = [0 ~ x^2 + y^2 - σ,
0 ~ x^2 - y^2 + ρ]
@named ns = NonlinearSystem(eqs, [x, y], [σ , ρ])\[ \begin{align} 0 =& - \sigma + x^{2} + y^{2} \\ 0 =& \rho + x^{2} - y^{2} \end{align} \]
u0 = [x => 1.0,
y => 1.0]
ps = [σ => 1.0
ρ => 0.5]2-element Vector{Pair{Num, Float64}}:
σ => 1.0
ρ => 0.5
prob = NonlinearProblem(ns, u0, ps)
sol = solve(prob, NewtonRaphson())retcode: Success
u: 2-element Vector{Float64}:
0.5000000000000006
0.8660254037844387
The last example shows off some of the features of the ModelingToolkit.jl package. We’ll learn more about this package in later sections but the point is that ModelingToolkit.jl allows us to combine numerical and symbolic mathematics. For example, we can print out the equations in the system in usual mathematical notation:
equations(ns)\[ \begin{align} 0 =& - \sigma + x^{2} + y^{2} \\ 0 =& \rho + x^{2} - y^{2} \end{align} \]
We can also ask for symbolic expressions for the Jacobian matrix of the system:
calculate_jacobian(ns)\[ \begin{equation} \left[ \begin{array}{cc} 2 x & 2 y \\ 2 x & - 2 y \\ \end{array} \right] \end{equation} \]