Skip to content

Redundant equations causes a crash #936

@azev77

Description

@azev77

The following nonlinear system causes a crash:

using ModelingToolkit, NonlinearSolve, Plots
if 1==1
      @variables LD, KD, Y, Π,   c, ℓ, LS, KS, v,   r, w
      @parameters αK, αL, γ, EH, EK     

      F(K,L,αK, αL) = (K^αK)*(L^αL)
      FK(K,L,αK,αL) = αK*(K^αK)*(L^αL)/K
      FL(K,L,αK,αL) = αL*(K^αK)*(L^αL)/L

      U(c,ℓ,γ) = γ*log(c) + (1.0 -γ)*log(ℓ)
      Uc(c,ℓ,γ) = γ/c 
      Uℓ(c,ℓ,γ) = (1.0 -γ)/ℓ

      eqs = [
            0.0 ~ r - FK(KD,LD,αK,αL),                    # r = F_K
            0.0 ~ w - FL(KD,LD,αK,αL),                    # w = F_L
            0.0 ~ Y - F(KD,LD,αK, αL),                    # Y = F(K,L)
            0.0 ~ Π - (Y- r*KD - w*LD),                   # Π = Y -rK -wL
            #
            0.0 ~ Uc(c,ℓ,γ)/Uℓ(c,ℓ,γ) - 1.0/w,            # Uc/Uℓ = 1/w   MRS = price ratio
            0.0 ~ c + w*- (w*EH + r*KS + Π),            # BC
            0.0 ~ EH - LS - ℓ,                            # ℓ = EH - L
            0.0 ~ EK - KS,                                # K ≤ EK  
            0.0 ~ v -  U(c,ℓ,γ),                          # value function 
            #
            0.0 ~ KD - KS,                                # Capital Market Clearing 
            0.0 ~ LD - LS,                                # Labor Market Clearing 
            0.0 ~ c - Y,                                  # Goods Market Clearing 
      ]
      ns = NonlinearSystem(eqs, 
            [LD, KD, Y, Π,   c, ℓ, LS, KS, v,   r, w,], 
            [αK, αL, γ, EH, EK,]
            )
      guess = [LD=>1.0, KD=>1.0, Y=>1.0, Π=>1.0,
            c=>1.0, ℓ=>1.0, LS=>1.0, KS=>1.0, v=>1.0,
            r=>1.0, w=>1.0]
      ps = [ αK=>0.45; αL=>0.51; γ=>0.5; EH=>24.0; EK=>10.0;]
      #ps = [ αK=>0.5; αL=>0.5; γ=>0.5; EH=>24.0; EK=>10.0;]
      prob = NonlinearProblem(ns,guess,ps)
      sol = solve(prob,NewtonRaphson())
      tuple(sol...)
end 

Message:
The terminal process "C:\Users\azevelev\AppData\Local\Programs\Julia-1.6.0\bin\julia.exe '-i', '--banner=no', '--project=C:\Users\azevelev.julia\environments\v1.6', 'c:\Users\azevelev.vscode\extensions\julialang.language-julia-1.1.37\scripts\terminalserver\terminalserver.jl', '\.\pipe\vsc-jl-repl-04d88e42-6073-49e7-a1c7-7d343e77fe5a', '\.\pipe\vsc-jl-cr-1ed06595-e8e0-49c4-9402-f2ce31c35611', 'USE_REVISE=true', 'USE_PLOTPANE=true', 'USE_PROGRESS=true', 'DEBUG_MODE=undefined'" terminated with exit code: 1.

It works if we comment out one (and only one) of the redundant market clearing equations:

using ModelingToolkit, NonlinearSolve, Plots
if 1==1
      @variables LD, KD, Y, Π,   c, ℓ, LS, KS, v,   r, w
      @parameters αK, αL, γ, EH, EK     

      F(K,L,αK, αL) = (K^αK)*(L^αL)
      FK(K,L,αK,αL) = αK*(K^αK)*(L^αL)/K
      FL(K,L,αK,αL) = αL*(K^αK)*(L^αL)/L

      U(c,ℓ,γ) = γ*log(c) + (1.0 -γ)*log(ℓ)
      Uc(c,ℓ,γ) = γ/c 
      Uℓ(c,ℓ,γ) = (1.0 -γ)/ℓ

      eqs = [
            0.0 ~ r - FK(KD,LD,αK,αL),                    # r = F_K
            0.0 ~ w - FL(KD,LD,αK,αL),                    # w = F_L
            0.0 ~ Y - F(KD,LD,αK, αL),                    # Y = F(K,L)
            0.0 ~ Π - (Y- r*KD - w*LD),                   # Π = Y -rK -wL
            #
            0.0 ~ Uc(c,ℓ,γ)/Uℓ(c,ℓ,γ) - 1.0/w,            # Uc/Uℓ = 1/w   MRS = price ratio
            0.0 ~ c + w*- (w*EH + r*KS + Π),            # BC
            0.0 ~ EH - LS - ℓ,                            # ℓ = EH - L
            0.0 ~ EK - KS,                                # K ≤ EK  
            0.0 ~ v -  U(c,ℓ,γ),                          # value function 
            #
            #0.0 ~ KD - KS,                                # Capital Market Clearing 
            0.0 ~ LD - LS,                                # Labor Market Clearing 
            0.0 ~ c - Y,                                  # Goods Market Clearing 
      ]
      ns = NonlinearSystem(eqs, 
            [LD, KD, Y, Π,   c, ℓ, LS, KS, v,   r, w,], 
            [αK, αL, γ, EH, EK,]
            )
      guess = [LD=>1.0, KD=>1.0, Y=>1.0, Π=>1.0,
            c=>1.0, ℓ=>1.0, LS=>1.0, KS=>1.0, v=>1.0,
            r=>1.0, w=>1.0]
      ps = [ αK=>0.45; αL=>0.51; γ=>0.5; EH=>24.0; EK=>10.0;]
      #ps = [ αK=>0.5; αL=>0.5; γ=>0.5; EH=>24.0; EK=>10.0;]
      prob = NonlinearProblem(ns,guess,ps)
      sol = solve(prob,NewtonRaphson())
      tuple(sol...)
end 

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