Skip to content

Linearize is failing when using AutoFiniteDiff() #185

Closed
@1-Bart-1

Description

@1-Bart-1

This MWE fails:

using ModelPredictiveControl, FiniteDiff, DifferentiationInterface

ad_type = AutoFiniteDiff()

function f(x, u, _ , p)
    g, L, K, m = p          # [m/s²], [m], [kg/s], [kg]
    θ, ω = x[1], x[2]       # [rad], [rad/s]
    τ  = u[1]               # [Nm]= ω
    dω = -g/L*sin(θ) - K/m*ω + τ/m/L^2
    return [dθ, dω]
end
h(x, _ , _ ) = [180/π*x[1]] # [°]
p_model = [9.8, 0.4, 1.2, 0.3]
nu, nx, ny, Ts = 1, 2, 1, 0.1
vu, vx, vy = ["\$τ\$ (Nm)"], ["\$θ\$ (rad)", "\$ω\$ (rad/s)"], ["\$θ\$ (°)"]
model = setname!(NonLinModel(f, h, Ts, nu, nx, ny; p=p_model, jacobian=ad_type); u=vu, x=vx, y=vy)

linmodel = ModelPredictiveControl.linearize(model, x=[π, 0], u=[0])

While it runs succesfully when using ad_type = AutoForwardDiff()

Stacktrace:

ERROR: LoadError: ArgumentError: range must be non-empty
Stacktrace:
  [1] maximum
    @ ./range.jl:851 [inlined]
  [2] finite_difference_jacobian!(J::Matrix{…}, f::DifferentiationInterface.FixTail{…}, x::Vector{…}, cache::FiniteDiff.JacobianCache{…}, f_in::Nothing; relstep::Float64, absstep::Float64, colorvec::UnitRange{…}, sparsity::Nothing, dir::Bool)
    @ FiniteDiff ~/.julia/packages/FiniteDiff/EBPBu/src/jacobians.jl:450
  [3] finite_difference_jacobian! (repeats 2 times)
    @ ~/.julia/packages/FiniteDiff/EBPBu/src/jacobians.jl:406 [inlined]
  [4] jacobian!
    @ ~/.julia/packages/DifferentiationInterface/7eD1K/ext/DifferentiationInterfaceFiniteDiffExt/twoarg.jl:391 [inlined]
  [5] linfunc!
    @ ~/.julia/packages/ModelPredictiveControl/NFmDi/src/model/linearization.jl:42 [inlined]
  [6] linearize_core!
    @ ~/.julia/packages/ModelPredictiveControl/NFmDi/src/model/linearization.jl:195 [inlined]
  [7] linearize!(linmodel::LinModel{…}, model::NonLinModel{…}; x::Vector{…}, u::Vector{…}, d::Vector{…})
    @ ModelPredictiveControl ~/.julia/packages/ModelPredictiveControl/NFmDi/src/model/linearization.jl:166
  [8] linearize!
    @ ~/.julia/packages/ModelPredictiveControl/NFmDi/src/model/linearization.jl:155 [inlined]
  [9] linearize(model::NonLinModel{…}; kwargs::@Kwargs{})
    @ ModelPredictiveControl ~/.julia/packages/ModelPredictiveControl/NFmDi/src/model/linearization.jl:131
 [10] top-level scope
    @ ~/Code/KitePredictiveControl.jl/mwe/finitediff.jl:37
 [11] include(fname::String)
    @ Main ./sysimg.jl:38
 [12] top-level scope
    @ REPL[1]:1
in expression starting at /home/bart/Code/KitePredictiveControl.jl/mwe/finitediff.jl:37
Some type information was truncated. Use `show(err)` to see complete types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions