Skip to content

Commit 4953565

Browse files
committed
Add plus method
Signed-off-by: ErikQQY <[email protected]>
1 parent b4f6233 commit 4953565

File tree

4 files changed

+63
-23
lines changed

4 files changed

+63
-23
lines changed

docs/make.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ makedocs(;
1717
"FractionalSystems.jl" => "index.md",
1818
"Get Started" => "get_started.md",
1919
"Root Locus" => "rlocus.md",
20+
"Frequency Analysis" => "frequency.md",
2021
"Conversation" => "conversation.md"
2122
],
2223
)

docs/src/frequency.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
# Frequency domain analysis
22

3+
Frequency analysis is a powerful tool in system analysis, we can access the properties of the system in frequency domain to reveal the stability, gain margin, phase margin etc.
4+
5+
In FractionalSystems.jl, it is also easy to transform the system to frequency domain:
6+
7+
```julia-repl
8+
julia>
9+
```

src/fotf/fotf.jl

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,48 @@ Add two fractional order model.
5656
You can't add two systems with different I/O delay.
5757
5858
"""
59-
#=function +(G1::FOTF, G2::FOTF)
60-
59+
function +(G1::FOTF, G2::FOTF)
60+
G = sisoplus(G1, G2)
61+
return G
6162
end
62-
=#
63-
#=
64-
function sisoplus(G1, G2)
63+
64+
"""
65+
sisoplus(G1, G2)
66+
67+
The addition of two single-input-single-output FOTF.
68+
"""
69+
function sisoplus(G1::FOTF, G2::FOTF)
6570
if iszero(G2)
6671
return G1
6772
elseif G1.ioDelay == G2.ioDelay
68-
key=0
69-
G1=simplify(G1)
70-
G2=simplify(G2)
71-
(a1, na1, b1, nb1)=fotfdata(G1)
72-
(a2, na2, b2, nb2)=fotfdata(G2)
73+
key = 0
74+
G1 = simplify(G1)
75+
G2 = simplify(G2)
76+
(a1, na1, b1, nb1) = fotfdata(G1)
77+
(a2, na2, b2, nb2) = fotfdata(G2)
7378
if length(a1) == length(a2)
74-
if all()
79+
if a1 == a2 && na1 == na2
80+
a=a1
81+
na=na1
82+
b=[b1; b2]
83+
nb=[nb1; nb2]
84+
key=1
85+
end
86+
end
87+
88+
if key == 0
89+
a = kron(a1, a2)
90+
na = kronsum(na1, na2)
91+
b = [kron(a1, b2); kron(b1, a2)]
92+
nb = [kronsum(na1, nb2); kronsum(nb1, na2)]
93+
end
94+
95+
G = fotf(a, na, b, nb)
96+
return G
7597
end
7698
end
77-
=#
7899

79-
function simplify(G::FOTF, tol=0.000001)
100+
function simplify(G::FOTF, tol=0.0000000001)
80101
(b, nb) = polyuniq(G.num, G.nn, tol)
81102
(a, na) = polyuniq(G.den, G.nd, tol)
82103

@@ -107,12 +128,13 @@ end
107128
function polyuniq(a, an, tol)
108129
an = sort(an, rev=true)
109130
ii = sortperm(an, rev=true)
131+
110132
a = a[ii]
111133
ax = diff(an)
112-
key=1
134+
key::Int64 = 1
113135
for i = 1:length(ax)
114136
if abs(ax[i]) <= tol
115-
a[key] = a[key]+a[key+1]
137+
a[key] = a[key] + a[key+1]
116138
deleteat!(a, key+1)
117139
deleteat!(an, key+1)
118140
else
@@ -131,15 +153,19 @@ end
131153
132154
Return the Kronecker sum of two matrices
133155
"""
134-
function kronsum(A, B)
135-
(ma, na) = size(A)
136-
(mb, nb) = size(B)
156+
function kronsum(A::T, B::T) where {T <: AbstractArray}
157+
ma, na = size(A, 2), size(A, 1)
158+
mb, nb = size(B, 2), size(B, 1)
137159
A = reshape(A, 1, ma, 1, na)
138160
B= reshape(B, mb, 1, nb, 1)
139161
C = reshape(broadcast(+, A, B), (mb*mb, na*nb))
140162
return C
141163
end
142164

165+
function kronsum(A::T, B::T) where {T <: Number}
166+
return A+B
167+
end
168+
143169

144170
"""
145171
fotf2cotf(tf)
@@ -232,11 +258,11 @@ julia> fotfdata(G)
232258
```
233259
"""
234260
function fotfdata(G::FOTF)
235-
b=G.num
236-
a=G.den
237-
nb=G.nn
238-
na=G.nd
239-
L=G.ioDelay
261+
b = G.num
262+
a = G.den
263+
nb = G.nn
264+
na = G.nd
265+
L = G.ioDelay
240266

241267
return [a, na, b, nb, L]
242268
end

src/fotf/freqresp.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
using ControlSystems
22

3+
"""
4+
freqresp(s, G)
5+
6+
The frequency response of FOTF.
7+
"""
38
function ControlSystems.freqresp(s, G::FOTF)
49
(a, na, b, nb, L) = fotfdata(G)
510

@@ -11,6 +16,7 @@ function ControlSystems.freqresp(s, G::FOTF)
1116
Q = a'*(s[k].^na)
1217
H1[k] = P/Q
1318
end
19+
1420
if L>0
1521
H1 = H1.*exp.(L*s)
1622
end

0 commit comments

Comments
 (0)