> For the complete documentation index, see [llms.txt](https://www.opal-rt.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.opal-rt.com/docs/smart-power-simulation-software-sps/blocks-functions-tools/simulation-and-analysis/power_statespace.md).

# power\_statespace

## Description

The **power\_statespace function** computes state-space model of linear electrical circuit expressed as

$$
\dot{x}=Ax+Bu \\\[8pt]
y=Cx+Du
$$

where *x* is the vector of state-space variables (inductor currents and capacitor voltages), *u* is the vector of voltage and current inputs, and *y* is the vector of voltage and current outputs.

When you build a circuit from SPS blocks, power\_statespace is automatically called by the power\_analyze command. power\_statespace is also available as a stand-alone command for expert users. This allows you to generate state-space models without using the SPS block modeling interface and to access options that are not available through the SPS library. For example, using power\_statespace, you can model transformers and mutual inductances with more than three windings.

{% hint style="info" %}
You must call power\_statespace with a minimum of seven input arguments.
{% endhint %}

The linear circuit can contain any combination of voltage and current sources, RLC branches, multi-winding transformers, mutually coupled inductances, and switches. The state variables are inductor currents and capacitor voltages.

The state-space representation (matrices A,B,C,D, and vector x0) computed by power\_statespace can then be used in a Simulink® system, via a State-Space block, to perform simulation of the electrical circuit. Nonlinear elements (mechanical or power electronic switches, transformer saturation, machines, distributed parameter lines, etc.) can be connected to the linear circuit.

These Simulink models are interfaced with the linear circuit through voltage outputs and current inputs of the state-space model. You can find the models of the nonlinear elements provided with the SPS library.

## Syntax

{% code overflow="wrap" lineNumbers="true" %}

```matlab
[A,B,C,D,states,x0,x0sw,rlsw,u,x,y,freq,Asw,Bsw,Csw,Dsw,Hlin] = power_statespace(rlc,switches,source,line_dist,yout,y_type,unit )
```

{% endcode %}

{% code overflow="wrap" lineNumbers="true" %}

```matlab
[A,B,C,D,states,x0,x0sw,rlsw,u,x,y,freq,Asw,Bsw,Csw,Dsw,Hlin] = power_statespace(rlc,switches,source,line_dist,yout,y_type,unit , net_arg1,net_arg2,net_arg3,...,netsim_flag,fid_outfile, freq_sys,ref_node,vary_name,vary_val)
```

{% endcode %}

The second one computes the same as above but you can also specify optional arguments. To use these optional arguments, the number of input arguments must be 12, 13, 14 or 16.

## Input Arguments

The number of input arguments must be 7, 12, 13, 14, or 16. Arguments 8 to 16 are optional. The first seven arguments that must be specified are

<table data-header-hidden><thead><tr><th width="250.4444580078125"></th><th></th></tr></thead><tbody><tr><td><strong>rlc</strong></td><td><p>Branch matrix specifying the network topology as well as the resistance R, inductance L, and capacitance C values.</p><p>See format below.</p></td></tr><tr><td><strong>switches</strong></td><td><p>Switch matrix.</p><p>Specify an empty variable if no switches are used. See format below.</p></td></tr><tr><td><strong>source</strong></td><td>Source matrix specifying the parameters of the electrical voltage and current sources. Specify an empty variable if no sources are used. See format below.</td></tr><tr><td><strong>line_dist</strong></td><td>Distributed parameter line matrix. Specify an empty variable if no distributed lines are used. See format below.</td></tr><tr><td><strong>yout</strong></td><td>Matrix of output, expressed as character vectors. See format below.</td></tr><tr><td><strong>y_type</strong></td><td>Integer vector indicating output types (0 for voltage output, 1 for current output).</td></tr><tr><td><strong>unit</strong></td><td><p>Character vector specifying the units to be used for R, L, and C values in the rlc matrix.</p><p>If unit = 'OHM', R L C values are specified in ohms Ω at the fundamental frequency specified by freq_sys (default value is 60 Hz). If unit = 'OMU', R L C values are specified in ohms (Ω), millihenries (mH), and microfarads (µF).</p></td></tr></tbody></table>

The last nine arguments are optional. The first three are used to pass arguments from the power\_analyze command.

Hereafter, only the arguments to be specified when power\_statespace is used as a stand-alone command are described:

<table data-header-hidden><thead><tr><th width="249.5556640625"></th><th></th></tr></thead><tbody><tr><td><strong>net_arg1</strong>, <strong>net_arg2</strong>, <strong>net_arg3</strong></td><td><p>Used to pass arguments from power_analyze.</p><p>Specify an empty variable [] for each of these arguments.</p></td></tr><tr><td><strong>netsim_flag</strong></td><td><p>Integer controlling the messages displayed during the execution of power_statespace.</p><p>Default value is 0. If netsim_flag = 0, the version number, number of states, inputs, outputs, and modes are displayed.</p><p>Output values are displayed in polar form for each source frequency.</p><p>If netsim_flag = 1, only version number, number of states, inputs, and outputs are displayed. If netsim_flag = 2, no message is displayed during execution.</p></td></tr><tr><td><strong>fid_outfile</strong></td><td><p>File identifier of the power_statespace output file containing parameter values, node numbers, steady-state outputs, and special messages.</p><p>Default value is 0.</p></td></tr><tr><td><strong>freq_sys</strong></td><td>Fundamental frequency (Hz) considered for specification of X<sub>L</sub> and X<sub>C</sub> reactances if unit is set to 'OHM'. Default value is 60 Hz.</td></tr><tr><td><strong>ref_node</strong></td><td>Reference node number used for ground of PI transmission lines. If −1 is specified, the user is prompted to specify a node number.</td></tr><tr><td><strong>vary_name</strong></td><td>Matrix containing the symbolic variable names used in output, expressed as character vectors. These variables must be defined in your MATLAB<sup>®</sup> workspace.</td></tr><tr><td><strong>vary_val</strong></td><td>Vector containing the values of the variable names specified in vary_name.</td></tr></tbody></table>

## Output Arguments

<table data-header-hidden><thead><tr><th width="250.33331298828125"></th><th></th></tr></thead><tbody><tr><td><strong>A,B,C,D</strong></td><td>state-space matrices of the linear circuit with all switches open. > A(nstates, nstates) , B(nstates, ninput),``C(noutput, nstates) , D(noutput, ninput), where nstates is the number of state variables, ninput is the number of inputs, and noutput is the number of outputs.</td></tr><tr><td><strong>states</strong></td><td><p>Matrix containing the names of the state variables. Each name has the following format: Inductor currents: Il_bxx_nzz1_zz2 Capacitor voltages: Uc_bxx_nzz1_zz2 where > xx = branch number``zz1 = first node number of the branch``zz2 = second node number of the branch The last lines of the states matrix, which are followed by an asterisk, indicate inductor currents and capacitor voltages that are not considered as state variables.</p><p>This situation arises when inductor currents or capacitor voltages are not independent (inductors forming a cut set – for example, inductors connected in series – or capacitors forming a loop).</p><p>The currents and voltages followed by asterisks can be expressed as a linear combination of the other state variables.</p></td></tr><tr><td><strong>x0</strong></td><td>Column vector of initial values of state variables considering the open or closed status of switches.</td></tr><tr><td><strong>x0sw</strong></td><td>Vector of initial values of switch currents.</td></tr><tr><td><strong>rlsw</strong></td><td>Matrix (nswitch,2) containing the R and L values of series switch impedances in ohms (Ω) and henries (H). nswitch is the number of switches in the circuit.</td></tr><tr><td><strong>u,x,y</strong></td><td><p>Matrices u(ninput,nfreq), x(nstates,nfreq), and y(noutput,nfreq) containing the steady-state complex values of inputs, states, and outputs. nfreq is the length of the freq vector.</p><p>Each column corresponds to a different source frequency, as specified by the next argument, freq.</p></td></tr><tr><td><strong>freq</strong></td><td>Column vector containing the source frequencies ordered by increasing frequency.</td></tr><tr><td><strong>Asw,Bsw,Csw,Dsw</strong></td><td>State-space matrices of the circuit including the closed switches. Each closed switch with an internal inductance adds one extra state to the circuit.</td></tr><tr><td><strong>Hlin</strong></td><td>Three-dimensional array (nfreq, noutput, ninput) of the nfreq complex transfer impedance matrices of the linear system corresponding to each frequency of the freq vector.</td></tr></tbody></table>

## Format of the RLC Input Matrix

Two formats are allowed:

* Six columns: Implicit branch numbering. Branch numbers correspond to the RLC line numbers.
* Seven columns: Explicit branch numbering. Branch number Nobr is assigned by the user.

Each line of the RLC matrix must be specified according to the following format :

\[node1, node2, type, R, L, C, Nobr] for RLC branch or line branch

\[node1, node2, type, R, L, C, Nobr] for transformer magnetizing branch

\[node1, node2, type, R, L, U, Nobr] for transformer winding

\[node1, node2, type, R, L, U, Nobr] for mutual inductances

* node1: First node number of the branch.
* The node number must be positive or zero. Decimal node numbers are allowed.
* node2: Second node number of the branch. The node number must be positive or zero. Decimal node numbers are allowed.
* type: Integer indicating the type of connection of RLC elements, or, if negative, the transmission line length:type = 0: Series RLC elementtype = 1: Parallel RLC elementtype = 2: Transformer windingtype = 3: Coupled (mutual) winding If typeis negative, the transmission line is modeled by a PI section of length |type|. See details below.

For a **mutual inductor or a transformer having N windings**, N+1 consecutive lines must be specified in RLCmatrix:

1. N lines with type = 2or type = 3; (one line per winding). Each line specifies R/L/U or R/Xl/Xc where \[R/L, R/Xl = winding resistance and leakage reactance for a transformers or winding resistance and self reactance for mutually coupled windings. Uis the nominal voltage of transformer winding (specify 0 if type = 3).
2. One extra line with type = 1 for the magnetizing branch of a transformer (parallel Rm/Lm or Rm/Xm) or one line with type = 0 for a mutual impedance (series Rm/Lm or Rm/Xm).

For a **transformer magnetizing branch or a mutual impedance**, the first node number is an internal node located behind the leakage reactance of the first winding. The second node number must be the same as the second node number of the first winding.

To model a **saturable transformer**, you must use a nonlinear inductance instead of the linear inductance simulating the reactive losses. Set the Lm/Xmvalue to 0 (no linear inductance) and use the [Saturable Transformer](https://www.opal-rt.com/docs/smart-power-simulation-software-sps/blocks-functions-tools/power-transmission-and-distribution/transformers/saturable-transformer.md) block, set with proper flux-current characteristics.

This block can be found in Power Grid Elements library. It must be connected to the linear part of the system (State-Space block or S-function) between a voltage output (voltage across the magnetizing branch) and a current input (current source injected into the transformer internal node).

If type is negative, its absolute value specifies the length (km) of a transmission line simulated by a PI section. For a transmission line, the R/L/C or R/Xl/Xc values must be specified in Ω/km, mH/km, and µF/km, or in Ω/km.

<table data-header-hidden><thead><tr><th width="250.22222900390625"></th><th></th></tr></thead><tbody><tr><td><strong>Parameter</strong></td><td><strong>Description</strong></td></tr><tr><td>R</td><td>Branch resistance (Ω)</td></tr><tr><td>Xl</td><td>Branch inductive reactance (Ω at freq_sys) or transformer winding leakage reactance (Ω at freq_sys)</td></tr><tr><td>L</td><td>Branch inductance (mH)</td></tr><tr><td>Xc</td><td>Branch capacitive reactance (Ω at freq_sys). The negative sign of Xc is optional.</td></tr><tr><td>C</td><td>Capacitance (µF)</td></tr><tr><td>U</td><td><p>Nominal voltage of transformer winding. The same units (volts or kV) must be used for each winding.</p><p>For a mutual inductance (type=3), this value must be set to zero.</p></td></tr></tbody></table>

{% hint style="info" %} Zero value for R, L or Xl, C or Xc in a series or parallel branch indicates that the corresponding element does not exist. {% endhint %}

The following restrictions apply for transformer winding R-L values. Null values are not allowed for secondary impedances if some transformer secondaries form loops (as in a three-phase delta connection).

Specify a very low value for R or L or both (e.g., 1e-6 pu based on rated voltage and power) to simulate a quasi-ideal transformer. The resistive and inductive parts of the magnetizing branch can be set to infinite (no losses; specify Xm = Rm = inf).

### Format of the source input matrix

Three formats are allowed:

* Five columns: All sources are generating the same frequency specified by freq\_sys.
* Six columns: The frequency of each source is specified in column 6.
* Seven columns: The seventh column is used to specify the type of nonlinear element modeled by the current source.

Each line of the source matrix must be specified according to the following format:

\[ node1, node2, type, amp, phase, freq, model ]

* node1, node2: Node numbers corresponding to the source terminals. These are the polarity conventions:
  * Voltage source: node1 is the positive terminal.
  * Current source: Positive current flowing from node1 to node2 inside the source.
* type:Integer indicating the type of source: 0 for voltage source, 1 for current source.
* amp:Amplitude of the AC or DC voltage or current (V or A).
* phase: Phase of the AC voltage or current (degree).
* freq: Frequency (Hz) of the generated voltage or current. Default value is 60 Hz. For a DC voltage or current source, specify phase= 0 and freq= 0. amp can be set to a negative value. The generated signals areamp \* sin(2π\*freq\*t + phase)for AC, amp for DC.
* model:Integer specifying the type of nonlinear element modeled by the current source (saturable inductance, thyristor, switch,...). Used by power\_analyze only.

#### Order in which sources must be specified

The commands that compute the state-space representation of a system expect the sources in a certain order. You must respect this order in order to obtain correct results. You must be particularly careful if the system contains any switches. This is the proper ordering of sources:

1. The currents from all switches that have a null inductance (Lon = 0), if any.
2. The currents from all nonlinear models that have a finite inductance (switches with Lon > 0, the magnetizing inductance in saturable transformers, etc.), if any.
3. All other voltage and current sources in any order, if any. Refer to the Example section below for an example illustrating proper ordering of sources for a system containing nonlinear elements.

### Format of the switches input matrix

Switches are nonlinear elements simulating mechanical or electronic devices such as circuit breakers, diodes, or thyristors. Like other nonlinear elements, they are simulated by current sources driven by the voltage appearing across their terminals. Therefore, they cannot have a null impedance. They are simulated as ideal switches in series with a series R-L circuit.

Various models of switches (circuit breaker, ideal switch, and power electronic devices) are available in the SPS library. They must be interconnected to the linear part of the system through appropriate voltage outputs and current inputs.

The switch parameters must be specified in a line of the switches matrix in seven different columns, according to the following format.

\[ node1, node2, status, R, L/Xl, no\_I , no\_U ]

<table data-header-hidden><thead><tr><th width="250.22216796875"></th><th></th></tr></thead><tbody><tr><td><strong>Parameter</strong></td><td><strong>Description</strong></td></tr><tr><td>node1, node2</td><td>Node numbers corresponding to the switch terminals</td></tr><tr><td>status</td><td>Code indicating the initial status of the switch at t = 0: 0 = open; 1 = closed</td></tr><tr><td>R</td><td>Resistance of the switch when closed (Ω)</td></tr><tr><td>L/Xl</td><td>Inductance of the switch when closed (mH) or inductive reactance (Ω at freq_sys)</td></tr></tbody></table>

{% hint style="info" %} For these last two fields, you must use the same units as those specified for the RLC matrix. Either field can be set to 0, but not both. {% endhint %}

The next two fields specify the current input number and the voltage output number to be used for interconnecting the switch model to the State-Space block. The output number corresponding to the voltage across a particular switch must be the same as the input number corresponding to the current from the same switch (see Example section below):

* no\_I: Current input number coming from the output of the switch model
* no\_U: Voltage output number driving the input of the switch model

### Format of the Line\_Dist matrix

The distributed parameter line model contains two parts:

1. A linear part containing current sources and resistances that are connected at the line sending and receiving buses together with the linear circuit.
2. A nonlinear part available in the [Distributed Parameters Line](https://www.opal-rt.com/docs/smart-power-simulation-software-sps/blocks-functions-tools/power-transmission-and-distribution/lines/distributed-parameters-line.md) block of the Power Grid Elements library.
3. This block performs the phase-to-mode transformations of voltage and currents and simulates the transmission delays for each mode. The *distributed\_param\_line* block must be connected to appropriate voltage outputs and current inputs of the linear part of the system.
4. The line parameters have to be specified in the line\_dist matrix and also in the Distributed Parameters Line block.

Each row of the line\_dist matrix is used to specify a distributed parameter transmission line. The number of columns of line\_dist depends on the number of phases of the transmission line.

For an nphase line, the first (4 + 3 \* nphase + nphase^2) columns are used. For example, for a three-phase line, 22 columns are used.

\[nphase, no\_I, no\_U, length, L/Xl, Zc, Rm, speed, Ti]

<table data-header-hidden><thead><tr><th width="249.7777099609375"></th><th></th></tr></thead><tbody><tr><td><strong>Parameter</strong></td><td><strong>Description</strong></td></tr><tr><td>nphase</td><td>Number of phases of the transmission line</td></tr><tr><td>no_I</td><td>Input number in the source matrix corresponding to the first current source Is_1 of the line model. Each line model uses 2*nphase current sources specified in the source matrix as follows: Is_1, Is_2,…, Is_nphase for the sending end followed by Ir_1, Ir_2,…, Ir_nphase for the receiving end.</td></tr><tr><td>no_U</td><td>Output number of the state-space corresponding to the first voltage output Vs_1 feeding the line model. Each line model uses 2*nphase voltage outputs in the source matrix as follows: Vs_1, Vs_2,…, Vs_nphase for the sending end followed by Vr_1, Vr_2,…, Vr_nphase for the receiving end.</td></tr><tr><td>length</td><td>Length of the line (km)</td></tr><tr><td>Zc</td><td>Vector of the nphase modal characteristic impedances (Ω)</td></tr><tr><td>Rm</td><td>Vector of the nphase modal series resistances (Ω/km)</td></tr><tr><td>speed</td><td>Vector of the nphase modal propagation speeds (km/s)</td></tr><tr><td>Ti</td><td>Transformation matrix from mode to phase currents such that Iphase = Ti * Imod. The nphase * nphase matrix must be given in vector format, [col_1, col_2,… col_nphase].</td></tr></tbody></table>

### Format of the Yout matrix

The desired outputs are specified by the matrix yout. Each line of the yout matrix must be an algebraic expression containing a linear combination of states and state derivatives, specified according to the following format.

<table data-header-hidden><thead><tr><th width="249.55560302734375"></th><th></th></tr></thead><tbody><tr><td><strong>Parameter</strong></td><td><strong>Description</strong></td></tr><tr><td>Uc_bn</td><td>Capacitor voltage of branch <em>n</em></td></tr><tr><td>Il_bn</td><td>Inductor current of branch <em>n</em></td></tr><tr><td>dUc_bn</td><td>Derivative of Uc_bn or Il_bn</td></tr><tr><td>Un, In</td><td>Source voltage or current specified by line <em>n</em> of the source matrix</td></tr><tr><td>U_nx1_x2</td><td>Voltage between nodes x1 and x2 = Ux1 −Ux2</td></tr><tr><td>I_bn</td><td>Current in branch <em>n</em> flowing from node1 to node2 (See format of RLC matrix). For a parallel RLC branch, I_bn corresponds to the total current IR + IL + IC.</td></tr><tr><td>I_bn_nx</td><td>Current flowing into node x of a PI transmission line specified by line <em>n</em> of the RLC matrix. This current includes the series inductive branch current and the capacitive shunt current.</td></tr></tbody></table>

Each output expression is built from voltage and current variable names defined above, their derivatives, constants, other variable names, parentheses and operators (+ − \* / ^), in order to form a valid MATLAB expression. For example

yout = char(\['R1*I\_b1+Uc\_b3-L2*dIl\_b2','U\_n10\_20','I2+3\*I\_b5']);

If variable names are used (R1 and L2 in the above example), their names and values must be specified by the two input arguments vary\_name and vary\_val.

#### Sign conventions for voltages and currents

<table data-header-hidden><thead><tr><th width="249.55560302734375"></th><th></th></tr></thead><tbody><tr><td><strong>Parameter</strong></td><td><strong>Sign Convention</strong></td></tr><tr><td>I_bn, Il_bn, In</td><td>Branch current, inductor current of branch <em>n</em>, or current of source #n is oriented from <code>node1</code> to <code>node2</code></td></tr><tr><td>I_bn_nx</td><td>Current at one end (node x) of a PI transmission line. If x = node1, the current is entering the line. If x = node2, the current is leaving the line.</td></tr><tr><td>Uc_bn, Un</td><td>Voltage across capacitor or source voltage (Unode1 − Unode2)</td></tr><tr><td>U_nx1_x2</td><td>Voltage between nodes x1 and x2 = Ux1 − Ux2. Voltage of node x1 with respect to node x2.</td></tr></tbody></table>

#### Order in which outputs must be specified

The commands that compute the state-space representation of a system expect the outputs to be in a certain order. You must respect this order in order to obtain correct results. You must be particularly careful if the system contains any switches. The following list gives the proper ordering of outputs:

1. The voltages across all switches that have a null inductance (Lon = 0), if any
2. The currents of all switches that have a null inductance (Lon = 0), if any, in the same order as the voltages above
3. The voltages across all nonlinear models that have a finite inductance (switches with Lon > 0, the magnetizing inductance in saturable transformers, etc.)
4. All other voltage and current measurements that you request, in any order

Refer to the Example section below for an example illustrating proper ordering of outputs for a system containing nonlinear elements.

## Examples

The following circuit consists of two sources (one voltage source and one current source), two series RLC branches (R1-L1 and C6), two parallel RLC branches (R5-C5 and L7-C7), one saturable transformer, and two switches (Sw1 and Sw2). Sw1 is initially closed whereas Sw2 is initially open. Three measurement outputs are specified (I1, V2, and V3).

This circuit has seven nodes numbered 0, 1, 2, 2.1, 10, 11, and 12. Node 0 is used for the ground. Node 2.1 is the internal node of the transformer where the magnetization branch is connected.

<figure><img src="https://799605156-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdlNg8IbOdahCpFCyzxSN%2Fuploads%2Fgit-blob-04cc80f71a557cff1a27e92a7ccab57e72105510%2Fpower_statespace_function_example.png?alt=media" alt=""><figcaption></figcaption></figure>

### Linear State-Space

You can use the power\_statespace command to find the state-space model of the linear part of the circuit. The nonlinear elements Sw1, Sw2, and Lsat must be modeled separately by means of current sources driven by the voltages appearing across their terminals.

Therefore you must provide three additional current sources and three additional voltage outputs for interfacing the nonlinear elements to the linear circuit.

You can find the state-space model of the circuit by entering the following commands in a MATLAB script file. The example is available in the power\_circ2ss.m file. Notice that an output text file named power\_circ2ss.net containing information on the system is requested in the call to power\_statespace.

{% code lineNumbers="true" %}

```matlab
unit='OMU'; % Units = ohms, mH, and uF
rlc=[
%N1	N2	type	R	L	C(uF)/U(V)
1	2	0	0.1	1	0	%R1 L1
2	0	2	0.05	1.5	100	%transfo Wind.#1
10	0	2	0.20	0	200	%transfo Wind.#2
2.1	0	1	1000	0	0	%transfo mag. branch
11	0	1	200	0	1	%R5 C5
11	12	0	0	0	1e-3	%C6
12	0	1	0	500	2	%L7 C7
];
source=[
%N1	N2	type	U/I	phase	freq
10	11	1	0	0	0	%Sw1
11	12	1	0	0	0	%Sw2
2.1	0	1	0	0	0	%Saturation
1	0	0	100	0	60	%Voltage source
0	10	1	2	-30	180	%Current source
]; 
switches=[
%N1	N2	status	R(ohm)	L(mH)	I#	U#  #
10	11	1	0.01	0	1	1	%Sw1
11	12	0	0.1	0	2	2	%Sw2
];
%outputs
%
% Both switches have Lon=0, so their voltages must be the first outputs,
% immediately followed by their currents (in the same order as the voltages).
% The voltage across all nonlinear models that don't have L=0 follow
% (in this case the saturable transformer's magnetizing inductor).
% The measurements that you request follow, in any order.
%
y_u1='U_n10_11';			%U_Sw1= Voltage across Sw1
y_u2='U_n11_12';			%U_Sw2= Voltage across Sw2
y_i3='I1';			%I1= Switch current Sw1
y_i4='I2';			%I2= Switch current Sw2
y_u5='U_n2.1_0';			%U_sat= Voltage across saturable reactor 
y_i6='I_b1';			%I1 measurement
y_u7='U_n11_0';			%V2 measurement
y_u8='U_n12_0';			%V3 measurement
yout=char(y_u1,y_u2,y_i3,y_i4,y_u5,y_i6,y_u7,y_u8);								% outputs
y_type=[0,0,1,1,0,1,0,0];				%output types; 0=voltage 1=current
% Open file that contains power_statespace output information
fid=fopen('power_circ2ss.net','w');
[A,B,C,D,states,x0,x0sw,rlsw,u,x,y,freq,Asw,Bsw,Csw,Dsw,Hlin]=  
power_statespace(rlc,switches,source,[],yout,y_type,unit,[],[], 
[],0,fid);
```

### Command Line Messages

While power\_statespace is executing, the following messages are displayed.

{% code lineNumbers="true" %}

```matlab
Computing state space representation of linear electrical circuit 
(V2.0)...
(4 states ; 5 inputs ; 7 outputs)
Oscillatory modes and damping factors:
F=159.115Hz zeta=4.80381e-08
Steady state outputs @ F=0 Hz :
y_u1= 0Volts
y_u2= 0Volts
y_i3= 0Amperes
y_i4= 0Amperes
y_u5= 0Volts
y_i6= 0Amperes
y_u7= 0Volts
y_u8= 0Volts
Steady state outputs @ F=60 Hz :
y_u1 = 0.009999 Volts < 3.168 deg.
y_u2 = 199.4 Volts < -1.148 deg.
y_i3 = 0.9999 Amperes < 3.168 deg.
y_i4 = 0 Amperes < 0 deg.
y_u5 = 99.81 Volts < -1.144 deg.
y_i6 = 2.099 Amperes < 2.963 deg.
y_u7 = 199.4 Volts < -1.148 deg.
y_u8 = 0.01652 Volts < 178.9 deg.
Steady state outputs @ F=180 Hz :
y_u1 = 0.00117 Volts < 65.23 deg.
y_u2 = 22.78 Volts < 52.47 deg.
y_i3 = 0.117 Amperes < 65.23 deg.
y_i4 = 0 Amperes < 0 deg.
y_u5 = 11.4 Volts < 53.48 deg.
y_i6 = 4.027 Amperes < 146.5 deg.
y_u7 = 22.83 Volts < 52.47 deg.
y_u8 = 0.0522 Volts < 52.47 deg.
```

{% endcode %}

### State-Space Output

The names of the state variables are returned in the states matrix.

{% code lineNumbers="true" %}

```matlab
states
states =
Il_b2_n2_2.1
Uc_b5_n11_0
Uc_b6_n11_12
Il_b7_n12_0
Il_b1_n1_2*
Uc_b7_n12_0*
```

{% endcode %}

Although this circuit contains a total of six inductors and capacitors, there are only four state variables. The names of the state variables are given by the first four lines of the states matrix. The last two lines are followed by an asterisk indicating that these two variables are a linear combination of the state variables. The dependencies can be viewed in the output file power\_circ2ss.net.

The following capacitor voltages are dependent:

{% code lineNumbers="true" %}

```matlab
The following capacitor voltages are dependent:
Uc_b7_n12_0 =  + Uc_b5_n11_0  - Uc_b6_n11_12
The following inductor currents are dependent:
Il_b1_n1_2 =  + Il_b2_n2_0
```

{% endcode %}

The A,B,C,D matrices contain the state-space model of the circuit without nonlinear elements (all switches open). The x0 vector contains the initial state values considering the switch Sw1 closed. The Asw, Bsw, Csw, and Dsw matrices contain the state-space model of the circuit considering the closed switch Sw1. The x0sw vector contains the initial current in the closed switch.

{% code lineNumbers="true" %}

```
A
A = 
	-4.0006e+05	0	0	0 
	         0	-4995	0	-499.25 
	         0	-4992.5	0	4.9925e+05 
	         0	2	-2	0
Asw
Asw = 
	  -80.999	-199.99	0	0 
	4.9947e+05	-5244.7	0	 -499.25 
	4.9922e+05	-5242.1	0	4.9925e+05 
	         0	2	-2	0
```

{% endcode %}

The system source frequencies are returned in the freq vector.

{% code lineNumbers="true" %}

```matlab
freq
freq =
	 0    60   180
```

{% endcode %}

The corresponding steady-state complex outputs are returned in the (6-by-3) y matrix where each column corresponds to a different source frequency.

For example, you can obtain the magnitude of the six voltage and current outputs at 60 Hz as follows.

{% code lineNumbers="true" %}

```matlab
abs(y(:,2))
ans =
	  0.0099987
199.42
	  0.99987
	  0
	  99.808
	  2.0993
	  199.41
	  0.016519
```

{% endcode %}

The initial values of the four state variables are returned in the x0 vector. You must use this vector in the State-Space block to start the simulation in steady state.

{% code lineNumbers="true" %}

```matlab
x0
x0 =
	2.3302
	14.111
	14.07
	3.1391e-05
```

{% endcode %}

The initial values of switch currents are returned in x0sw. To start the simulation in steady state, you must use these values as initial currents for the nonlinear model simulating the switches.

{% code lineNumbers="true" %}

```matlab
x0sw
x0sw =
0.16155
	  0
```

{% endcode %}

The Simulink diagram of the circuit is available in the power\_circ2ss\_slk model. If no resistive switches had been used, the linear part of the circuit could have been simulated with the State-Space block of the Simulink/Continuous library. However, as resistive switches are used, the sfun\_psbcontc S-function is used instead of the State-Space block. This S-function reevaluates the state-space matrices during simulation when the circuit topology is changing (after a switch is opened or closed). Appropriate inputs and outputs are used to connect the switch and saturable reactance models to the linear system. Notice that the status of each switch is fed back from the breaker to the S-function, after the inputs mentioned earlier. You can find the Breaker and Saturable Transformer blocks in the Power Grid Elements library containing all the nonlinear continuous models used by SPS software. As the breaker model is vectorized, a single block is used to simulate the two switches Sw1 and Sw2.

If you use the SPS library to build your circuit, the same Simulink system is generated automatically by the power\_analyze command. The SPS version of this system is also available in the power\_circ2ss\_sps model.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.opal-rt.com/docs/smart-power-simulation-software-sps/blocks-functions-tools/simulation-and-analysis/power_statespace.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
