ARX Time Series Model

Apps.ARXTimeSeries History

Hide minor edits - Show changes to output

Added lines 30-111:

(:toggle hide gekko_siso button show="Python GEKKO SISO ARX":)
(:div id=gekko_siso:)

%width=550px%Attach:sysid_siso_tclab_id.png

(:source lang=python:)
# see https://apmonitor.com/wiki/index.php/Apps/ARXTimeSeries
from gekko import GEKKO
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# load data and parse into columns
url = 'http://apmonitor.com/do/uploads/Main/tclab_dyn_data2.txt'
data = pd.read_csv(url)
t = data['Time']
u = data['H1']
y = data['T1']

m = GEKKO()

# system identification
na = 2 # output coefficients
nb = 2 # input coefficients
yp,p,K = m.sysid(t,u,y,na,nb,pred='meas')

plt.figure()
plt.subplot(2,1,1)
plt.plot(t,u,label=r'$Heater_1$')
plt.legend()
plt.ylabel('Heater')
plt.subplot(2,1,2)
plt.plot(t,y)
plt.plot(t,yp)
plt.legend([r'$T_{meas}$',r'$T_{pred}$'])
plt.ylabel('Temperature (°C)')
plt.xlabel('Time (sec)')
plt.show()
(:sourceend:)
(:divend:)

(:toggle hide gekko_mimo button show="Python GEKKO MIMO ARX":)
(:div id=gekko_mimo:)

%width=550px%Attach:sysid_mimo_tclab_id.png

(:source lang=python:)
from gekko import GEKKO
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# load data and parse into columns
url = 'http://apmonitor.com/do/uploads/Main/tclab_dyn_data2.txt'
data = pd.read_csv(url)
t = data['Time']
u = data[['H1','H2']]
y = data[['T1','T2']]

m = GEKKO()

# system identification
na = 2 # output coefficients
nb = 2 # input coefficients
yp,p,K = m.sysid(t,u,y,na,nb,pred='meas')

plt.figure()
plt.subplot(2,1,1)
plt.plot(t,u,label=r'$Heater_1$')
plt.legend([r'$Heater_1$',r'$Heater_2$'])
plt.ylabel('Heaters')
plt.subplot(2,1,2)
plt.plot(t,y)
plt.plot(t,yp,'--')
plt.legend([r'$T1_{meas}$',r'$T2_{meas}$',\
            r'$T1_{pred}$',r'$T2_{pred}$'])
plt.ylabel('Temperature (°C)')
plt.xlabel('Time (sec)')
plt.show()
(:sourceend:)
(:divend:)
June 12, 2019, at 10:17 PM by 10.37.73.127 -
Added lines 153-154:
* [[Apps/TimeDelay | Time Delay]]
June 12, 2019, at 09:15 PM by 10.37.73.127 -
Changed line 13 from:
 GEKKO Usage: y,u = m.arx(A,B,na,nb,ny,nu)
to:
 GEKKO Usage: y,u = m.arx(p,y=[],u=[])
April 27, 2019, at 07:50 PM by 64.134.232.75 -
Changed line 47 from:
# A (ny x na)
to:
# A (na x ny)
Changed line 50 from:
# B (ny x nu x nb)
to:
# B (ny x (nb x nu))
April 27, 2019, at 07:49 PM by 64.134.232.75 -
Changed lines 48-49 from:
A = np.array([[0.36788,0.223],\
              [0.36788,-0.136]])
to:
A = np.array([[0.36788,0.36788],\
              [0.223,-0.136]])
Changed lines 51-53 from:
B = np.array([[0.63212,0.31606],\
             [0.18964,1.26420]])
to:
B1 = np.array([0.63212,0.18964]).T
B2 = np.array([0.31606,1.26420]).T
B = np
.array([[B1],[B2]])

C = np.array([0,0])

# create parameter dictionary
# parameter dictionary p['a'], p['b'], p['c']
# a (coefficients for a polynomial, na x ny)
# b (coefficients for b polynomial, ny x (nb x nu))
# c (coefficients for output bias, ny)
p = {'a':A,'b':B,'c':C}

