# **TODO:** Perform everything in discrete time as well. Check if disturbance-free convergece rate and robustness are different things there. In continuous time, I expect them to be the same and be high-gain control.
# %% [markdown]
# **TODO:** Run longer trainings on both convergence rate and gain bound, plot controller parameter values versus gradient steps, check if path followed is similar
# %% [markdown]
# **TODO:** Plot both gain bound and convergence rate versus gradient steps in all training examples. Use missing for gain bound if optimization fails. This is also intended to check if they are related.
# %% [markdown]
# **TODO:** Add checks using other methods for P and PID control cases
# %% [markdown]
# **TODO:** Introduce uncertainty as an unknown parameter in model estimation. As a simple example, take $x = 1/(s+\beta)$ and pretend that I only know $\beta \in [0.1, 0.3]$. Rewrite as $sx+\beta x = u$ and then $sx = -(\beta_{\mbox{nominal}} + \delta \beta ) x + u$ with the new signal $(\delta \beta \; x)^2 \le 0.1^2 x^2$. Do not use the uncertainty in $u$ that I had before.
# %% [markdown]
# **TODO:** Ha en gain bound från mätbrus till tillstånd. Gör detta för alla processer.
# If I can use flux with parameters, then that would be great.
# %% [markdown]
# # PID
# %% [markdown]
# PID controller of a high order system. Tore says that having a few zeros makes it a lot trickier, so I should have stable poles and som eright-half-plane zeros. Find a good gain bound using gradients.
#
# Placing poles at $-1$, $-1/2\pm i$, $1/2\pm i$ and zeros at $1$ and $2$
#
# Take small steps
# %% [markdown]
# ## Vanilla case
# %%
k_val=zeros(3)
G=1/(s-1)^2
C=k_val'*vcat(1,1/s,s)
sys=CS.feedback(G,C)
function CS_gain_bound(k_val)
G=1/(s-1)^2
C=k_val'*vcat(1,1/s,s)
sys=CS.feedback(G,C)
(g,p)=CS.bode(sys)
maximum(g)
end
function CS_conv_rate(k_val)
G=1/(s-1)^2
C=k_val'*vcat(1,1/s,s)
sys=CS.feedback(G,C)
-maximum(real.(CS.pole(sys)))
end
# %%
CS_conv_rate([1,.1,.1])
# %%
CS_gain_bound([1,.1,.1])
# %% [markdown]
# **TODO:** I should plot the P, I and D, in both cases, probably.
# **TODO:** This plot shows gain bound from w to x but I think the above plot shows gain bound to x_pid. I should build yet another system to obtain gain bounds to x only.
# **TODO:** Use IQC.jl to get gain bound from w to x only and compare to that. Can I get gain bound to all as max of gain bounds? If gain bounds are correlated for a specific w, then maximal gain bound of components might be lower than true gain bound for MIMO system. Bodemag can get the gain bound to the euclidian norm of the magnitude, and it has an implementatin in `ControlSystems.jl`.
# **TODO:** Plot both convergence rate and gain_bound in all cases. Create systems for both for all cases, and check if convergence rate improved gain bound.
# %% [markdown]
# The state is stable, but the integrator signal is not. Talk to Tore about what I should want.