# Linear First Order System

This notebook demonstrates simulation of a linear first-order system in Pyomo using two distinct approaches. The first uses the `Simulator` class from Pyomo which can employ the 


```
%%capture
!pip install -q pyomo
```

## First-Order Differential Equation with Initial Condition

The following cell implements a solution to a first-order linear model in the form

\begin{align}
\tau\frac{dy}{dt} + y & = K u(t) \\
\end{align}

where $\tau$ and $K$ are model parameters, and $u(t)$ is an external process input.


```
% matplotlib inline
from pyomo.environ import *
from pyomo.dae import *
import matplotlib.pyplot as plt

tf = 10
tau = 1
K = 5

# define u(t)
u = lambda t: 1

# create a model object
model = ConcreteModel()

# define the independent variable
model.t = ContinuousSet(bounds=(0, tf))

# define the dependent variables
model.y = Var(model.t)
model.dydt = DerivativeVar(model.y)

# fix the initial value of y
model.y[0].fix(0)

# define the differential equation as a constraint
model.ode = Constraint(model.t, rule=lambda model, t: tau*model.dydt[t] + model.y[t] == K*u(t))

# transform dae model to discrete optimization problem
#TransformationFactory('dae.finite_difference').apply_to(model, nfe=50, method='BACKWARD')

# solve the model
#SolverFactory('ipopt').solve(model).write()

tsim, profiles = Simulator(model, package='scipy').simulate(numpoints=100)

plt.plot(tsim, profiles)

# access elements of a ContinuousSet object
t = [t for t in model.t]

# access elements of a Var object
y = [model.y[t]() for t in model.y]

plt.plot(t,y)
plt.xlabel('time / sec')
plt.ylabel('response')
plt.title('Response of a linear first-order ODE')
```


```

```

85ff6a4cdbc7b40e8c9a79aff932b4a1dcd14857
ipynb
Jupyter Notebook
notebooks/simulation/Linear_First_Order_System.ipynb
edgBR/ND-Pyomo-Cookbook
0fb121f7e572e088a83c22c166c159ff55c2efb4
Apache-2.0
notebooks/simulation/Linear_First_Order_System.ipynb
edgBR/ND-Pyomo-Cookbook
0fb121f7e572e088a83c22c166c159ff55c2efb4
Apache-2.0
notebooks/simulation/Linear_First_Order_System.ipynb
edgBR/ND-Pyomo-Cookbook
0fb121f7e572e088a83c22c166c159ff55c2efb4
Apache-2.0
2020-09-03T18:53:07.000Z
2020-09-03T18:53:07.000Z
Qwen/Qwen-72B
1. YES
2. YES