fork download
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import seaborn as sns
  4.  
  5. def transfer_function(num, denum):
  6. num = np.array(num, dtype=np.float64)
  7. denum = np.array(denum, dtype=np.float64)
  8. size = len(denum) - len(num)
  9. temp = np.zeros(size)
  10. num = np.concatenate((temp, num))
  11. tf = np.vstack((num, denum))
  12. return tf
  13.  
  14. def compute_roots(tf, gains):
  15. num, denum = tf[0], tf[1]
  16. num_roots = len(np.roots(denum))
  17. roots = []
  18.  
  19. for gain in gains:
  20. ch_eq = denum + gain*num
  21. ch_roots = np.roots(ch_eq)
  22. ch_roots.sort()
  23. roots.append(ch_roots)
  24.  
  25. roots = np.vstack(roots)
  26. return roots
  27.  
  28. def plot_root_locus(gains, roots):
  29. real_vals = np.real(roots)
  30. imag_vals = np.imag(roots)
  31. colors = ['b', 'm', 'c', 'r', 'g']
  32.  
  33. fig, ax = plt.subplots()
  34. ax.set_xlabel('Re')
  35. ax.set_ylabel('Im')
  36. ax.axvline(x=0, color='k', lw=1)
  37. ax.grid(True, which='both')
  38.  
  39. ax.scatter(real_vals[0, :], imag_vals[0, :], marker='x', color='blue')
  40. ax.scatter(real_vals[-1, :], imag_vals[-1, :], marker='o', color='red')
  41.  
  42. gain_text = ['k = {:1.2f}'.format(k) for k in gains]
  43.  
  44. temp_real_vals = real_vals[1:-1, :]
  45. temp_imag_vals = imag_vals[1:-1, :]
  46. color_range = range(temp_real_vals.shape[1])
  47.  
  48. for r, i, j in zip(temp_real_vals.T, temp_imag_vals.T, color_range):
  49. ax.plot(r, i, color=colors[j])
  50.  
  51. return fig, ax
  52.  
  53. if __name__ == "__main__":
  54. num = [1, 3]
  55. denum = [1, 9, 32, 44, 21]
  56. GH = transfer_function(num, denum)
  57.  
  58. gains = np.linspace(0.0, 10.0, num=500)
  59.  
  60. roots = compute_roots(GH, gains)
  61. fig, ax = plot_root_locus(gains, roots)
  62. plt.show()
  63.  
  64. # Print the numerical values
  65. print("Numerical Values:")
  66. print("GH Numerator Coefficients:", GH[0])
  67. print("GH Denominator Coefficients:", GH[1])
  68. print("Gains:", gains)
  69. print("Roots:", roots)
Success #stdin #stdout 2.97s 120312KB
stdin
Standard input is empty
stdout
Numerical Values:
GH Numerator Coefficients: [0. 0. 0. 1. 3.]
GH Denominator Coefficients: [ 1.  9. 32. 44. 21.]
Gains: [ 0.          0.02004008  0.04008016  0.06012024  0.08016032  0.1002004
  0.12024048  0.14028056  0.16032064  0.18036072  0.2004008   0.22044088
  0.24048096  0.26052104  0.28056112  0.3006012   0.32064128  0.34068136
  0.36072144  0.38076152  0.4008016   0.42084168  0.44088176  0.46092184
  0.48096192  0.501002    0.52104208  0.54108216  0.56112224  0.58116232
  0.6012024   0.62124248  0.64128257  0.66132265  0.68136273  0.70140281
  0.72144289  0.74148297  0.76152305  0.78156313  0.80160321  0.82164329
  0.84168337  0.86172345  0.88176353  0.90180361  0.92184369  0.94188377
  0.96192385  0.98196393  1.00200401  1.02204409  1.04208417  1.06212425
  1.08216433  1.10220441  1.12224449  1.14228457  1.16232465  1.18236473
  1.20240481  1.22244489  1.24248497  1.26252505  1.28256513  1.30260521
  1.32264529  1.34268537  1.36272545  1.38276553  1.40280561  1.42284569
  1.44288577  1.46292585  1.48296593  1.50300601  1.52304609  1.54308617
  1.56312625  1.58316633  1.60320641  1.62324649  1.64328657  1.66332665
  1.68336673  1.70340681  1.72344689  1.74348697  1.76352705  1.78356713
  1.80360721  1.82364729  1.84368737  1.86372745  1.88376754  1.90380762
  1.9238477   1.94388778  1.96392786  1.98396794  2.00400802  2.0240481
  2.04408818  2.06412826  2.08416834  2.10420842  2.1242485   2.14428858
  2.16432866  2.18436874  2.20440882  2.2244489   2.24448898  2.26452906
  2.28456914  2.30460922  2.3246493   2.34468938  2.36472946  2.38476954
  2.40480962  2.4248497   2.44488978  2.46492986  2.48496994  2.50501002
  2.5250501   2.54509018  2.56513026  2.58517034  2.60521042  2.6252505
  2.64529058  2.66533066  2.68537074  2.70541082  2.7254509   2.74549098
  2.76553106  2.78557114  2.80561122  2.8256513   2.84569138  2.86573146
  2.88577154  2.90581162  2.9258517   2.94589178  2.96593186  2.98597194
  3.00601202  3.0260521   3.04609218  3.06613226  3.08617234  3.10621242
  3.12625251  3.14629259  3.16633267  3.18637275  3.20641283  3.22645291
  3.24649299  3.26653307  3.28657315  3.30661323  3.32665331  3.34669339
  3.36673347  3.38677355  3.40681363  3.42685371  3.44689379  3.46693387
  3.48697395  3.50701403  3.52705411  3.54709419  3.56713427  3.58717435
  3.60721443  3.62725451  3.64729459  3.66733467  3.68737475  3.70741483
  3.72745491  3.74749499  3.76753507  3.78757515  3.80761523  3.82765531
  3.84769539  3.86773547  3.88777555  3.90781563  3.92785571  3.94789579
  3.96793587  3.98797595  4.00801603  4.02805611  4.04809619  4.06813627
  4.08817635  4.10821643  4.12825651  4.14829659  4.16833667  4.18837675
  4.20841683  4.22845691  4.24849699  4.26853707  4.28857715  4.30861723
  4.32865731  4.34869739  4.36873747  4.38877756  4.40881764  4.42885772
  4.4488978   4.46893788  4.48897796  4.50901804  4.52905812  4.5490982
  4.56913828  4.58917836  4.60921844  4.62925852  4.6492986   4.66933868
  4.68937876  4.70941884  4.72945892  4.749499    4.76953908  4.78957916
  4.80961924  4.82965932  4.8496994   4.86973948  4.88977956  4.90981964
  4.92985972  4.9498998   4.96993988  4.98997996  5.01002004  5.03006012
  5.0501002   5.07014028  5.09018036  5.11022044  5.13026052  5.1503006
  5.17034068  5.19038076  5.21042084  5.23046092  5.250501    5.27054108
  5.29058116  5.31062124  5.33066132  5.3507014   5.37074148  5.39078156
  5.41082164  5.43086172  5.4509018   5.47094188  5.49098196  5.51102204
  5.53106212  5.5511022   5.57114228  5.59118236  5.61122244  5.63126253
  5.65130261  5.67134269  5.69138277  5.71142285  5.73146293  5.75150301
  5.77154309  5.79158317  5.81162325  5.83166333  5.85170341  5.87174349
  5.89178357  5.91182365  5.93186373  5.95190381  5.97194389  5.99198397
  6.01202405  6.03206413  6.05210421  6.07214429  6.09218437  6.11222445
  6.13226453  6.15230461  6.17234469  6.19238477  6.21242485  6.23246493
  6.25250501  6.27254509  6.29258517  6.31262525  6.33266533  6.35270541
  6.37274549  6.39278557  6.41282565  6.43286573  6.45290581  6.47294589
  6.49298597  6.51302605  6.53306613  6.55310621  6.57314629  6.59318637
  6.61322645  6.63326653  6.65330661  6.67334669  6.69338677  6.71342685
  6.73346693  6.75350701  6.77354709  6.79358717  6.81362725  6.83366733
  6.85370741  6.87374749  6.89378758  6.91382766  6.93386774  6.95390782
  6.9739479   6.99398798  7.01402806  7.03406814  7.05410822  7.0741483
  7.09418838  7.11422846  7.13426854  7.15430862  7.1743487   7.19438878
  7.21442886  7.23446894  7.25450902  7.2745491   7.29458918  7.31462926
  7.33466934  7.35470942  7.3747495   7.39478958  7.41482966  7.43486974
  7.45490982  7.4749499   7.49498998  7.51503006  7.53507014  7.55511022
  7.5751503   7.59519038  7.61523046  7.63527054  7.65531062  7.6753507
  7.69539078  7.71543086  7.73547094  7.75551102  7.7755511   7.79559118
  7.81563126  7.83567134  7.85571142  7.8757515   7.89579158  7.91583166
  7.93587174  7.95591182  7.9759519   7.99599198  8.01603206  8.03607214
  8.05611222  8.0761523   8.09619238  8.11623246  8.13627255  8.15631263
  8.17635271  8.19639279  8.21643287  8.23647295  8.25651303  8.27655311
  8.29659319  8.31663327  8.33667335  8.35671343  8.37675351  8.39679359
  8.41683367  8.43687375  8.45691383  8.47695391  8.49699399  8.51703407
  8.53707415  8.55711423  8.57715431  8.59719439  8.61723447  8.63727455
  8.65731463  8.67735471  8.69739479  8.71743487  8.73747495  8.75751503
  8.77755511  8.79759519  8.81763527  8.83767535  8.85771543  8.87775551
  8.89779559  8.91783567  8.93787575  8.95791583  8.97795591  8.99799599
  9.01803607  9.03807615  9.05811623  9.07815631  9.09819639  9.11823647
  9.13827655  9.15831663  9.17835671  9.19839679  9.21843687  9.23847695
  9.25851703  9.27855711  9.29859719  9.31863727  9.33867735  9.35871743
  9.37875752  9.3987976   9.41883768  9.43887776  9.45891784  9.47895792
  9.498998    9.51903808  9.53907816  9.55911824  9.57915832  9.5991984
  9.61923848  9.63927856  9.65931864  9.67935872  9.6993988   9.71943888
  9.73947896  9.75951904  9.77955912  9.7995992   9.81963928  9.83967936
  9.85971944  9.87975952  9.8997996   9.91983968  9.93987976  9.95991984
  9.97995992 10.        ]
Roots: [[-3.37018261-2.01196743j -3.37018261+2.01196743j -1.12981739-0.29428766j
  -1.12981739+0.29428766j]
 [-3.37011001-2.01084705j -3.37011001+2.01084705j -1.12988999-0.30130883j
  -1.12988999+0.30130883j]
 [-3.37003837-2.00972603j -3.37003837+2.00972603j -1.12996163-0.30817722j
  -1.12996163+0.30817722j]
 ...
 [-3.47733194-1.44839641j -3.47733194+1.44839641j -1.02266806-1.59368605j
  -1.02266806+1.59368605j]
 [-3.47777941-1.44739446j -3.47777941+1.44739446j -1.02222059-1.59528487j
  -1.02222059+1.59528487j]
 [-3.47822727-1.44639333j -3.47822727+1.44639333j -1.02177273-1.59688158j
  -1.02177273+1.59688158j]]