# 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 

## 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.


```python
% 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')
```


```python

```

e38b8cbcfa90eab05d6254ae008e1d1cb57437f6
ipynb
Jupyter Notebook
notebooks/simulation/Linear_First_Order_System.ipynb
gschivley/ND-Pyomo-Cookbook
7bddccd14eb3044e9d3e7c46999e1c9c29541e16
Apache-2.0
notebooks/simulation/Linear_First_Order_System.ipynb
gschivley/ND-Pyomo-Cookbook
7bddccd14eb3044e9d3e7c46999e1c9c29541e16
Apache-2.0
notebooks/simulation/Linear_First_Order_System.ipynb
gschivley/ND-Pyomo-Cookbook
7bddccd14eb3044e9d3e7c46999e1c9c29541e16
Apache-2.0
2018-10-14T16:40:40.000Z
2021-07-20T12:02:29.000Z
Qwen/Qwen-72B
1. YES
2. YES