Skip to content

Commit

Permalink
Merge pull request #147 from baggepinnen/errormsg
Browse files Browse the repository at this point in the history
add better error message for invalid initial guess
  • Loading branch information
baggepinnen authored Nov 4, 2023
2 parents 501e667 + 374bd9b commit 11e312a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/src/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ ControlSystemIdentification.aic
- [`autocorplot`](@ref)
- [`crosscorplot`](@ref)
- [`modelfit`](@ref)
- [`rms`](@ref)
- [`mse`](@ref)
- [`sse`](@ref)

## Video tutorials

Expand Down
9 changes: 9 additions & 0 deletions ext/ControlSystemIdentificationLSOptExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ function _inner_pem(
T = length(yvv)
ny = size(R2, 1)

# Test the initial guess to give good error message
try
xp = discrete_dynamics(x0, uvv[1], p0, 0.0)
all(isfinite, xp) || throw(ArgumentError("Initial guess for x0 and p0 produced a non-finite state update ($xp). Improve the initial guess to avoid this error."))
catch e
@error "Aborting nonlinear PEM due to error in the dynamics update"
rethrow()
end

res = optimize!(
LeastSquaresProblem(;
x = p_guess,
Expand Down
6 changes: 5 additions & 1 deletion test/test_nonlinear_pem.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using ControlSystemIdentification, SeeToDee, LowLevelParticleFilters
using LeastSquaresOptim
using StaticArrays

using LinearAlgebra
using Test

function quadtank(h, u, p, t)
k1, k2, g = p[1], p[2], 9.81
Expand Down Expand Up @@ -60,6 +61,9 @@ isinteractive() && plot(d)
x00 = 0.5*[2.5, 1.5, 3, 3]
p0 = [1.4, 1.4, 5.1, 0.25]


@test_throws "Initial" ControlSystemIdentification.nonlinear_pem(d, discrete_dynamics, measurement, p0, Inf*x00, R1, 100R2, nu)

model = ControlSystemIdentification.nonlinear_pem(d, discrete_dynamics, measurement, p0, x00, R1, 100R2, nu)

p_opt = model.p
Expand Down

0 comments on commit 11e312a

Please sign in to comment.