File tree Expand file tree Collapse file tree 2 files changed +4
-2
lines changed Expand file tree Collapse file tree 2 files changed +4
-2
lines changed Original file line number Diff line number Diff line change @@ -220,7 +220,8 @@ function generate_jacobian(
220
220
end
221
221
222
222
function calculate_hessian (sys:: NonlinearSystem ; sparse = false , simplify = false )
223
- rhs = [eq. rhs for eq in equations (sys)]
223
+ obs = Dict (eq. lhs => eq. rhs for eq in observed (sys))
224
+ rhs = map (eq -> fixpoint_sub (eq. rhs, obs), equations (sys))
224
225
vals = [dv for dv in unknowns (sys)]
225
226
if sparse
226
227
hess = [sparsehessian (rhs[i], vals, simplify = simplify) for i in 1 : length (rhs)]
Original file line number Diff line number Diff line change @@ -285,7 +285,7 @@ sys = structural_simplify(ns; conservative = true)
285
285
@test length (equations (sys)) == 1
286
286
287
287
# https://github.com/SciML/ModelingToolkit.jl/issues/2858
288
- @testset " Jacobian with observed equations that depend on unknowns" begin
288
+ @testset " Jacobian/Hessian with observed equations that depend on unknowns" begin
289
289
@variables x y z
290
290
@parameters σ ρ β
291
291
eqs = [0 ~ σ * (y - x)
@@ -312,6 +312,7 @@ sys = structural_simplify(ns; conservative = true)
312
312
eqs = [0 ~ x^ 2 + 2 * z + y, z ~ y, y ~ x] # analytical solution x = y = z = 0 or -3
313
313
@mtkbuild ns = NonlinearSystem (eqs) # solve for y with observed chain z -> x -> y
314
314
@test isequal (expand .(calculate_jacobian (ns)), [3 // 2 + y;;])
315
+ @test isequal (calculate_hessian (ns), [[1 ;;]])
315
316
prob = NonlinearProblem (ns, unknowns (ns) .=> - 4.0 ) # give guess < -3 to reach -3
316
317
sol = solve (prob, NewtonRaphson ())
317
318
@test sol[x] ≈ sol[y] ≈ sol[z] ≈ - 3
You can’t perform that action at this time.
0 commit comments