就是個離散控制下的Jury’s Test測試小程式。

gist

原始碼

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""
Title: Jury's Test For Descrete Control System
Copyright: Dephilia
Date: 2019/09/23
"""



def calnextrow(row):
  """This will cal a new row for test."""
  
  # input: an an-1...
  # ouput: bn bn-1...
  size=len(row)
  nextrow=[]
  row_rev=row[::-1]
  for i in range(size-1):
    temp=row[0]*row_rev[i+1]-row[i+1]*row_rev[0]
    nextrow.append(temp)
    
  return nextrow

def main():

  # Jury's test
  
  """
  Input example:
  
  [1,0,-0.25]
  """

  import ast
  intext=input("Input characteristic equation as array:\nFor example:[1,0,-0.25]\n")

  testlist=ast.literal_eval(intext)

  order=len(testlist)-1

  # a_n>0
  if testlist[0]<0:
    print("a_n must large than 0.")
    return 0

  # First check Q(1)>0
  r1=sum(testlist)
  print("Q(1)=",r1)
  if r1<=0:
    print("Unstable")
    return 0

  # Next check (-1)^nQ(-1)>0

  r2=(-1)**order*sum([testlist[i]*((-1)**(order-i)) for i in range(order)]+[testlist[-1]])
  print("(-1)^n Q(-1)=",r2)
  if r2<=0:
    print("Unstable")
    return 0

  # Check |a0|<an
  r3=abs(testlist[-1])<testlist[0]
  print("|a0|<an=",r3)
  if not r3:
    print("Unstable")
    return 0

  # Establish Table
  deal=testlist
  print(0," - ",deal)
  print(0," - ",deal[::-1])
  for i in range(order-1):
    deal=calnextrow(deal)
    print(i+1," - ",deal)
    print(i+1," - ",deal[::-1])
    if abs(deal[0])>=abs(deal[-1]):
      print("Unstable")
      return 0

  print("\033[1;35m Stable \033[0m")

if __name__=="__main__":
  main()