<jupyter_start><jupyter_text>VRFT without measurement noise (no instrumental variables)<jupyter_code># Copyright (c) [2021] <NAME> [<EMAIL>]. All rights reserved.
# This file is part of PythonVRFT.
# PythonVRFT is free software: you can redistribute it and/or modify
# it under the terms of the MIT License. You should have received a copy of
# the MIT License along with PythonVRFT.
# If not, see <https://opensource.org/licenses/MIT>.
#
# Code author: [<NAME> - <EMAIL>]
# Last update: 10th January 2021, by <EMAIL>
#

# Example 1
# ------------
# In this example we see how to apply VRFT to a simple SISO model
# without any measurement noise.
# Input data is generated using a square signal
#<jupyter_output><empty_output><jupyter_text>Load libraries<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import scipy.signal as scipysig
from vrft import *<jupyter_output><empty_output><jupyter_text>System, Reference Model and Control law<jupyter_code># System
dt = 1e-2
num = [0.5]
den = [1, -0.9]
sys = ExtendedTF(num, den, dt=dt)

# Reference Model
refModel = ExtendedTF([0.6], [1, -0.4], dt=dt)

# Control law
control = [ExtendedTF([1], [1, -1], dt=dt),
        ExtendedTF([1, 0], [1, -1], dt=dt)]<jupyter_output><empty_output><jupyter_text>Generate signals<jupyter_code># Generate input siganl
t_start = 0
t_end = 10
t = np.arange(t_start, t_end, dt)
u = np.ones(len(t))
u[200:400] = np.zeros(200)
u[600:800] = np.zeros(200)

# Open loop experiment
t, y = scipysig.dlsim(sys, u, t)
y = y.flatten()

# Save data into an IDDATA Object with 0 initial condition
# Length of the initial condition depends on the reference model
data = iddata(y, u, dt, [0])<jupyter_output><empty_output><jupyter_text>VRFT<jupyter_code># VRFT Pre-filter
prefilter = refModel * (1 -  refModel)

# VRFT method
theta, r, loss, C = compute_vrft(data, refModel, control, prefilter)

#Obtained controller
print("Loss: {}\nTheta: {}\nController: {}".format(loss, theta, C))<jupyter_output>Loss: 4.4859502383167834e-30
Theta: [-1.08  1.2 ]
Controller: ExtendedTF(
array([ 1.2 , -1.08]),
array([ 1., -1.]),
dt: 0.01
)<jupyter_text>Verify performance<jupyter_code># Closed loop system
closed_loop = (C * sys).feedback()

t = t[:len(r)]
u = np.ones(len(t))

_, yr = scipysig.dlsim(refModel, u, t)
_, yc = scipysig.dlsim(closed_loop, u, t)
_, ys = scipysig.dlsim(sys, u, t)

yr = np.array(yr).flatten()
ys = np.array(ys).flatten()
yc = np.array(yc).flatten()
fig, ax = plt.subplots(4, sharex=True, figsize=(12,8), dpi= 100, facecolor='w', edgecolor='k')
ax[0].plot(t, yr,label='Reference System')
ax[0].plot(t, yc, label='CL System')
ax[0].set_title('Systems response')
ax[0].grid(True)
ax[1].plot(t, ys, label='OL System')
ax[1].set_title('OL Systems response')
ax[1].grid(True)
ax[2].plot(t, y[:len(r)])
ax[2].grid(True)
ax[2].set_title('Experiment data')
ax[3].plot(t, r)
ax[3].grid(True)
ax[3].set_title('Virtual Reference')

# Now add the legend with some customizations.
legend = ax[0].legend(loc='lower right', shadow=True)

# The frame is matplotlib.patches.Rectangle instance surrounding the legend.
frame = legend.get_frame()
frame.set_facecolor('0.90')

# Set the fontsize
for label in legend.get_texts():
    label.set_fontsize('large')

for label in legend.get_lines():
    label.set_linewidth(1.5)  # the legend line width
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15858375483487058, 'fraction_numerical': 0.03391847664385599, 'mean_word_length': 5.214417744916821, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '512367', 'license': 'MIT', 'n_tokens_mistral': 1348, 'n_tokens_neox': 1281, 'n_words': 346, 'path': 'examples/notebook_example_1.ipynb', 'repo_name': 'alexberndt/PythonVRFT', 'size': '3399'}