Nonlinear systems

Updated: 2026-05-30

What is a nonlinear system?

Linear systems can be solved with direct proportional equations: if you double the input, the output doubles. They can typically be resolved step by step.

In many real scenarios, systems are nonlinear: the variables are so interdependent that you cannot solve them independently. You must solve them simultaneously.

Example: two water tanks connected by a pipe

If two tanks are connected by a pipe, the flow depends on the pressure difference, and each tank’s pressure depends on that same flow. That coupling makes the equations interdependent.

A_new = A - flow * DT
B_new = B + flow * DT
flow  = K * (A - B)

This may look manageable for two tanks, but once you extend the system (e.g., add a third tank), naive sequential updates can produce incorrect behavior because the equations are coupled.

Solving nonlinear coupled systems

GPSS-Plus provides the DYNAMIC resource to define a set of coupled equations (EXPRESSIONS) and the unknowns you want to solve for (STATES), along with helper parameters and previous-step values (VARIABLES).

Internally, this is solved iteratively (Newton-Raphson style) using Jacobians to converge on a solution at each time step.

Typical declaration

You define:

  • EXPRESSIONS: the equations of the system
  • STATES: the unknown variables to solve
  • VARIABLES: constants and previous-step values (e.g. _PREV)

Then, on every cycle, you call:

SOLVE {name:"sys", DT:0.1, SAVEVALUE:"result"}

and copy solved states back into SAVEVALUEs for plotting and visualization.

Changing parameters during runtime

You can alter values in VARIABLES at runtime using DYNAMIC_SET, for example changing the previous pressure of tank 2 to force a new equilibrium.

Summary

  • Coupled systems require simultaneous solving.
  • DYNAMIC models coupled physics with simple expressions.
  • SOLVE advances the system over time with convergence control.

Example

/*

 Nonlinear systems
 
*/
SYSTEM {TYPE:PRE_RUN, TRIGGER:PRE_RUN}
SYSTEM {type:OPTIONS, TIME_DECIMALS:1, SPEED:5}

PLOTTER {NAME:pressures, Y_0:tank1, Y_1:tank2, X:TIME_AC1}
PLOTTER {NAME:flowPlot, Y_0:flow, X:TIME_AC1}

Graphic {NAME:txtStatus, Type:TEXT, X:327, Y:486, Text:"txt"}   ; Status text

INITIAL TANK1_PRESSURE, 10
INITIAL TANK2_PRESSURE, 1
INITIAL K, 0.1

INITIAL dynamic_config, {
  EXPRESSIONS: [
    "PIPE_FLOW + PIPE_K * (TANK1_PRESSURE - TANK2_PRESSURE)",
    "TANK1_PRESSURE - TANK1_PRESSURE_PREV - PIPE_FLOW * DT",
    "TANK2_PRESSURE - TANK2_PRESSURE_PREV + PIPE_FLOW * DT"
  ],
  STATES: ["PIPE_FLOW", "TANK1_PRESSURE", "TANK2_PRESSURE"],
  VARIABLES: ["PIPE_K", "TANK1_PRESSURE_PREV", "TANK2_PRESSURE_PREV"]
}

INITIAL dynamic_values, {
  PIPE_K: X$K,
  TANK1_PRESSURE_PREV: X$TANK1_PRESSURE,
  TANK2_PRESSURE_PREV: X$TANK2_PRESSURE
}

DYNAMIC {name:sys,
  CONFIG:V$dynamic_config,
  VALUES:V$dynamic_values,
  X:300, Y:300,
  TOLERANCE: 1e-6, MAX_ITER: 10
}

START 1000

include ./library_graphics/tank.lib

;==============================================================

PROCEDURE agent.init

  timeout change_tank, 10
  advance 0.1

  while (1==1)

    SOLVE {name:"sys", DT:0.1, SAVEVALUE:"result"}

    SAVEVALUE TANK1_PRESSURE, X$(result.TANK1_PRESSURE)
    SAVEVALUE TANK2_PRESSURE, X$(result.TANK2_PRESSURE)
    SAVEVALUE PIPE_FLOW,      X$(result.PIPE_FLOW)

    PLOT pressures, X$TANK1_PRESSURE, X$TANK2_PRESSURE
    PLOT flowPlot,  X$PIPE_FLOW

    assign rFlow, round(X$PIPE_FLOW, 3)
    assign rH1,   round(X$TANK1_PRESSURE, 3)
    assign rH2,   round(X$TANK2_PRESSURE, 3)

    move {name:txtStatus, text:"flow: P$rFlow  T1: P$rH1  T2: P$rH2"}

    CALL tank1.tank.set, P$rH1
    CALL tank2.tank.set, P$rH2

    IF (ABS(X$PIPE_FLOW) < 0.001)
      stop
    ENDIF

    advance 0.1,0
  endwhile

  stop
ENDPROCEDURE

;====================================================================

PROCEDURE PRE_RUN
  call create_tanks
  TIMEOUT agent.init, 0
  TERMINATE_VE
ENDPROCEDURE

;=================================================

PROCEDURE create_tanks

  assign config,{title:"TANK 1",
    x:100, y:50,
    width:50, height:180,
    value:0,
    max_value:10,
    "color":"#ff0000"}

  timeout tank1.tank.init, 0, V$config

  assign config,{title:"TANK 2",
    x:400, y:50,
    width:50, height:180,
    value:0,
    max_value:10,
    "color":"#ff0000"}

  timeout tank2.tank.init, 0, V$config

ENDPROCEDURE

;=================================================

PROCEDURE change_tank
  assign newParams, {TANK2_PRESSURE_PREV:10}
  dynamic_set sys, V$newParams
  TERMINATE_VE
ENDPROCEDURE