Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tutorial analyzing hybrid systems #903

Merged
merged 4 commits into from
Dec 4, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add zoh example
  • Loading branch information
baggepinnen committed Dec 4, 2023
commit 3e653f2fe563f1af0e3718af1d1d815af94dccf1
51 changes: 51 additions & 0 deletions example/zoh.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# In this example, we analyze the effect of ZoH sampling in continuous time and compare it to the equivalent discrete-time system
using ControlSystems, Plots
s = tf('s')
P = tf(0.1, [1, 0.1, 0.1])

Ts = 1 # Sample interval
Z = (1 - delay(Ts))/s # The transfer function of the ZoH operator
Pd = c2d(P, Ts) # The equivalent discrete-time system
Pz = P*Z # The continuous-time version of the discrete-time system

w = exp10.(-2:0.01:log10(2*0.5))

bodeplot(P, w, lab="P")
bodeplot!(Pz, w, lab="P zoh")
bodeplot!(Pd, w, lab="Pd")

vline!([0.5], l=(:black, :dash), lab="Nyquist freq.", legend=:bottomleft)

##
# The step response of Pz matches the discrete output of Pd delayed by half the sample time
resP = step(P, 12)
resPz = step(Pz, 12)
resPd = step(Pd, 12)
plot([resP, resPz, resPd], lab=["P" "Pz" "Pd"])

t_shift = resPd.t .+ Ts / 2
plot!(t_shift, resPd.y[:], lab="Pd shifted", m=:o, l=:dash)


##
# With a piecewise constant input, even if it's not a step, we get the same result
Tf = 20
ufun = (x,t)->[sin(2pi*floor(t/Ts)*Ts/5)]
resP = lsim(P, ufun, Tf)
resPz = lsim(Pz, ufun, 0:0.01:Tf)
resPd = lsim(Pd, ufun, Tf)
plot([resP, resPz, resPd], lab=["P" "Pz" "Pd"])

t_shift = resPd.t .+ Ts / 2
plot!(t_shift, resPd.y[:], lab="Pd shifted", m=:o)

##
# With a continuous input signal, the result is different,
# after the initial transient, the output of Pz matches that of Pd exactly
# try plotting with the plotly() backend and zoom in at the end
Tf = 150
ufun = (x,t)->[sin(2pi*t/5)]
resP = lsim(P, ufun, Tf)
resPz = lsim(Pz, ufun, 0:0.01:Tf)
resPd = lsim(Pd, ufun, Tf)
plot([resP, resPz, resPd], lab=["P" "Pz" "Pd"])