Changed line 68 from:
y,u = m.arx(A,B,na,nb,ny,nu)
to:
y,u = m.arx(p)
April 27, 2019, at 07:36 PM by 64.134.232.75 -
Added line 142:
* [[http://apmonitor.com/do/index.php/Main/ModelIdentification|MIMO Model Identification]]
Changed line 138 from:
Also see
to:
Also see:
Changed lines 138-141 from:
Also see [[Apps/LinearStateSpace|Continuous State Space]] and [[Apps/DiscreteStateSpace|Discrete State Space]]
to:
Also see

*
[[Apps/LinearStateSpace|Continuous State Space]]
* [[Apps/DiscreteStateSpace|Discrete State Space]]
Changed line 13 from:
 GEKKO Usage: y,u = m.arx(a,b,na,nb,ny,nu)
to:
 GEKKO Usage: y,u = m.arx(A,B,na,nb,ny,nu)
Changed line 8 from:
 Data: A, B, C, and D matrices
to:
 Data: A, B matrices
Added lines 33-34:
!!!! Example Model Predictive Control in [[https://gekko.readthedocs.io/en/latest/|GEKKO]]
Deleted lines 36-77:
!!!! Example Model in APMonitor

 Objects
  sys = arx
 End Objects
 
 Parameters
  mv1
  mv2
 End Parameters
 
 Variables
  cv1 = 0
  cv2 = 0
 End Variables
 
 Connections
  mv1 = sys.u[1]
  mv2 = sys.u[2]
  cv1 = sys.y[1]
  cv2 = sys.y[2]
 End Connections
 
 File sys.txt
  2 ! inputs
  2 ! outputs
  1 ! number of input terms
  2 ! number of output terms
 End File
 
 File sys.alpha.txt
  0.36788, 0.36788
  0.223, -0.136
 End File
 
 File sys.beta.txt
  0.63212, 0.18964
  0.31606, 1.2642
 End File

!!!! Example Model Predictive Control in [[https://gekko.readthedocs.io/en/latest/|GEKKO]]

Added lines 97-136:

!!!! Example Model in APMonitor

 Objects
  sys = arx
 End Objects
 
 Parameters
  mv1
  mv2
 End Parameters
 
 Variables
  cv1 = 0
  cv2 = 0
 End Variables
 
 Connections
  mv1 = sys.u[1]
  mv2 = sys.u[2]
  cv1 = sys.y[1]
  cv2 = sys.y[2]
 End Connections
 
 File sys.txt
  2 ! inputs
  2 ! outputs
  1 ! number of input terms
  2 ! number of output terms
 End File
 
 File sys.alpha.txt
  0.36788, 0.36788
  0.223, -0.136
 End File
 
 File sys.beta.txt
  0.63212, 0.18964
  0.31606, 1.2642
 End File
Changed lines 19-20 from:
With na=3, nb=2, nu=1, and ny=1 the time series model is:
to:
With ''n'_a_'''=3, ''n'_b_'''=2, ''n'_u_'''=1, and ''n'_y_'''=1 the time series model is:
Changed line 23 from:
There may also be multiple inputs and multiple outputs such as when na=1, nb=1, ny=2, and nu=2.
to:
There may also be multiple inputs and multiple outputs such as when ''n'_a_'''=1, ''n'_b_'''=1, ''n'_u_'''=2, and ''n'_y_'''=2.
Changed line 17 from:
{$y_{k+1} = \sum_{i=1}^n_a a_i \, y_{k-i+1} + \sum_{i=1}^n_b b_i \, u_{k-i+1}$}
to:
{$y_{k+1} = \sum_{i=1}^{n_a} a_i y_{k-i+1} + \sum_{i=1}^{n_b} b_i u_{k-i+1}$}
Added lines 1-138:
(:title ARX Time Series Model:)
(:keywords linear, ARX, Output Error, dynamic, multiple input, multiple output, MIMO, model predictive control:)
(:description Autoregressive exogenous models are a linear representation of a dynamic system in discrete form. Examples show how to use a time series model in APMonitor, Python GEKKO, and Python Numpy.:)

%width=50px%Attach:apm.png [[Main/Objects|APMonitor Objects]]

 Type: Object
 Data: A, B, C, and D matrices
 Inputs: Input (u)
 Outputs: Output (y)
 Description: ARX Time Series Model
 APMonitor Usage: sys = arx
 GEKKO Usage: y,u = m.arx(a,b,na,nb,ny,nu)

ARX time series models are a linear representation of a dynamic system in discrete time. Putting a model into ARX form is the basis for many methods in process dynamics and control analysis. Below is the time series model with a single input and single output with ''k'' as an index that refers to the time step.

{$y_{k+1} = \sum_{i=1}^n_a a_i \, y_{k-i+1} + \sum_{i=1}^n_b b_i \, u_{k-i+1}$}

With na=3, nb=2, nu=1, and ny=1 the time series model is:

{$y_{k+1} = a_1 \, y_k + a_2 \, y_{k-1} + a_3 \, y_{k-2} + b_1 \, u_k + b_2 \, u_{k-1}$}

There may also be multiple inputs and multiple outputs such as when na=1, nb=1, ny=2, and nu=2.

{$y1_{k+1} = a_{1,1} \, y1_k + b_{1,1} \, u1_k + b_{1,2} \, u2_k$}

{$y2_{k+1} = a_{1,2} \, y2_k + b_{2,1} \, u1_k + b_{2,2} \, u2_k$}

Time series models are used for identification and advanced control. It has been in use in the process industries such as chemical plants and oil refineries since the 1980s. Model predictive controllers rely on dynamic models of the process, most often linear empirical models obtained by system identification.

These models are typically in the finite impulse response, time series, or linear state space form.  Once in %blue%A%red%P%black%Monitor form, nonlinear elements can be added to avoid multiple model switching, gain scheduling, or other ad hoc measures commonly employed because of linear MPC restrictions.

%width=550px%Attach:arx_time_series.png

!!!! Example Model in APMonitor

 Objects
  sys = arx
 End Objects
 
 Parameters
  mv1
  mv2
 End Parameters
 
 Variables
  cv1 = 0
  cv2 = 0
 End Variables
 
 Connections
  mv1 = sys.u[1]
  mv2 = sys.u[2]
  cv1 = sys.y[1]
  cv2 = sys.y[2]
 End Connections
 
 File sys.txt
  2 ! inputs
  2 ! outputs
  1 ! number of input terms
  2 ! number of output terms
 End File
 
 File sys.alpha.txt
  0.36788, 0.36788
  0.223, -0.136
 End File
 
 File sys.beta.txt
  0.63212, 0.18964
  0.31606, 1.2642
 End File

!!!! Example Model Predictive Control in [[https://gekko.readthedocs.io/en/latest/|GEKKO]]

(:source lang=python:)
import numpy as np
from gekko import GEKKO
import matplotlib.pyplot as plt

na = 2 # Number of A coefficients
nb = 1 # Number of B coefficients
ny = 2 # Number of outputs
nu = 2 # Number of inputs

# A (ny x na)
A = np.array([[0.36788,0.223],\
              [0.36788,-0.136]])
# B (ny x nu x nb)
B = np.array([[0.63212,0.31606],\
              [0.18964,1.26420]])

# Create GEKKO model
m = GEKKO(remote=False)

# Build GEKKO ARX model
y,u = m.arx(A,B,na,nb,ny,nu)

# load inputs
tf = 20 # final time
u1 = np.zeros(tf+1)
u2 = u1.copy()
u1[5:] = 3.0
u2[10:] = 5.0
u[0].value = u1
u[1].value = u2

# customize names
mv1 = u[0]
mv2 = u[1]
cv1 = y[0]
cv2 = y[1]

# options
m.time = np.linspace(0,tf,tf+1)
m.options.imode = 4
m.options.nodes = 2

# simulate
m.solve()

plt.figure(1)
plt.subplot(2,1,1)
plt.plot(m.time,mv1.value,'r-',label=r'$MV_1$')
plt.plot(m.time,mv2.value,'b--',label=r'$MV_2$')
plt.ylabel('MV')
plt.legend(loc='best')
plt.subplot(2,1,2)
plt.plot(m.time,cv1.value,'r:',label=r'$CV_1$')
plt.plot(m.time,cv2.value,'b.-',label=r'$CV_2$')
plt.ylabel('CV')
plt.xlabel('Time (sec)')
plt.legend(loc='best')
plt.show()
(:sourceend:)

Also see [[Apps/LinearStateSpace|Continuous State Space]] and [[Apps/DiscreteStateSpace|Discrete State Space]]